Skip to content

Commit a972baf

Browse files
authored
feat: Get Maps API key from unversioned file instead of resource - kotlin (googlemaps-samples#189)
New process to add your own Google Maps API key: 1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key) 1. Create a file in the `ApiDemos/kotlin/app` directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) 1. Add a single line to `ApiDemos/kotlin/app/secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step 1. Build and run
1 parent b2246b1 commit a972baf

File tree

6 files changed

+34
-27
lines changed

6 files changed

+34
-27
lines changed

ApiDemos/kotlin/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ This sample use the Gradle build system.
2323
First download the samples by cloning this repository or downloading an archived
2424
snapshot. (See the options at the top of the page.)
2525

26-
In Android Studio, use "Open an existing Android Studio project". Next select the ApiDemos/kotlin/ directory that you downloaded
26+
In Android Studio, use "Open an existing Android Studio project". Next select the `ApiDemos/kotlin/` directory that you downloaded
2727
from this repository.
2828
If prompted for a gradle configuration accept the default settings.
2929

30-
Alternatively use the "gradlew build" command to build the project directly.
30+
Alternatively use the `gradlew build` command to build the project directly.
3131

32-
Add your API key to the file `debug/values/google_maps_api.xml`.
33-
It's pulled from there into your app's `AndroidManifest.xml` file.
34-
See the [quick guide to getting an API key](https://developers.google.com/maps/documentation/android-api/signup).
32+
This demo app requires that you add your own Google Maps API key:
33+
34+
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
35+
1. Create a file in the `ApiDemos/kotlin/app` directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
36+
1. Add a single line to `ApiDemos/kotlin/app/secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
37+
1. Build and run
3538

3639
Support
3740
-------

ApiDemos/kotlin/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/build
2+
secure.properties

ApiDemos/kotlin/app/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ android {
1313
versionCode 1
1414
versionName "1.0"
1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
17+
// Read the API key from ApiDemos/kotlin/app/secure.properties into R.string.maps_api_key
18+
def secureProps = new Properties()
19+
if (file("secure.properties").exists()) {
20+
file("secure.properties")?.withInputStream { secureProps.load(it) }
21+
}
22+
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
23+
24+
// To add your Maps API key to this project:
25+
// 1. Create a file ApiDemos/kotlin/app/secure.properties
26+
// 2. Add this line, where YOUR_API_KEY is your API key:
27+
// MAPS_API_KEY=YOUR_API_KEY
1628
}
1729
buildTypes {
1830
release {

ApiDemos/kotlin/app/src/debug/res/values/google_maps_api.xml

Lines changed: 0 additions & 17 deletions
This file was deleted.

ApiDemos/kotlin/app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@
2828
android:roundIcon="@mipmap/ic_launcher_round"
2929
android:supportsRtl="true"
3030
android:theme="@style/AppTheme">
31+
<!--
32+
To add your Maps API key to this project:
33+
1. Create a file ApiDemos/kotlin/app/secure.properties
34+
2. Add this line, where YOUR_API_KEY is your API key:
35+
MAPS_API_KEY=YOUR_API_KEY
36+
-->
3137
<meta-data
3238
android:name="com.google.android.geo.API_KEY"
33-
android:value="@string/google_maps_key" />
39+
android:value="@string/maps_api_key" />
3440
<activity android:name=".MainActivity">
3541
<intent-filter>
3642
<action android:name="android.intent.action.MAIN" />

ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos/MainActivity.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ import android.content.Intent
2222
import android.os.Bundle
2323
import android.view.View
2424
import android.view.ViewGroup
25-
import android.widget.AdapterView
26-
import android.widget.ArrayAdapter
27-
import android.widget.ListAdapter
28-
import android.widget.ListView
25+
import android.widget.*
2926
import androidx.appcompat.app.AppCompatActivity
3027

3128
/**
@@ -51,6 +48,11 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
5148
onItemClickListener = this@MainActivity
5249
emptyView = emptyMessage
5350
}
51+
52+
if (getString(R.string.maps_api_key).isEmpty()) {
53+
Toast.makeText(this, "Add your own API key in ApiDemos/kotlin/app/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show()
54+
}
55+
5456
}
5557

5658
/**

0 commit comments

Comments
 (0)