Secrets Gradle plugin

  • Securely manage your Google Maps API key using the Secrets Gradle Plugin for Android, avoiding storage in version control.

  • The plugin reads your API key from a local secrets.properties file and makes it accessible in your BuildConfig and manifest.

  • Install the plugin by adding it as a dependency and applying it in your Gradle files, then configure it to locate your API key.

  • Utilize the local.defaults.properties file to provide a fallback API key for builds and prevent failures when the secrets.properties file is missing.

  • Refer to the plugin's GitHub page and the setup guide for detailed information and a comprehensive example.

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:

  1. In Android Studio, open your root-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 app-level build.gradle file and add the following code to the plugins element.

    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")
    }
  3. If you use Android Studio, sync your project with Gradle.
  4. Open the local.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. 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}" />
        

    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.

What's next