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?
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.
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
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.
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