Skip to content

Commit ad04adc

Browse files
fix: stop forcing users to supply api key via local.properties on Android (#133)
1 parent 979548d commit ad04adc

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,31 @@ android {
5656
}
5757
```
5858

59-
1. To securely store your API key, it is recommended to use the [Google Maps Secrets Gradle Plugin](https://developers.google.com/maps/documentation/android-sdk/secrets-gradle-plugin). This plugin helps manage API keys without exposing them in your app's source code.
59+
To securely store your API key, it is recommended to use the [Google Maps Secrets Gradle Plugin](https://developers.google.com/maps/documentation/android-sdk/secrets-gradle-plugin). This plugin helps manage API keys without exposing them in your app's source code.
6060

6161
See example configuration for secrets plugin at example applications [build.gradle](./SampleApp/android/app/build.gradle) file.
6262

6363
### iOS
6464

65-
1. Set the iOS version in your application PodFile.
65+
To set up, specify your API key in the application delegate `ios/Runner/AppDelegate.m`:
6666

67-
`platform: ios, '14.0'`
67+
```objective-c
68+
@implementation AppDelegate
6869

69-
1. Make sure to run `pod install` from your application `ios` module.
70+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
71+
{
72+
[GMSServices provideAPIKey:@"API_KEY"];
73+
[GMSServices setMetalRendererEnabled:YES];
74+
return [super application:application didFinishLaunchingWithOptions:launchOptions];
75+
}
7076

71-
1. To set up, store your API key in the application plist file ([example](./SampleApp/ios/SampleApp/Info.plist), the key is defined under the API_KEY value). Then you need to update your [AppDelegate](./SampleApp/ios/SampleApp/AppDelegate.mm) file so the key can be read.
77+
```
7278
79+
[!NOTE]Above code snipped also enables Metal rendering for Google Maps SDK. If you are not using Metal rendering, you can remove the following line:
80+
81+
```objective-c
82+
[GMSServices setMetalRendererEnabled:YES];
83+
```
7384

7485
## Usage
7586

SampleApp/android/app/build.gradle

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
plugins {
15-
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.5"
16-
}
14+
1715
apply plugin: "com.android.application"
1816
apply plugin: "com.facebook.react"
17+
apply plugin: 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
1918

2019
/**
2120
* This is the configuration block to customize your React Native Android app.
@@ -136,4 +135,20 @@ dependencies {
136135
}
137136
}
138137

138+
secrets {
139+
// This example application employs the Gradle plugin
140+
// com.google.android.libraries.mapsplatform.secrets-gradle-plugin
141+
// to securely manage the Google Maps API key.
142+
// For more information on the plugin, visit:
143+
// https://developers.google.com/maps/documentation/android-sdk/secrets-gradle-plugin
144+
propertiesFileName = "local.properties"
145+
146+
// For CI/CD, you can have a file with default keys that can be
147+
// safely checked in to your source code version control.
148+
// defaultPropertiesFileName = 'local.defaults.properties'
149+
150+
// Ignore all keys matching the regexp "sdk.*"
151+
ignoreList.add("sdk.*")
152+
}
153+
139154
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

SampleApp/android/app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@
3737
<category android:name="android.intent.category.LAUNCHER" />
3838
</intent-filter>
3939
</activity>
40+
41+
<meta-data
42+
android:name="com.google.android.geo.API_KEY"
43+
android:value="${MAPS_API_KEY}" />
4044
</application>
4145
</manifest>

SampleApp/android/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,13 @@ buildscript {
3131
dependencies {
3232
classpath("com.android.tools.build:gradle")
3333
classpath("com.facebook.react:react-native-gradle-plugin")
34+
classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
35+
}
36+
}
37+
38+
allprojects {
39+
repositories {
40+
google()
41+
mavenCentral()
3442
}
3543
}

android/build.gradle

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414

1515
import groovy.json.JsonSlurper
1616

17-
def getApiKey(){
18-
def Properties props = new Properties()
19-
props.load(new FileInputStream(new File('local.properties')))
20-
return props['MAPS_API_KEY']
21-
}
22-
2317
buildscript {
2418
repositories {
2519
google()
@@ -32,9 +26,6 @@ buildscript {
3226
}
3327
}
3428

35-
plugins {
36-
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.5"
37-
}
3829
apply plugin: 'com.android.library'
3930
apply plugin: 'kotlin-android'
4031

@@ -53,7 +44,6 @@ android {
5344
versionCode 1
5445
// get version name from package.json version
5546
versionName "1.0"
56-
manifestPlaceholders = [ MAPS_API_KEY:getApiKey()]
5747
}
5848

5949
lintOptions {

android/src/main/AndroidManifest.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
<service
2525
android:name="com.google.android.react.navsdk.NavInfoReceivingService"
2626
android:exported="false" />
27-
28-
<meta-data
29-
android:name="com.google.android.geo.API_KEY"
30-
android:value="${MAPS_API_KEY}" />
3127
</application>
3228

3329
</manifest>

0 commit comments

Comments
 (0)