Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 308
Giorgio Barchiesi Avatar asked May 05 '13 13:05

Giorgio Barchiesi


3 Answers

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.

like image 140
Michal Palczewski Avatar answered Oct 10 '22 23:10

Michal Palczewski


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

like image 34
Cimlman Avatar answered Oct 10 '22 22:10

Cimlman


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.

like image 20
Akronix Avatar answered Oct 10 '22 22:10

Akronix