Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using Google Maps v2 API in my project. In Google Maps v2 the debug/release API key is defined in AndroidManifest.xml. I have seen the link but in that map key is defined in a xml layout file not in AndroidManifest.xml. So can I define both debug and release keys for my project in AndroidManifest.xml?

I want 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"/> 
like image 252
Kirit Vaghela Avatar asked Jun 17 '13 05:06

Kirit Vaghela


People also ask

Can I get Google Maps API key without billing?

Yes you need to setup a billing account, there is no way around it these days.

Where do I put Map API key Android?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key. Click Close.


2 Answers

Using build.gradle

buildTypes {      debug {         buildConfigField("String", "map_api_key", "\"your debug map api key here\"")     }     release {         buildConfigField("String", "map_api_key", "\"your release map api key here\"")     } } 

I solved this issue using this steps:

In Google Developer API Console

  1. Click on Create New Android key...
  2. In cmd.exe/Terminal: keytool -list -v -keystore mystore.keystore
  3. Password: android
  4. Now enter SHA1 key;package name for debug and press enter
  5. Enter SHA1 key;package name for release
  6. Click on Create

Now use this API key your project

<meta-data     android:name="com.google.android.maps.v2.API_KEY"     android:value="@string/common_map_api_key"/> 
like image 80
Kirit Vaghela Avatar answered Oct 02 '22 17:10

Kirit Vaghela


One of the Best way to use build.gradle file in latest Android 5.0 and Android 6.0, Android 9+ (API 20, 21,22,23, 24, 25,26, 27 28, 29)

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

  • Add custom value to manifestplaceholders build.gradle file.

See below

buildTypes {     debug {         manifestPlaceholders = [ mapApiKeyValue:"GHjaSyAjlyp3O831lgaonHMXsd-_DpQ3002x3S4"]     }      release {         manifestPlaceholders = [ mapApiKeyValue:"AIzaSyAuMGDLr2HeuRed4JA0CrdYYdZRjeC3EA"]     } } 
  • Edit manifest file like 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)


Updated on 5/3/2018 for Xamarin Form and Xamarin Native Apps

Open AssemblyInfo.cs in Android Project and addd the following code

#if DEBUG    [assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "DebugKey123123123")] #else    [assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "ReleaseKey123123123")] #endif 

To Check the AndroidManifest file, goto obj/Debug/android folder and open the manifest file and check the meta info,

<meta-data         android:name="com.google.android.maps.v2.API_KEY"         android:value="DebugKey123123123" /> 
like image 36
Zumry Mohamed Avatar answered Oct 02 '22 17:10

Zumry Mohamed