Secrets Gradle plugin

  • 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.properties file 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.properties and local.defaults.properties).

  • This approach ensures your sensitive API keys are not exposed in your code repository, enhancing security.

Google strongly recommends that you don't check an API key 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 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:

  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

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

    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

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

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

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

    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

    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