Giorgio Barchiesi
Giorgio Barchiesi

Reputation: 6177

Android Google Maps v2 - Debug key vs Release key

it is clear to me how to get a debug key for use with Google Maps v2 library, and also how to get a release key. Currently the relevant section of my manifest file looks like this:

<!-- Debug -->
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="[my debug key here]"/>

<!-- Release        
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="[my release key here]"/>
-->

The relevant key is uncommented, the other one is commented.

Can anyone indicate a comfortable way to avoid this annoyance of commenting/uncommenting these pieces of manifest file everytime a debug rather than release version is needed?

Upvotes: 16

Views: 13285

Answers (3)

Akronix
Akronix

Reputation: 2169

Alternatively, you can place your debug key in app/src/debug/res/values/google_maps_api.xml with a content similar to this:

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx</string>

In the same way, place the release key in app/src/release/res/values/google_maps_api.xml.

In this manner you have both keys and same source code. This is very convenient for open source projects where you want to publish your source code but not your API keys. You just need to ignore / not upload the google_maps_api.xml file and you're good to go.

Upvotes: 3

Cimlman
Cimlman

Reputation: 3824

Different Google Map API keys for debug build and release build can be defined in build.gradle:

...
android {
    ...
    buildTypes {
       debug {
           resValue "string", "google_maps_api_key", "<debug_key>"
           ...
       }
       release {
           resValue "string", "google_maps_api_key", "<release_key>"
           ...
       }
    }
}

Just replace <debug_key> and <release_key> with your actual keys.

And refer to this resource value in AndroidManifest.xml:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/google_maps_api_key"/>

This solution is also described in the following Stack Overflow question:

Manage Google Maps API Key with Gradle in Android Studio

Upvotes: 5

Michal Palczewski
Michal Palczewski

Reputation: 918

With version 2 API's you can use the same key for release and debug. In your google api's console edit your allowed android apps and on each line put your debug/release key, and then your app name. You can use multiple lines, then it will work with both keys.

Upvotes: 37

Related Questions