Skip to content

Commit 2ab19b7

Browse files
authored
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
1 parent bf82dc8 commit 2ab19b7

File tree

35 files changed

+218
-413
lines changed

35 files changed

+218
-413
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ build/
44
*.iml
55
local.properties
66
.DS_Store
7-
secure.properties

ApiDemos/java/README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ in the Java programming language.
77

88
They demonstrate most of the features available in the API.
99

10-
This app was written with a minSdk of 16 and the androidx appcompat library, but it can be easily
11-
adapted to use native functionality instead (for example replacing `SupportMapFragment` with
12-
`MapFragment`).
13-
14-
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.
10+
The Maps SDK for Android samples can be found under the `gms` gradle product
11+
flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle
12+
product flavor. The active product flavor can be modified through
13+
Android Studio’s “Build Variants” toolbar options.
1514

1615
Pre-requisites
1716
--------------
1817

19-
- Android SDK v29
18+
- Android SDK v30
2019
- Latest Android Build Tools
2120
- Android Support Repository
2221
- Google Repository
23-
- Google Play services
22+
- Google Play Services
2423

2524
Getting Started
2625
---------------
@@ -35,12 +34,7 @@ If prompted for a gradle configuration accept the default settings.
3534

3635
Alternatively use the `gradlew build` command to build the project directly.
3736

38-
This demo app requires that you add your own Google Maps API key:
39-
40-
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
41-
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
42-
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
43-
1. Build and run
37+
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.
4438

4539
Support
4640
-------
@@ -61,4 +55,4 @@ CONTRIBUTING.md.
6155
License
6256
-------
6357

64-
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
58+
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

ApiDemos/java/app/build.gradle

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import org.apache.tools.ant.filters.ConcatFilter
33
apply plugin: 'com.android.application'
44
apply plugin: 'project-report'
55

6+
// Set the properties within `local.properties` into a `Properties` class so
7+
// that values within `local.properties` (e.g. Maps API key) are accessible in
8+
// this file.
9+
Properties properties = new Properties()
10+
if (rootProject.file("local.properties").exists()) {
11+
properties.load(rootProject.file("local.properties").newDataInputStream())
12+
}
13+
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
14+
615
android {
716
compileSdkVersion 30
817

@@ -14,15 +23,16 @@ android {
1423
versionName "1.0"
1524
multiDexEnabled true
1625

17-
// Read the API key from ./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") ?: "")
26+
// Inject the Maps API key into the manifest
27+
manifestPlaceholders = [ mapsApiKey : mapsApiKey ]
28+
29+
// Reading the Maps API key into a BuildConfig value. This is only
30+
// necessary so that the sample app can display a toast message if the
31+
// API key was not set, otherwise, this can be skipped.
32+
buildConfigField("String", "MAPS_API_KEY", "\"$mapsApiKey\"" )
2333

2434
// To add your Maps API key to this project:
25-
// 1. Create a file ./secure.properties
35+
// 1. Open the root project's local.properties file
2636
// 2. Add this line, where YOUR_API_KEY is your API key:
2737
// MAPS_API_KEY=YOUR_API_KEY
2838
}

ApiDemos/java/app/src/gms/AndroidManifest.xml

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,13 @@
1717
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1818
package="com.example.mapdemo">
1919

20-
<!--
21-
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
22-
Google Maps Android API v2, but you must specify either coarse or fine
23-
location permissions for the 'MyLocation' functionality.
24-
-->
25-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
26-
27-
<!-- EXTERNAL_STORAGE permissions are optional for Android 6.0 onwards. -->
28-
<uses-permission
29-
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
30-
android:maxSdkVersion="22" />
31-
<uses-permission
32-
android:name="android.permission.READ_EXTERNAL_STORAGE"
33-
android:maxSdkVersion="22" />
34-
3520
<application
3621
android:allowBackup="true"
3722
android:icon="@mipmap/ic_launcher"
3823
android:label="@string/demo_title"
3924
android:supportsRtl="true"
4025
android:theme="@style/AppTheme">
4126

42-
<!--
43-
To add your Maps API key to this project:
44-
1. Create a file ApiDemos/java/app/secure.properties
45-
2. Add this line, where YOUR_API_KEY is your API key:
46-
MAPS_API_KEY=YOUR_API_KEY
47-
-->
48-
<meta-data
49-
android:name="com.google.android.geo.API_KEY"
50-
android:value="@string/maps_api_key" />
51-
5227
<activity android:name=".MainActivity">
5328
<intent-filter>
5429
<action android:name="android.intent.action.MAIN" />

ApiDemos/java/app/src/gms/java/com/example/mapdemo/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ protected void onCreate(Bundle savedInstanceState) {
8585
list.setOnItemClickListener(this);
8686
list.setEmptyView(findViewById(R.id.empty));
8787

88-
if (getString(R.string.maps_api_key).isEmpty()) {
89-
Toast.makeText(this, "Add your own API key in ApiDemos/java/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show();
88+
if (BuildConfig.MAPS_API_KEY.isEmpty()) {
89+
Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show();
9090
}
9191
}
9292

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

Lines changed: 10 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1818
package="com.example.mapdemo">
1919

20+
<!--
21+
Android 11 package visibility changes require that apps specify which
22+
set of other packages on the device that they can access. Since this
23+
sample uses Google Maps, specifying the Google Maps package name is
24+
required so that the buttons on the Map toolbar launch the Google Maps
25+
app.
26+
-->
2027
<queries>
2128
<package android:name="com.google.android.apps.maps" />
2229
</queries>
@@ -44,129 +51,14 @@
4451
android:theme="@style/AppTheme">
4552

4653
<!--
47-
To add your Maps API key to this project:
48-
1. Create a file ApiDemos/java/app/secure.properties
54+
To add your Maps API key to this project:
55+
1. Open the root project's local.properties file
4956
2. Add this line, where YOUR_API_KEY is your API key:
5057
MAPS_API_KEY=YOUR_API_KEY
5158
-->
5259
<meta-data
5360
android:name="com.google.android.geo.API_KEY"
54-
android:value="@string/maps_api_key" />
55-
56-
<activity android:name=".MainActivity">
57-
<intent-filter>
58-
<action android:name="android.intent.action.MAIN" />
59-
<category android:name="android.intent.category.LAUNCHER" />
60-
</intent-filter>
61-
</activity>
62-
<activity
63-
android:name=".BasicMapDemoActivity"
64-
android:label="@string/basic_map_demo_label" />
65-
<activity
66-
android:name=".CameraDemoActivity"
67-
android:label="@string/camera_demo_label" />
68-
<activity
69-
android:name=".CameraClampingDemoActivity"
70-
android:label="@string/camera_clamping_demo_label" />
71-
<activity
72-
android:name=".CircleDemoActivity"
73-
android:label="@string/circle_demo_label" />
74-
<activity
75-
android:name=".EventsDemoActivity"
76-
android:label="@string/events_demo_label" />
77-
<activity
78-
android:name=".GroundOverlayDemoActivity"
79-
android:label="@string/ground_overlay_demo_label" />
80-
<activity
81-
android:name=".IndoorDemoActivity"
82-
android:label="@string/indoor_demo_label" />
83-
<activity
84-
android:name=".LayersDemoActivity"
85-
android:label="@string/layers_demo_label" />
86-
<activity
87-
android:name=".LiteDemoActivity"
88-
android:label="@string/lite_demo_label" />
89-
<activity
90-
android:name=".LiteListDemoActivity"
91-
android:label="@string/lite_list_demo_label" />
92-
<activity
93-
android:name=".LocationSourceDemoActivity"
94-
android:label="@string/location_source_demo_label" />
95-
<activity
96-
android:name=".MapInPagerDemoActivity"
97-
android:label="@string/map_in_pager_demo_label" />
98-
<activity
99-
android:name=".MarkerDemoActivity"
100-
android:label="@string/marker_demo_label" />
101-
<activity
102-
android:name=".MarkerCloseInfoWindowOnRetapDemoActivity"
103-
android:label="@string/marker_close_info_window_on_retap_demo_label" />
104-
<activity
105-
android:name=".polyline.PolylineDemoActivity"
106-
android:label="@string/polyline_demo_label" />
107-
<activity
108-
android:name=".MultiMapDemoActivity"
109-
android:label="@string/multi_map_demo_label" />
110-
<activity
111-
android:name=".MyLocationDemoActivity"
112-
android:label="@string/my_location_demo_label" />
113-
<activity
114-
android:name=".OptionsDemoActivity"
115-
android:label="@string/options_demo_label" />
116-
<activity
117-
android:name=".PolygonDemoActivity"
118-
android:label="@string/polygon_demo_label" />
119-
<activity
120-
android:name=".ProgrammaticDemoActivity"
121-
android:label="@string/programmatic_demo_label" />
122-
<activity
123-
android:name=".RawMapViewDemoActivity"
124-
android:label="@string/raw_map_view_demo_label" />
125-
<activity
126-
android:name=".RetainMapDemoActivity"
127-
android:label="@string/retain_map_demo_label" />
128-
<activity
129-
android:name=".SaveStateDemoActivity"
130-
android:label="@string/save_state_demo_label" />
131-
<activity
132-
android:name=".SnapshotDemoActivity"
133-
android:label="@string/snapshot_demo_label" />
134-
<activity
135-
android:name=".SplitStreetViewPanoramaAndMapDemoActivity"
136-
android:label="@string/split_street_view_panorama_and_map_demo_label" />
137-
<activity
138-
android:name=".StreetViewPanoramaBasicDemoActivity"
139-
android:label="@string/street_view_panorama_basic_demo_label" />
140-
<activity
141-
android:name=".StreetViewPanoramaEventsDemoActivity"
142-
android:label="@string/street_view_panorama_events_demo_label" />
143-
<activity
144-
android:name=".StreetViewPanoramaNavigationDemoActivity"
145-
android:label="@string/street_view_panorama_navigation_demo_label" />
146-
<activity
147-
android:name=".StreetViewPanoramaOptionsDemoActivity"
148-
android:label="@string/street_view_panorama_options_demo_label" />
149-
<activity
150-
android:name=".StreetViewPanoramaViewDemoActivity"
151-
android:label="@string/street_view_panorama_view_demo_label" />
152-
<activity
153-
android:name=".StyledMapDemoActivity"
154-
android:label="@string/styled_map_demo_label" />
155-
<activity
156-
android:name=".TagsDemoActivity"
157-
android:label="@string/tags_demo_label" />
158-
<activity
159-
android:name=".TileCoordinateDemoActivity"
160-
android:label="@string/tile_coordinate_demo_label" />
161-
<activity
162-
android:name=".TileOverlayDemoActivity"
163-
android:label="@string/tile_overlay_demo_label" />
164-
<activity
165-
android:name=".UiSettingsDemoActivity"
166-
android:label="@string/ui_settings_demo_label" />
167-
<activity
168-
android:name=".VisibleRegionDemoActivity"
169-
android:label="@string/visible_region_demo_label" />
61+
android:value="${mapsApiKey}" />
17062

17163
</application>
17264

ApiDemos/java/app/src/v3/AndroidManifest.xml

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,13 @@
1717
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1818
package="com.example.mapdemo">
1919

20-
<!--
21-
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
22-
Google Maps Android API v2, but you must specify either coarse or fine
23-
location permissions for the 'MyLocation' functionality.
24-
-->
25-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
26-
27-
<!-- EXTERNAL_STORAGE permissions are optional for Android 6.0 onwards. -->
28-
<uses-permission
29-
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
30-
android:maxSdkVersion="22" />
31-
<uses-permission
32-
android:name="android.permission.READ_EXTERNAL_STORAGE"
33-
android:maxSdkVersion="22" />
34-
3520
<application
3621
android:allowBackup="true"
3722
android:icon="@mipmap/ic_launcher"
3823
android:label="@string/demo_title"
3924
android:supportsRtl="true"
4025
android:theme="@style/AppTheme">
4126

42-
<!--
43-
To add your Maps API key to this project:
44-
1. Create a file ApiDemos/java/app/secure.properties
45-
2. Add this line, where YOUR_API_KEY is your API key:
46-
MAPS_API_KEY=YOUR_API_KEY
47-
-->
48-
<meta-data
49-
android:name="com.google.android.geo.API_KEY"
50-
android:value="@string/maps_api_key" />
51-
5227
<activity android:name=".MainActivity">
5328
<intent-filter>
5429
<action android:name="android.intent.action.MAIN" />

ApiDemos/java/app/src/v3/java/com/example/mapdemo/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ protected void onCreate(Bundle savedInstanceState) {
8585
list.setOnItemClickListener(this);
8686
list.setEmptyView(findViewById(R.id.empty));
8787

88-
if (getString(R.string.maps_api_key).isEmpty()) {
89-
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();
88+
if (BuildConfig.MAPS_API_KEY.isEmpty()) {
89+
Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show();
9090
}
9191
}
9292

ApiDemos/java/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
# org.gradle.parallel=true
1919
android.enableJetifier=true
2020
android.useAndroidX=true
21+
GOOGLE_MAPS_API_KEY="AIzaSyBri0nHwJxuIs8KgdsGXdJLFvg1vviUMfo"

0 commit comments

Comments
 (0)