Hrk Hrk - 2 years ago 293
Android Question

Manage Google Maps API Key with Gradle in Android Studio

I know Gradle is powerful and I would like to manage the API keys for development/produciton of Google Maps

Currently I always need to manually comment one line and uncomment the other to make it work. Is there a way to do it automatically in Gradle with some custom release configuration ?

<!-- MapView v2 API -->
<uses-library android:name="" />
<meta-data android:name="" android:value="[MY_DEV_KEY]" />
<!-- PROD
<meta-data android:name="" android:value="[MY_PROD_KEY]" />

Answer Source

Since you are using gradle you can do the following:


android {
  .. .. ...
    buildTypes {
       debug {
          resValue "string", "google_maps_api_key", "[YOUR DEV KEY]"
       release {
           resValue "string", "google_maps_api_key", "[YOUR PROD KEY]"

And in your AndroidManifest.xml


This way you only have one AndroidManifest.xml and you set value based on your build type. Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download