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"/>
Yes you need to setup a billing account, there is no way around it these days.
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.
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
keytool -list -v -keystore mystore.keystore
android
SHA1 key;package name
for debug and press enterSHA1 key;package name
for releaseNow use this API key your project
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/common_map_api_key"/>
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.
manifestplaceholders
build.gradle file.See below
buildTypes { debug { manifestPlaceholders = [ mapApiKeyValue:"GHjaSyAjlyp3O831lgaonHMXsd-_DpQ3002x3S4"] } release { manifestPlaceholders = [ mapApiKeyValue:"AIzaSyAuMGDLr2HeuRed4JA0CrdYYdZRjeC3EA"] } }
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)
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With