I know Gradle is powerful and I would like to manage the API keys for development/produciton of Google Maps
Currently I always need to manually comment one line and uncomment the other to make it work. Is there a way to do it automatically in Gradle with some custom release configuration ?
<!-- MapView v2 API --> <uses-library android:name="com.google.android.maps" /> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_DEV_KEY]" /> <!-- PROD <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="[MY_PROD_KEY]" /> -->
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.
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.
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