16

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?

Giorgio Barchiesi
  • 6,109
  • 3
  • 32
  • 36
  • The new Gradle-based build system should help in this area. You should be able to define debug vs. production string resources, and hold your API keys in one of those. – CommonsWare May 05 '13 at 13:54
  • Please, can you provide me steps to achieve google maps working on release mode? I have tried lots of things, but I couldn't figure out how to solve this. – guisantogui Oct 07 '15 at 10:28
  • The answer was given by Michal: it is possible to specify multiple fingerprints + package names, for the same key. Google documentation seems very accurate to me: https://developers.google.com/maps/documentation/android-api/signup – Giorgio Barchiesi Oct 10 '15 at 16:26

3 Answers3

37

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.

  • 1
    Great! In fact I hadn't noticed this little indication in the APi console: One SHA1 certificate fingerprint and package name (separated by a semicolon) per line. Thanks Michal. – Giorgio Barchiesi May 05 '13 at 15:47
  • It is still recommended to use different API Keys per release type, isn't it? I though it was about security, not about convenience. – diego nunes Mar 22 '15 at 19:34
  • If that is the case, what is the security risk? I tried to come up with scenarios, but could only think of if the api key is abused with the debug apk, thereby affecting the production app...but can't see how that would happen. – androidguy Jan 03 '20 at 01:37
5

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

Community
  • 1
  • 1
Cimlman
  • 3,404
  • 5
  • 25
  • 35
  • Manifest values can't be kept in string resources, if you have localization to several languages. Studio won't allow to generate release build. – Valeriya Nov 25 '16 at 08:00
3

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.

Akronix
  • 1,858
  • 2
  • 19
  • 32