Manage Google Maps API Key with Gradle in Android Studio

Manage Google Maps API Key with Gradle in Android Studio

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.

How to use gradle to replace Google Maps API key?

Shadowsheep's solution works, but it's not really what I want to do.

What I've ended up doing.

  • First, I was under the mistaken impression it was absolutely necessary to populate google-maps-api.xml with the key, when it apparently is sufficient to set the key in the Manifest.

  • Second, I've added a <meta-data android:name="com.google.android.geo.API_KEY" android:value"@string/GoogleMapsApiKey"> line into the Manifest

  • Third, I've set mapsKey via my chosen tool (gradle-credentials-plugin by etiennestuder)

  • Finally, I've added into the app's module build.gradle buildTypes section these two lines:

def mapsKey = credentials.GOOGLE_MAPS_API_KEY ?: "Key Missing"

it.resValue "string", "GoogleMapsApiKey", mapsKey

Missing api_key/current key with Google Services 3.0.0 and Maps API key in build.gradle

Have you tried putting an empty "current_key" as follows:

"api_key": [{ "current_key": "" }]

See how that goes.

EDIT:
You should replace "api_key": [] (located in "client") with "api_key": [{ "current_key": "" }]

how can set map api key in android studio?

You need to set your map key in your AndroidManifest.xml file. Between <application> </application> tag.

      <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your key" />

Refer Google map Integration in Android for better understanding. Take care to add all required permissions for map integration in manifest file.

To create google api key, you will require sha1 key. If you have not yet created it. That you can follow SHA1 key in Android studio

  <!-- map -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

Add this before your <application> tag in manifest.

Is there a safe way to manage API keys?

Here is another way:

Place the API key in a file accessible to the build machine/server, we'll call it:

/usr/api_user/api_key1

With contents:

myApiKey = abcdefghijklmnopqrstuvwxyz

You will now access it using the `BuildConfig' gradle object. Modify your code to this:

public interface APIContract {
//The API KEY MUST NOT BE PUBLISH. It is possible to generate a new one for free from www.themoviedb.org
//Remove before commit !!!
String API_KEY = BuildConfig.MY_API_KEY;
/...
}

Then in your build.gradle, add something like this:

buildConfigField "String", "MY_API_KEY", getMyApiKey("myApiKey")

And also add this:

//return a MY API KEY from a properties file.
def getMyApiKey(String property){
Properties properties = new Properties()
properties.load(new FileInputStream("/usr/api_user/api_key1"))
return "\"" + properties.getProperty(property) +"\""
}

You can relocate the API directory location, as you can tell, so that it is not a part of your repo. Of course, then it will have file system dependencies for the build... which you could have a list setup in a CI/CD environment (maybe a tool like Jenkins) to replicate the build files to a private repo, for backup purposes.

Saving the API Key in gradle.properties

  1. The idea of this indirection is to allow you to store API key in files that are not uploaded to the version control: gradle.properties is a local file and should not be stored under the version control, and BuildConfig is a generated class, so it will only be created at build time. It's definitely easier to store the API key somewhere as a plain String, but then you'll have to commit it to the repo.
  2. During build time, Gradle will generate the BuildConfig file to store some build-related constants. You can use buildConfigField command to instruct Gradle to add custom fields into BuildConfig. After the project is built, you can reference these constants inside your source code.

How can I create an Android application in Android Studio that uses the Google Maps Api v2?

Finally I managed to run GoogleMapsAPIv2 project using Android Studio.

EDIT: As mentioned by Xavier, this method is going to work for non-gradle based projects only. And UI part which was used in this tutorial will be excluded from AndroidStudio. So if you have your own project which uses Gradle build system, you need to manually modify build.gradle configuration file since Android Studio UI doesn't affect it.

EDIT2: With AndroidStudio v0.1.1 release, UI part responsible for modules dependencies has been eliminated, so for now we need to update dependencies manually through build.gradle file. UI for changing gradle dependencies is going to be released in next releases

EDIT3: For those who still tries to use this approach - please note that it is obsolete and doesn't work anymore

Here is what I did:

1) I took maps project from the Google Play Services samples and copied that to the separate directory. That is going to be our MapsApiV2 project we will be trying to run. On my Mac it was located at <sdk_location>/extras/google/google_play_services/samples
I placed it to the ~/Work/stack/

2) Copied google-play-services_lib project directory to the same place (~/Work/stack), so my working directory looks like this. Btw, lib project is located at <sdk_location>/extras/google/google_play_services/libproject:
Sample Image

3) Now let's open Android Studio. On welcome screen press Import Project and import our maps project from ~/Work/stack/maps. Now we see a lot of complaints about unknown reference to GMS library:

Sample Image

4) Now we need to add Google Play Service as a reference library. Going to View -> Open Module Settings

5) On the Modules tab, click + button and select Import Module and import your GooglePlayServices lib. I didn't change anything in the wizards, so clicked Next all the way to the end:

Sample Image

6) Now you need to reference this imported library. Open this screen again (go to View -> Module Settings). Make sure you have your maps project and Dependency tab selected. Click + to add a dependency and select Library. Choose your imported library there:

Sample Image

7) Now we can see that it is not complaining about GMS library, but still complaining about support library:

Sample Image

8) Let's fix it. I have my support library located at <sdk location>/extras/android/support/v13/android-support-v13.jar. So let's try to add it to our workspace. Go to View -> Open Module Settings and select Libraries tab. Select + -> Java and select support library:

Sample Image

9) Now it is going to ask you which project to add this lib to, so make sure you have selected your maps project:

Sample Image

10) At this point code should compile w/o problems. Just make sure you are targeting the right SDK version in Manifest.

Have fun



Related Topics



Leave a reply



Submit