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
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 affffd 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" />
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 release 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"/>
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"/>
This way you only have one AndroidManifest.xml and you set value based on your build type. Hope this helps.
For organizations that need to maintain separate keys, you can place them in separate directories in Android Studio. Make sure the subdirectory of src
you use matches a flavor or buildType
From Building Your Project with 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 name of the app.
<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>