Skip to content

Commit

Permalink
fix: stop forcing users to supply api key via local.properties on And…
Browse files Browse the repository at this point in the history
…roid (#133)
  • Loading branch information
ArturoSalazarB16 authored Apr 24, 2024
1 parent 979548d commit ad04adc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,31 @@ android {
}
```

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.
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.

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

### iOS

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

`platform: ios, '14.0'`
```objective-c
@implementation AppDelegate

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

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.
```
[!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:
```objective-c
[GMSServices setMetalRendererEnabled:YES];
```

## Usage

Expand Down
21 changes: 18 additions & 3 deletions SampleApp/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
plugins {
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.5"
}

apply plugin: "com.android.application"
apply plugin: "com.facebook.react"
apply plugin: 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'

/**
* This is the configuration block to customize your React Native Android app.
Expand Down Expand Up @@ -136,4 +135,20 @@ dependencies {
}
}

secrets {
// This example application employs the Gradle plugin
// com.google.android.libraries.mapsplatform.secrets-gradle-plugin
// to securely manage the Google Maps API key.
// For more information on the plugin, visit:
// https://developers.google.com/maps/documentation/android-sdk/secrets-gradle-plugin
propertiesFileName = "local.properties"

// For CI/CD, you can have a file with default keys that can be
// safely checked in to your source code version control.
// defaultPropertiesFileName = 'local.defaults.properties'

// Ignore all keys matching the regexp "sdk.*"
ignoreList.add("sdk.*")
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
4 changes: 4 additions & 0 deletions SampleApp/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />
</application>
</manifest>
8 changes: 8 additions & 0 deletions SampleApp/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ buildscript {
dependencies {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
}
}

allprojects {
repositories {
google()
mavenCentral()
}
}
10 changes: 0 additions & 10 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

import groovy.json.JsonSlurper

def getApiKey(){
def Properties props = new Properties()
props.load(new FileInputStream(new File('local.properties')))
return props['MAPS_API_KEY']
}

buildscript {
repositories {
google()
Expand All @@ -32,9 +26,6 @@ buildscript {
}
}

plugins {
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.5"
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

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

lintOptions {
Expand Down
4 changes: 0 additions & 4 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
<service
android:name="com.google.android.react.navsdk.NavInfoReceivingService"
android:exported="false" />

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />
</application>

</manifest>

0 comments on commit ad04adc

Please sign in to comment.