Skip to content

Commit 63a744d

Browse files
committed
chore(Snippet): Add region tags for API key security.
1 parent 2ab19b7 commit 63a744d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

snippets/app/build.gradle

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@ apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
44

5+
// [START maps_android_api_key_security_manifest_placeholder]
56
android {
6-
compileSdkVersion 29
7+
// [START_EXCLUDE silent]
8+
compileSdkVersion 30
79
buildToolsVersion "29.0.3"
8-
10+
// [END_EXCLUDE]
911
defaultConfig {
12+
// [START_EXCLUDE]
1013
applicationId "com.google.maps.example"
1114
minSdkVersion 21
12-
targetSdkVersion 29
15+
targetSdkVersion 30
1316
versionCode 1
1417
versionName "1.0"
15-
1618
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17-
}
19+
// [END_EXCLUDE]
20+
// Set the properties within `local.properties` into a `Properties` class so that values
21+
// within `local.properties` (e.g. Maps API key) are accessible in this file.
22+
Properties properties = new Properties()
23+
if (rootProject.file("local.properties").exists()) {
24+
properties.load(rootProject.file("local.properties").newDataInputStream())
25+
}
1826

27+
// Inject the Maps API key into the manifest
28+
manifestPlaceholders = [ mapsApiKey : properties.getProperty("MAPS_API_KEY", "") ]
29+
}
30+
// [START_EXCLUDE silent]
1931
buildTypes {
2032
release {
2133
minifyEnabled false
@@ -41,7 +53,9 @@ android {
4153
sourceCompatibility JavaVersion.VERSION_1_8
4254
targetCompatibility JavaVersion.VERSION_1_8
4355
}
56+
// [END_EXCLUDE]
4457
}
58+
// [END maps_android_api_key_security_manifest_placeholder]
4559

4660
dependencies {
4761
implementation fileTree(dir: "libs", include: ["*.jar"])

snippets/app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
android:label="@string/app_name"
2424
android:roundIcon="@mipmap/ic_launcher_round"
2525
android:supportsRtl="true"
26-
android:theme="@style/AppTheme" />
26+
android:theme="@style/AppTheme" >
27+
28+
<!-- [START maps_android_api_key_security_manifest_metadata] -->
29+
<meta-data
30+
android:name="com.google.android.geo.API_KEY"
31+
android:value="${mapsApiKey}" />
32+
<!-- [END maps_android_api_key_security_manifest_metadata] -->
33+
</application>
2734

2835
</manifest>

0 commit comments

Comments
 (0)