From 2ab19b7e0dfec52ab5cacdbe848317a36675d6be Mon Sep 17 00:00:00 2001 From: Chris Arriola Date: Wed, 25 Nov 2020 13:38:00 -0800 Subject: [PATCH] feat: Using local.properties to provide API key (#398) * feat: Using local.properties to provide API key instead of secure.properties. * Remove redundant declaration in main AndroidManifest.xml * Migrate ApiDemos - Kotlin to use local.properties. * Update README for tutorials. * Genericize. * Remove references to secure.properties. * s/master/main --- .gitignore | 1 - ApiDemos/java/README.md | 22 ++- ApiDemos/java/app/build.gradle | 24 +++- ApiDemos/java/app/src/gms/AndroidManifest.xml | 25 ---- .../com/example/mapdemo/MainActivity.java | 4 +- .../java/app/src/main/AndroidManifest.xml | 128 ++---------------- ApiDemos/java/app/src/v3/AndroidManifest.xml | 25 ---- .../com/example/mapdemo/MainActivity.java | 4 +- ApiDemos/java/gradle.properties | 1 + ApiDemos/kotlin/README.md | 31 ++--- ApiDemos/kotlin/app/build.gradle | 25 ++-- .../kotlin/app/src/gms/AndroidManifest.xml | 15 +- .../com/example/kotlindemos/MainActivity.kt | 5 +- .../kotlin/app/src/main/AndroidManifest.xml | 54 ++------ .../kotlin/app/src/v3/AndroidManifest.xml | 15 +- .../com/example/kotlindemos/MainActivity.kt | 4 +- README.md | 9 +- docs/GET_AN_API_KEY.md | 8 ++ secure.properties.template | 3 - .../java/CurrentPlaceDetailsOnMap/README.md | 15 +- .../CurrentPlaceDetailsOnMap/app/build.gradle | 23 ++-- tutorials/java/MapWithMarker/README.md | 13 +- tutorials/java/MapWithMarker/app/build.gradle | 20 ++- .../mapwithmarker/MapsMarkerActivity.java | 4 +- tutorials/java/Polygons/README.md | 9 +- tutorials/java/Polygons/app/build.gradle | 19 ++- tutorials/java/StyledMap/README.md | 9 +- tutorials/java/StyledMap/app/build.gradle | 21 +-- .../kotlin/CurrentPlaceDetailsOnMap/README.md | 10 +- .../CurrentPlaceDetailsOnMap/app/build.gradle | 19 ++- tutorials/kotlin/MapWithMarker/README.md | 14 +- .../kotlin/MapWithMarker/app/build.gradle | 19 ++- .../mapwithmarker/MapsMarkerActivity.kt | 5 +- tutorials/kotlin/Polygons/README.md | 9 +- tutorials/kotlin/Polygons/app/build.gradle | 19 ++- 35 files changed, 218 insertions(+), 413 deletions(-) create mode 100644 docs/GET_AN_API_KEY.md delete mode 100644 secure.properties.template diff --git a/.gitignore b/.gitignore index 108a5ca2..73145024 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ build/ *.iml local.properties .DS_Store -secure.properties diff --git a/ApiDemos/java/README.md b/ApiDemos/java/README.md index 0d1a310c..cb2fff59 100644 --- a/ApiDemos/java/README.md +++ b/ApiDemos/java/README.md @@ -7,20 +7,19 @@ in the Java programming language. They demonstrate most of the features available in the API. -This app was written with a minSdk of 16 and the androidx appcompat library, but it can be easily -adapted to use native functionality instead (for example replacing `SupportMapFragment` with -`MapFragment`). - -The Maps SDK for Android samples can be found under the `gms` gradle product flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle product flavor. The active product flavor can be modified through Android Studio’s “Build Variants” toolbar options. +The Maps SDK for Android samples can be found under the `gms` gradle product +flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle +product flavor. The active product flavor can be modified through +Android Studio’s “Build Variants” toolbar options. Pre-requisites -------------- -- Android SDK v29 +- Android SDK v30 - Latest Android Build Tools - Android Support Repository - Google Repository -- Google Play services +- Google Play Services Getting Started --------------- @@ -35,12 +34,7 @@ If prompted for a gradle configuration accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) -1. Add a single line to `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 +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -61,4 +55,4 @@ CONTRIBUTING.md. License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/ApiDemos/java/app/build.gradle b/ApiDemos/java/app/build.gradle index fad1b6a2..fc4aa965 100644 --- a/ApiDemos/java/app/build.gradle +++ b/ApiDemos/java/app/build.gradle @@ -3,6 +3,15 @@ import org.apache.tools.ant.filters.ConcatFilter apply plugin: 'com.android.application' apply plugin: 'project-report' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 30 @@ -14,15 +23,16 @@ android { versionName "1.0" multiDexEnabled true - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../secure.properties").exists()) { - file("../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Inject the Maps API key into the manifest + manifestPlaceholders = [ mapsApiKey : mapsApiKey ] + + // Reading the Maps API key into a BuildConfig value. This is only + // necessary so that the sample app can display a toast message if the + // API key was not set, otherwise, this can be skipped. + buildConfigField("String", "MAPS_API_KEY", "\"$mapsApiKey\"" ) // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY } diff --git a/ApiDemos/java/app/src/gms/AndroidManifest.xml b/ApiDemos/java/app/src/gms/AndroidManifest.xml index cf58787f..e4b6c50f 100644 --- a/ApiDemos/java/app/src/gms/AndroidManifest.xml +++ b/ApiDemos/java/app/src/gms/AndroidManifest.xml @@ -17,21 +17,6 @@ - - - - - - - - - - diff --git a/ApiDemos/java/app/src/gms/java/com/example/mapdemo/MainActivity.java b/ApiDemos/java/app/src/gms/java/com/example/mapdemo/MainActivity.java index 37921dcf..62529229 100644 --- a/ApiDemos/java/app/src/gms/java/com/example/mapdemo/MainActivity.java +++ b/ApiDemos/java/app/src/gms/java/com/example/mapdemo/MainActivity.java @@ -85,8 +85,8 @@ public final class MainActivity extends AppCompatActivity list.setOnItemClickListener(this); list.setEmptyView(findViewById(R.id.empty)); - if (getString(R.string.maps_api_key).isEmpty()) { - Toast.makeText(this, "Add your own API key in ApiDemos/java/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show(); + if (BuildConfig.MAPS_API_KEY.isEmpty()) { + Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show(); } } diff --git a/ApiDemos/java/app/src/main/AndroidManifest.xml b/ApiDemos/java/app/src/main/AndroidManifest.xml index 8a25c709..e5cd8c3b 100644 --- a/ApiDemos/java/app/src/main/AndroidManifest.xml +++ b/ApiDemos/java/app/src/main/AndroidManifest.xml @@ -17,6 +17,13 @@ + @@ -44,129 +51,14 @@ android:theme="@style/AppTheme"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + android:value="${mapsApiKey}" /> diff --git a/ApiDemos/java/app/src/v3/AndroidManifest.xml b/ApiDemos/java/app/src/v3/AndroidManifest.xml index 2b176f66..446588dd 100644 --- a/ApiDemos/java/app/src/v3/AndroidManifest.xml +++ b/ApiDemos/java/app/src/v3/AndroidManifest.xml @@ -17,21 +17,6 @@ - - - - - - - - - - diff --git a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MainActivity.java b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MainActivity.java index 164564f6..e308c30f 100644 --- a/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MainActivity.java +++ b/ApiDemos/java/app/src/v3/java/com/example/mapdemo/MainActivity.java @@ -85,8 +85,8 @@ public final class MainActivity extends AppCompatActivity list.setOnItemClickListener(this); list.setEmptyView(findViewById(R.id.empty)); - if (getString(R.string.maps_api_key).isEmpty()) { - Toast.makeText(this, "Add your own API key in ApiDemos/java/app/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show(); + if (BuildConfig.MAPS_API_KEY.isEmpty()) { + Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show(); } } diff --git a/ApiDemos/java/gradle.properties b/ApiDemos/java/gradle.properties index acf164f6..3e488398 100644 --- a/ApiDemos/java/gradle.properties +++ b/ApiDemos/java/gradle.properties @@ -18,3 +18,4 @@ # org.gradle.parallel=true android.enableJetifier=true android.useAndroidX=true +GOOGLE_MAPS_API_KEY="AIzaSyBri0nHwJxuIs8KgdsGXdJLFvg1vviUMfo" diff --git a/ApiDemos/kotlin/README.md b/ApiDemos/kotlin/README.md index 225099fe..c1d5bad2 100644 --- a/ApiDemos/kotlin/README.md +++ b/ApiDemos/kotlin/README.md @@ -1,21 +1,25 @@ Google Maps Android API Demos =================================== -These are demos for the [Google Maps Android API v2](https://developers.google.com/maps/documentation/android-api/) in Kotlin. +These are demos for the [Maps SDK for Android](https://developers.google.com/maps/documentation/android-api/) +and [Maps SDK for Android V3 BETA](https://developers.google.com/maps/documentation/android-sdk/v3-client-migration) libraries +in Kotlin. + They demonstrate most of the features available in the API. -This app was written for a minSdk of 15 and the v4 support library, but it can be easily adapted to -use native functionality instead. -(For example replacing ``SupportMapFragment`` with ``MapFragment``). - -The Maps SDK for Android samples can be found under the `gms` gradle product flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle product flavor. The active product flavor can be modified through Android Studio’s “Build Variants” toolbar options. +The Maps SDK for Android samples can be found under the `gms` gradle product +flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle +product flavor. The active product flavor can be modified through +Android Studio’s “Build Variants” toolbar options. Pre-requisites -------------- -- Android SDK v27 +- Android SDK v30 - Latest Android Build Tools - Android Support Repository +- Google Repository +- Google Play Services Getting Started --------------- @@ -25,18 +29,13 @@ This sample uses the Gradle build system. First download the samples by cloning this repository or downloading an archived snapshot. (See the options at the top of the page.) -In Android Studio, use "Open an existing Android Studio project". Next select the `ApiDemos/kotlin/` directory that you downloaded -from this repository. +In Android Studio, use "Open an existing Android Studio project". Next select +the `ApiDemos/kotlin/` directory that you downloaded from this repository. If prompted for a gradle configuration accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) -1. Add a single line to `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 +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -57,4 +56,4 @@ CONTRIBUTING.md. License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/ApiDemos/kotlin/app/build.gradle b/ApiDemos/kotlin/app/build.gradle index 3210c3fc..3c30b0af 100644 --- a/ApiDemos/kotlin/app/build.gradle +++ b/ApiDemos/kotlin/app/build.gradle @@ -4,6 +4,15 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 30 defaultConfig { @@ -15,15 +24,16 @@ android { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" multiDexEnabled true - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../secure.properties").exists()) { - file("../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Inject the Maps API key into the manifest + manifestPlaceholders = [ mapsApiKey : mapsApiKey ] + + // Reading the Maps API key into a BuildConfig value. This is only + // necessary so that the sample app can display a toast message if the + // API key was not set, otherwise, this can be skipped. + buildConfigField("String", "MAPS_API_KEY", "\"$mapsApiKey\"" ) // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY } @@ -52,7 +62,6 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.2.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'com.android.support.constraint:constraint-layout:2.0.4' implementation "androidx.cardview:cardview:1.0.0" implementation "androidx.recyclerview:recyclerview:1.1.0" implementation 'androidx.multidex:multidex:2.0.1' diff --git a/ApiDemos/kotlin/app/src/gms/AndroidManifest.xml b/ApiDemos/kotlin/app/src/gms/AndroidManifest.xml index 21b6a9d4..c709014c 100644 --- a/ApiDemos/kotlin/app/src/gms/AndroidManifest.xml +++ b/ApiDemos/kotlin/app/src/gms/AndroidManifest.xml @@ -16,11 +16,6 @@ - - - - - - + diff --git a/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/MainActivity.kt b/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/MainActivity.kt index eee73e00..20a40032 100644 --- a/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/MainActivity.kt +++ b/ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/MainActivity.kt @@ -49,10 +49,9 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener { emptyView = emptyMessage } - if (getString(R.string.maps_api_key).isEmpty()) { - 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() + if (BuildConfig.MAPS_API_KEY.isEmpty()) { + Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show() } - } /** diff --git a/ApiDemos/kotlin/app/src/main/AndroidManifest.xml b/ApiDemos/kotlin/app/src/main/AndroidManifest.xml index 46571aa3..d0639b6c 100644 --- a/ApiDemos/kotlin/app/src/main/AndroidManifest.xml +++ b/ApiDemos/kotlin/app/src/main/AndroidManifest.xml @@ -16,13 +16,22 @@ + - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + android:value="${mapsApiKey}" /> \ No newline at end of file diff --git a/ApiDemos/kotlin/app/src/v3/AndroidManifest.xml b/ApiDemos/kotlin/app/src/v3/AndroidManifest.xml index b272583b..6d156943 100644 --- a/ApiDemos/kotlin/app/src/v3/AndroidManifest.xml +++ b/ApiDemos/kotlin/app/src/v3/AndroidManifest.xml @@ -16,11 +16,6 @@ - - - - - - + diff --git a/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/MainActivity.kt b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/MainActivity.kt index 44192427..f65bc27e 100644 --- a/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/MainActivity.kt +++ b/ApiDemos/kotlin/app/src/v3/java/com/example/kotlindemos/MainActivity.kt @@ -56,8 +56,8 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener { emptyView = emptyMessage } - if (getString(R.string.maps_api_key).isEmpty()) { - 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() + if (BuildConfig.MAPS_API_KEY.isEmpty()) { + Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show() } } diff --git a/README.md b/README.md index 41e41777..2b2a2105 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This repo contains the following samples: 1. [WearOS](WearOS): Displays a map on a Wear OS device. This sample demonstrates the basic setup required for a gradle-based Android Studio project. -1. [Tutorials](https://github.com/googlemaps/android-samples/tree/master/tutorials): Samples +1. [Tutorials](https://github.com/googlemaps/android-samples/tree/main/tutorials): Samples associated with tutorials in the developer's guide. See each sample for a link to the associated guide. 1. [Snippets](snippets): Snippets for code found in https://developers.google.com/maps/documentation/android-sdk @@ -39,12 +39,7 @@ If prompted for a gradle configuration accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -The demo apps require that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) -1. Add a single line to `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 +The demo apps require that you add your own Google Maps API key. See [Get an API Key](docs/GET_AN_API_KEY.md) docs for more instructions. Support ------- diff --git a/docs/GET_AN_API_KEY.md b/docs/GET_AN_API_KEY.md new file mode 100644 index 00000000..83577619 --- /dev/null +++ b/docs/GET_AN_API_KEY.md @@ -0,0 +1,8 @@ +# Getting an API Key + +The demos found in this repository require an API key. To create and use an API key: + + 1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key) + 1. Open the file in the root directory of the sample called `local.properties` (by default, this file is **NOT** under version control) + 1. Add a single line to `local.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 diff --git a/secure.properties.template b/secure.properties.template deleted file mode 100644 index ad5874bf..00000000 --- a/secure.properties.template +++ /dev/null @@ -1,3 +0,0 @@ -# This is a template for the `secure.properties` file -# To use this, replace "YOUR_API_KEY" with your GMP API key and rename this file to `secure.properties` -MAPS_API_KEY="YOUR_API_KEY" diff --git a/tutorials/java/CurrentPlaceDetailsOnMap/README.md b/tutorials/java/CurrentPlaceDetailsOnMap/README.md index be9f59a0..04ebe351 100644 --- a/tutorials/java/CurrentPlaceDetailsOnMap/README.md +++ b/tutorials/java/CurrentPlaceDetailsOnMap/README.md @@ -7,9 +7,11 @@ This sample goes hand in hand with a tutorial for the Google Maps Android API: Prerequisites -------------- -- Android SDK v24 +- Android SDK v30 - Latest Android Build Tools - Android Support Repository +- Google Repository +- Google Play Services Getting started --------------- @@ -24,13 +26,8 @@ This sample uses the Gradle build system. 1. If prompted for a gradle configuration, accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) - 1. Add a single line to `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 - +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions. + Support ------- @@ -47,4 +44,4 @@ https://github.com/googlemaps/android-samples/issues License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/tutorials/java/CurrentPlaceDetailsOnMap/app/build.gradle b/tutorials/java/CurrentPlaceDetailsOnMap/app/build.gradle index da690fcb..0426de3d 100644 --- a/tutorials/java/CurrentPlaceDetailsOnMap/app/build.gradle +++ b/tutorials/java/CurrentPlaceDetailsOnMap/app/build.gradle @@ -1,25 +1,30 @@ apply plugin: 'com.android.application' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { - compileSdkVersion 29 + compileSdkVersion 30 buildToolsVersion '28.0.3' defaultConfig { applicationId "com.example.currentplacedetailsonmap" minSdkVersion 17 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../../secure.properties").exists()) { - file("../../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Read the API key from local.properties into R.string.maps_api_key + resValue "string", "maps_api_key", mapsApiKey // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY diff --git a/tutorials/java/MapWithMarker/README.md b/tutorials/java/MapWithMarker/README.md index 5927bbbc..1c940d62 100644 --- a/tutorials/java/MapWithMarker/README.md +++ b/tutorials/java/MapWithMarker/README.md @@ -7,9 +7,11 @@ This sample goes hand in hand with a tutorial for the Google Maps Android API: Prerequisites -------------- -- Android SDK v24 +- Android SDK v30 - Latest Android Build Tools - Android Support Repository +- Google Repository +- Google Play Services Getting started --------------- @@ -23,12 +25,7 @@ This sample uses the Gradle build system. 1. If prompted for a gradle configuration, accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) - 1. Add a single line to `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 +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -46,4 +43,4 @@ https://github.com/googlemaps/android-samples/issues License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/tutorials/java/MapWithMarker/app/build.gradle b/tutorials/java/MapWithMarker/app/build.gradle index f2650fca..f6fa332c 100644 --- a/tutorials/java/MapWithMarker/app/build.gradle +++ b/tutorials/java/MapWithMarker/app/build.gradle @@ -1,5 +1,14 @@ apply plugin: 'com.android.application' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 29 buildToolsVersion "28.0.3" @@ -11,17 +20,14 @@ android { versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../../secure.properties").exists()) { - file("../../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Read the API key from local.properties into R.string.maps_api_key + resValue "string", "maps_api_key", mapsApiKey // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY + } buildTypes { release { diff --git a/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java b/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java index 72a66783..93ac030b 100644 --- a/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java +++ b/tutorials/java/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.java @@ -38,8 +38,8 @@ public class MapsMarkerActivity extends AppCompatActivity super.onCreate(savedInstanceState); // Retrieve the content view that renders the map. setContentView(R.layout.activity_maps); - // Get the SupportMapFragment and request notification - // when the map is ready to be used. + + // Get the SupportMapFragment and request notification when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); diff --git a/tutorials/java/Polygons/README.md b/tutorials/java/Polygons/README.md index 47b332da..1a3af32c 100644 --- a/tutorials/java/Polygons/README.md +++ b/tutorials/java/Polygons/README.md @@ -23,13 +23,8 @@ This sample uses the Gradle build system. 1. Select the `Polygons` directory that you downloaded with this repository. 1. If prompted for a gradle configuration, accept the default settings. Alternatively use the `gradlew build` command to build the project directly. - -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) - 1. Add a single line to `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 +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -47,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/tutorials/java/Polygons/app/build.gradle b/tutorials/java/Polygons/app/build.gradle index d5c18e15..0026fc2c 100644 --- a/tutorials/java/Polygons/app/build.gradle +++ b/tutorials/java/Polygons/app/build.gradle @@ -1,5 +1,14 @@ apply plugin: 'com.android.application' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 29 buildToolsVersion '28.0.3' @@ -11,15 +20,11 @@ android { versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../../secure.properties").exists()) { - file("../../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Read the API key from local.properties into R.string.maps_api_key + resValue "string", "maps_api_key", mapsApiKey // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY diff --git a/tutorials/java/StyledMap/README.md b/tutorials/java/StyledMap/README.md index 843c8b45..3a94291f 100644 --- a/tutorials/java/StyledMap/README.md +++ b/tutorials/java/StyledMap/README.md @@ -24,12 +24,7 @@ This sample uses the Gradle build system. 1. If prompted for a gradle configuration, accept the default settings. Alternatively use the "gradlew build" command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) - 1. Add a single line to `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 +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -47,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/tutorials/java/StyledMap/app/build.gradle b/tutorials/java/StyledMap/app/build.gradle index 832ac206..94aa4e1e 100644 --- a/tutorials/java/StyledMap/app/build.gradle +++ b/tutorials/java/StyledMap/app/build.gradle @@ -1,5 +1,14 @@ apply plugin: 'com.android.application' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 29 buildToolsVersion '28.0.3' @@ -10,16 +19,12 @@ android { versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../../secure.properties").exists()) { - file("../../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + + // Read the API key from local.properties into R.string.maps_api_key + resValue "string", "maps_api_key", mapsApiKey // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY } diff --git a/tutorials/kotlin/CurrentPlaceDetailsOnMap/README.md b/tutorials/kotlin/CurrentPlaceDetailsOnMap/README.md index a9362f23..8dc48067 100644 --- a/tutorials/kotlin/CurrentPlaceDetailsOnMap/README.md +++ b/tutorials/kotlin/CurrentPlaceDetailsOnMap/README.md @@ -24,13 +24,7 @@ This sample uses the Gradle build system. 1. If prompted for a gradle configuration, accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) - 1. Add a single line to `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 - +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -48,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/tutorials/kotlin/CurrentPlaceDetailsOnMap/app/build.gradle b/tutorials/kotlin/CurrentPlaceDetailsOnMap/app/build.gradle index 9309e418..940913a7 100644 --- a/tutorials/kotlin/CurrentPlaceDetailsOnMap/app/build.gradle +++ b/tutorials/kotlin/CurrentPlaceDetailsOnMap/app/build.gradle @@ -2,6 +2,15 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 29 buildToolsVersion '28.0.3' @@ -13,15 +22,11 @@ android { versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../../secure.properties").exists()) { - file("../../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Read the API key from local.properties into R.string.maps_api_key + resValue "string", "maps_api_key", mapsApiKey // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY diff --git a/tutorials/kotlin/MapWithMarker/README.md b/tutorials/kotlin/MapWithMarker/README.md index 8718f456..fe7d9da4 100644 --- a/tutorials/kotlin/MapWithMarker/README.md +++ b/tutorials/kotlin/MapWithMarker/README.md @@ -7,9 +7,11 @@ This sample goes hand in hand with a tutorial for the Google Maps Android API: Prerequisites -------------- -- Android SDK v24 +- Android SDK v30 - Latest Android Build Tools - Android Support Repository +- Google Repository +- Google Play Services Getting started --------------- @@ -23,13 +25,7 @@ This sample uses the Gradle build system. 1. If prompted for a gradle configuration, accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) - 1. Add a single line to `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 - +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -47,4 +43,4 @@ https://github.com/googlemaps/android-samples/issues License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/tutorials/kotlin/MapWithMarker/app/build.gradle b/tutorials/kotlin/MapWithMarker/app/build.gradle index 8a391e74..c97b7e49 100644 --- a/tutorials/kotlin/MapWithMarker/app/build.gradle +++ b/tutorials/kotlin/MapWithMarker/app/build.gradle @@ -2,6 +2,15 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 29 buildToolsVersion "28.0.3" @@ -13,15 +22,11 @@ android { versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../../secure.properties").exists()) { - file("../../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Read the API key from local.properties into R.string.maps_api_key + resValue "string", "maps_api_key", mapsApiKey // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY } diff --git a/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt b/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt index 0bce9c30..8502eb9f 100644 --- a/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt +++ b/tutorials/kotlin/MapWithMarker/app/src/main/java/com/example/mapwithmarker/MapsMarkerActivity.kt @@ -36,10 +36,7 @@ class MapsMarkerActivity : AppCompatActivity(), OnMapReadyCallback { super.onCreate(savedInstanceState) // Retrieve the content view that renders the map. setContentView(R.layout.activity_maps) - if (getString(R.string.maps_api_key).isEmpty()) { - Toast.makeText(this, "Add your own API key in MapWithMarker/app/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show() - } - + // Get the SupportMapFragment and request notification when the map is ready to be used. val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment mapFragment?.getMapAsync(this) diff --git a/tutorials/kotlin/Polygons/README.md b/tutorials/kotlin/Polygons/README.md index ccafe9a5..1a3af32c 100644 --- a/tutorials/kotlin/Polygons/README.md +++ b/tutorials/kotlin/Polygons/README.md @@ -24,12 +24,7 @@ This sample uses the Gradle build system. 1. If prompted for a gradle configuration, accept the default settings. Alternatively use the `gradlew build` command to build the project directly. -This demo app requires that you 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 root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key) - 1. Add a single line to `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 +This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions. Support ------- @@ -47,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues License ------- -Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo. +Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo. diff --git a/tutorials/kotlin/Polygons/app/build.gradle b/tutorials/kotlin/Polygons/app/build.gradle index 77c22eb2..596346c1 100644 --- a/tutorials/kotlin/Polygons/app/build.gradle +++ b/tutorials/kotlin/Polygons/app/build.gradle @@ -2,6 +2,15 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android' +// Set the properties within `local.properties` into a `Properties` class so +// that values within `local.properties` (e.g. Maps API key) are accessible in +// this file. +Properties properties = new Properties() +if (rootProject.file("local.properties").exists()) { + properties.load(rootProject.file("local.properties").newDataInputStream()) +} +def mapsApiKey = properties.getProperty("MAPS_API_KEY", "") + android { compileSdkVersion 29 buildToolsVersion '28.0.3' @@ -13,15 +22,11 @@ android { versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - // Read the API key from ./secure.properties into R.string.maps_api_key - def secureProps = new Properties() - if (file("../../../../secure.properties").exists()) { - file("../../../../secure.properties")?.withInputStream { secureProps.load(it) } - } - resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "") + // Read the API key from local.properties into R.string.maps_api_key + resValue "string", "maps_api_key", mapsApiKey // To add your Maps API key to this project: - // 1. Create a file ./secure.properties + // 1. Open the root project's local.properties file // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY }