Secrets Gradle plugin

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:

  1. In Android Studio, open your project-level build.gradle file and add the following code to the dependencies element under buildscript.

    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")
        }
    }
    
  2. Open your module-level build.gradle file and add the following code to the plugins element.

    Groovy

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. Save the file and sync your project with Gradle.
  4. Open the secrets.properties in your project level directory, and then add the following code. Replace YOUR_API_KEY with your API key.
    MAPS_API_KEY=YOUR_API_KEY 
  5. Save the file.
  6. In your AndroidManifest.xml file, go to com.google.android.geo.API_KEY and update the android:value attribute as follows:
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="${MAPS_API_KEY}" />
  7. Optionally edit the properties of the plugin to specify a different secrets file or other properties. In Android Studio, open your project-level build.gradle file and edit the secrets property:

    secrets {
        // Optionally specify a different file name containing your secrets.
        // The plugin defaults to "local.properties"
        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