Stay organized with collectionsSave and categorize content based on your preferences.
Google strongly recommends that you not check anAPI keyinto your
version control system. Instead, you should store it in a localsecrets.propertiesfile,
which is located in the root directory of your project but excluded from version control, and then
use theSecrets Gradle Plugin for Androidto 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-generatedBuildConfigclass and in the Android manifest file.
For a complete example of using the Secrets Gradle Plugin for Android to access an API key,
seeSet 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-levelbuild.gradle.ktsorbuild.gradlefile and add the following code to thedependencieselement underbuildscript.
Open thesecrets.propertiesfile in your top-level directory, and then add the
following code. ReplaceYOUR_API_KEYwith your API key. Store your key in this file
becausesecrets.propertiesis excluded from being checked into a version control
system.
PLACES_API_KEY=YOUR_API_KEY
Create thelocal.defaults.propertiesfile in your top-level directory, the same
folder as thesecrets.propertiesfile, 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 thesecrets.propertiesfile is not found so that builds don't fail. This can happen if
you clone the app from a version control system which omitssecrets.propertiesand
you have not yet created asecrets.propertiesfile locally to provide your
API key.
In Android Studio, open your module-levelbuild.gradle.ktsorbuild.gradlefile and edit thesecretsproperty. If thesecretsproperty does not exist, add it.
Edit the properties of the plugin to setpropertiesFileNametosecrets.properties, setdefaultPropertiesFileNametolocal.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_KEYpropertiesFileName="secrets.properties"// A properties file containing default secret values. This file can be// checked in version control.defaultPropertiesFileName="local.defaults.properties"}
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_KEYpropertiesFileName="secrets.properties"// A properties file containing default secret values. This file can be// checked in version control.defaultPropertiesFileName="local.defaults.properties"}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-03 UTC."],[[["\u003cp\u003eSecurely store your API keys outside of version control using the Secrets Gradle Plugin for Android.\u003c/p\u003e\n"],["\u003cp\u003eThe plugin reads API keys from a local \u003ccode\u003esecrets.properties\u003c/code\u003e file and makes them accessible within your Android project.\u003c/p\u003e\n"],["\u003cp\u003eYou need to install the plugin, configure it in your Gradle files, and create the necessary properties files (\u003ccode\u003esecrets.properties\u003c/code\u003e and \u003ccode\u003elocal.defaults.properties\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eThis approach ensures your sensitive API keys are not exposed in your code repository, enhancing security.\u003c/p\u003e\n"]]],["To protect API keys, store them in a `secrets.properties` file, excluded from version control. Install the Secrets Gradle Plugin by adding it to the `build.gradle` files. In module-level `build.gradle`, configure the plugin to read from `secrets.properties` and set `local.defaults.properties` as backup. Add your API key to `secrets.properties` and use the `DEFAULT_API_KEY` in the backup file. Sync the project to apply changes. This plugin exposes keys as variables.\n"],null,["# Secrets Gradle plugin\n\nGoogle strongly recommends that you not check an [API key](/maps/documentation/places/android-sdk/get-api-key) into your\nversion control system. Instead, you should store it in a local `secrets.properties` file,\nwhich is located in the root directory of your project but excluded from version control, and then\nuse the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin)\nto read the API key.\n\nThe Secrets Gradle Plugin for Android reads secrets, including the API key, from\na properties file not checked into a version control system. The plugin then exposes those properties\nas variables in the Gradle-generated `BuildConfig` class and in the Android manifest file.\n\nFor a complete example of using the Secrets Gradle Plugin for Android to access an API key,\nsee [Set up an Android Studio project](/maps/documentation/places/android-sdk/config).\n\n\nInstallation and usage\n----------------------\n\n| **Note:** See the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin) documentation on GitHub for the latest system requirements and installation instructions.\n\nTo install the Secrets Gradle Plugin for Android in your Google Maps project:\n\n1. In Android Studio, open your top-level `build.gradle.kts` or `build.gradle` file and add the following code to the `dependencies` element under `buildscript`. \n\n ### Kotlin\n\n ```yaml\n buildscript {\n dependencies {\n classpath(\"com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1\")\n }\n }\n ```\n\n ### Groovy\n\n ```yaml\n buildscript {\n dependencies {\n classpath \"com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1\"\n }\n }\n ```\n2. Open your module-level `build.gradle.kts` or `build.gradle` file and add the following code to the `plugins` element. \n\n ### Kotlin\n\n ```kotlin\n plugins {\n // ...\n id(\"com.google.android.libraries.mapsplatform.secrets-gradle-plugin\")\n }\n ```\n\n ### Groovy\n\n ```yaml\n plugins {\n // ...\n id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'\n }\n ```\n3. In your module-level `build.gradle.kts` or `build.gradle` file, ensure that `targetSdk` and `compileSdk` are set to 34.\n4. [Sync your project with Gradle](https://developer.android.com/studio/build#sync-files).\n 5. Open the `secrets.properties` file in your top-level directory, and then add the following code. Replace `YOUR_API_KEY` with your API key. Store your key in this file because `secrets.properties` is excluded from being checked into a version control system. **Note:** If the `secrets.properties` file does not exist, create it in the same folder as the `local.properties` file. \n\n ```scdoc\n PLACES_API_KEY=YOUR_API_KEY\n ```\n6. Create the `local.defaults.properties` file in your top-level directory, the same\n folder as the `secrets.properties` file, and then add the following code.\n\n **Note:** Enter the code as shown. Don't replace `DEFAULT_API_KEY` with your API key. \n\n ```scdoc\n PLACES_API_KEY=DEFAULT_API_KEY\n ```\n\n The purpose of this file is to provide a backup location for the API key if the\n `secrets.properties` file is not found so that builds don't fail. This can happen if\n you clone the app from a version control system which omits `secrets.properties` and\n you have not yet created a `secrets.properties` file locally to provide your\n API key.\n7. In Android Studio, open your module-level `build.gradle.kts` or\n `build.gradle` file and edit the `secrets` property. If the\n `secrets` property does not exist, add it.\n\n Edit the properties of the plugin to set `propertiesFileName` to\n `secrets.properties`, set `defaultPropertiesFileName` to\n `local.defaults.properties`, and set any other properties. \n\n ### Kotlin\n\n ```kotlin\n secrets {\n // To add your Maps API key to this project:\n // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.\n // 2. Add this line, where YOUR_API_KEY is your API key:\n // MAPS_API_KEY=YOUR_API_KEY\n propertiesFileName = \"secrets.properties\"\n\n // A properties file containing default secret values. This file can be\n // checked in version control.\n defaultPropertiesFileName = \"local.defaults.properties\"\n }\n \n ```\n\n ### Groovy\n\n ```groovy\n secrets {\n // To add your Maps API key to this project:\n // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file.\n // 2. Add this line, where YOUR_API_KEY is your API key:\n // MAPS_API_KEY=YOUR_API_KEY\n propertiesFileName = \"secrets.properties\"\n\n // A properties file containing default secret values. This file can be\n // checked in version control.\n defaultPropertiesFileName = \"local.defaults.properties\"\n }\n \n ```\n\nWhat's next\n-----------\n\n- View the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin) GitHub project page.\n- View [Set up an Android Studio project](/maps/documentation/places/android-sdk/config) for a complete example of using the plugin."]]