Google strongly recommends that you not check an API key into your
version control system. Instead, you should store it in a local secrets.properties
file,
which is located in the root directory of your project but excluded from version control, and then
use the Secrets Gradle Plugin for Android
to read the API key.
The Secrets Gradle Plugin for Android reads secrets, including the API key, from
a properties file not checked into a version control system. The plugin then exposes those properties
as variables in the Gradle-generated BuildConfig
class and in the Android manifest file.
For a complete example of using the Secrets Gradle Plugin for Android to access an API key, see Set up an Android Studio project.
Installation and usage
To install the Secrets Gradle Plugin for Android in your Google Maps project:
-
In Android Studio, open your top-level
build.gradle.kts
orbuild.gradle
file and add the following code to thedependencies
element underbuildscript
.Kotlin
buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
Groovy
buildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } }
-
Open your module-level
build.gradle.kts
orbuild.gradle
file and add the following code to theplugins
element.Kotlin
plugins { // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
- In your module-level
build.gradle.kts
orbuild.gradle
file, ensure thattargetSdk
andcompileSdk
are set to 34. - Save the file and sync your project with Gradle.
-
Open the
secrets.properties
file in your top-level directory, and then add the following code. ReplaceYOUR_API_KEY
with your API key. Store your key in this file becausesecrets.properties
is excluded from being checked into a version control system.PLACES_API_KEY=YOUR_API_KEY
- Save the file.
-
Create the
local.defaults.properties
file in your top-level directory, the same folder as thesecrets.properties
file, and then add the following code.PLACES_API_KEY=DEFAULT_API_KEY
The purpose of this file is to provide a backup location for the API key if the
secrets.properties
file is not found so that builds don't fail. This can happen if you clone the app from a version control system which omitssecrets.properties
and you have not yet created asecrets.properties
file locally to provide your API key. - Save the file.
-
In Android Studio, open your module-level
build.gradle.kts
orbuild.gradle
file and edit thesecrets
property. If thesecrets
property does not exist, add it.Edit the properties of the plugin to set
propertiesFileName
tosecrets.properties
, setdefaultPropertiesFileName
tolocal.defaults.properties
, and set any other properties.Kotlin
secrets { // To add your Maps API key to this project: // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file. // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*" }
Groovy
secrets { // To add your Maps API key to this project: // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file. // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*" }
What's next
- View the Secrets Gradle Plugin for Android GitHub project page.
- View Set up an Android Studio project for a complete example of using the plugin.