Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage Google Maps API Key with Gradle in Android Studio

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]" /> --> 
like image 416
Hrk Avatar asked Sep 30 '14 15:09

Hrk


People also ask

How do I add a Google API key to my 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.


1 Answers

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.

like image 126
bond Avatar answered Oct 13 '22 22:10

bond