[Video](https://www.youtube.com/watch?v=X8lYNW_Or2o)

Google strongly recommends that you don't check an [API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key.md.txt) into
your version control system. Instead, 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](https://github.com/google/secrets-gradle-plugin)
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](https://developers.google.com/maps/documentation/android-sdk/config.md.txt).

## Installation and usage

To install the Secrets Gradle Plugin for Android and store your API key:

1. In Android Studio, open your top-level `build.gradle.kts` or `build.gradle` file and add the following code to the `plugins` block.

   ### Kotlin

   ```kotlin
   plugins {
       // ...
       id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.1" apply false
   }
   ```

   ### Groovy

   ```groovy
   plugins {
       // ...
       id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
   }
   ```
2. Open your module-level `build.gradle.kts` or `build.gradle` file and add the following code to the `plugins` element.

   ### Kotlin

   ```kotlin
   plugins {
       id("com.android.application")
       // ...
       id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
   }
   ```

   ### Groovy

   ```groovy
   plugins {
       id 'com.android.application'
       // ...
       id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
   }
   ```
3. If you use Android Studio, [sync your
   project with Gradle](https://developer.android.com/studio/build#sync-files).
4. Open the `secrets.properties` file in your top-level directory, and then add the following code. Replace `YOUR_API_KEY` with your API key. Make sure to add `secrets.properties` to your `.gitignore` file (or otherwise exclude it from your version control system) so that you don't check your API key into version control.

   > [!NOTE]
   > **Note:** If the `secrets.properties` file doesn't exist, create it in the same folder as the `local.properties` file.

   ```
   MAPS_API_KEY=YOUR_API_KEY
   ```
5. Create the `local.defaults.properties` file in your top-level
   directory, the same folder as the `secrets.properties` file, and
   then add the following code.

   > [!NOTE]
   > **Note:** Enter the code as shown. Don't replace `DEFAULT_API_KEY` with your API key.

   ```
   MAPS_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 isn't found so that builds
   don't fail. This can happen if you clone the app from a version control
   system that omits `secrets.properties` and you haven't yet
   created a `secrets.properties` file locally to provide your API
   key.
6. In your `AndroidManifest.xml` file, go to `com.google.android.geo.API_KEY` and update the `android:value` attribute as follows:

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

   **Note:**
   `com.google.android.geo.API_KEY` is the recommended metadata name
   for the API key. A key with this name can be used to authenticate to multiple
   Google Maps-based APIs on the Android platform, including the
   Maps SDK for Android. For backwards compatibility, the API also
   supports the name `com.google.android.maps.v2.API_KEY`. This legacy
   name allows authentication to the Android Maps API v2 only. An application can
   specify only one of the API key metadata names. If both are specified, the API
   throws an exception.
7. In Android Studio, open your module-level `build.gradle.kts`
   or `build.gradle` file and edit the `secrets`
   property. If the `secrets` property doesn't exist, add it.

   Edit the properties of the plugin to set `propertiesFileName`
   to `"secrets.properties"`, set
   `defaultPropertiesFileName` to
   `"local.defaults.properties"`, and set any other properties.

   ### Kotlin

   ```kotlin
   secrets {
       // To add your Maps API key to this project:
       // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root 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"
   }
   ```

   ### Groovy

   ```groovy
   secrets {
       // To add your Maps API key to this project:
       // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root 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"
   }
   ```

## What's next

- See the [Secrets Gradle Plugin for Android](https://github.com/google/secrets-gradle-plugin) GitHub project page.
- See [Set up an Android Studio project](https://developers.google.com/maps/documentation/android-sdk/config.md.txt) for a complete example of using the plugin.