Android: automatically selects debug / release Maps v2 api key?

I am using the Google Maps API v2 in my project. In Google Maps v2, the debug / release API key is defined in AndroidManifest.xml . I saw the link , but in this key the maps are defined in the xml layout file not in AndroidManifest.xml . Can I define debug and release keys for my project in AndroidManifest.xml ?

I need something like this in AndroidManifest.xml :

If debug mode:

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

If release mode:

 <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/release_map_api_key"/> 
+37
android android-manifest google-maps key google-maps-android-api-2
Jun 17 '13 at 5:40
source share
4 answers

I solved this problem using the following steps:

In the Google Developer API Console

  • Click Create New Android key ...
  • In cmd.exe / Terminal: keytool -list -v -keystore mystore.keystore
  • Password: android
  • Now enter SHA1 key;package name for debugging and press enter
  • Enter SHA1 key;package name to release
  • Click Create

Now use this API key of your project

 <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/common_map_api_key"/> 
+65
Jun 17 '13 at 6:35
source share

One of the best ways to use the build.gradle file in recent versions of Android 5.0 and Android 6.0 (API 20, 21,22,23)

Well, you can use them simply without creating product flavors in gradle . This is another example that we can achieve with gradle . You can achieve this in two simple steps.

  • Add a custom value to the manifestplaceholders build.gradle file.

See below

 buildTypes { debug { manifestPlaceholders = [ mapApiKeyValue:"GHjaSyAjlyp3O831lgaonHMXsd-_DpQ3002x3S4"] } release { manifestPlaceholders = [ mapApiKeyValue:"AIzaSyAuMGDLr2HeuRed4JA0CrdYYdZRjeC3EA"] } } 
  • Edit the manifest file as shown below.

part of my manifest file

  <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="${mapApiKeyValue}" /> 

This solution works for the latest Android 5.0 and Android 6.0 (API 20, 21,22,23)

+16
Sep 01 '16 at 7:49
source share

For organizations that need to support separate keys, you can put them in separate directories in Android Studio. Make sure the src subdirectory you use matches your taste or buildType

From Building a project using Gradle :

 To build each version of your app, the build system combines source code and resources from: src/main/ - the main source directory (common to all variants) src/<buildType>/ - the build type source directory src/<flavorName>/ - the flavor source directory 

In projectroot/yourapp/build.gradle :

 buildTypes { debug { runProguard false debuggable true } release { runProguard true debuggable false ... } 

In projectroot/yourapp/src/main/AndroidManifest.xml :

 ... <application android:name=".MyApplication" android:theme="@style/Theme"> <!-- Don't put your key here --> ... 

In projectroot/yourapp/src/debug/AndroidManifest.xml , fully qualify the application name.

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:name="com.hipmunk.android.HipmunkApplication"> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="yourdebugkey" /> </application> </manifest> 

In projectroot/yourapp/src/release/AndroidManifest.xml :

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:name="com.hipmunk.android.HipmunkApplication"> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="release key" /> </application> </manifest> 
+14
Aug 12 '14 at 18:00
source share

Since you are using gradle, you can do the following:

build.gradle

 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

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

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

0
Jun 22 '17 at 11:46 on
source share



All Articles