Page Summary
-
Securely store your API keys outside of version control using the Secrets Gradle Plugin for Android.
-
The plugin reads API keys from a local
secrets.propertiesfile and makes them accessible within your Android project. -
You need to install the plugin, configure it in your Gradle files, and create the necessary properties files (
secrets.propertiesandlocal.defaults.properties). -
This approach ensures your sensitive API keys are not exposed in your code repository, enhancing security.
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 and store your API key:
-
In Android Studio, open your root-level
build.gradlefile and add the following code to thedependencieselement underbuildscript.Groovy
buildscript { dependencies { // ... classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } }
Kotlin
buildscript { dependencies { // ... classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
-
Open your app-level
build.gradlefile and add the following code to thepluginselement.Groovy
plugins { id 'com.android.application' // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.android.application") // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- If you use Android Studio, sync your project with Gradle.
-
Open the
local.propertiesin your project level directory, and then add the following code. ReplaceYOUR_API_KEYwith your API key.PLACES_API_KEY=YOUR_API_KEY
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.