How to configure Google Maps API keys for Android

Dragana Vucic
5 min readApr 21, 2020

--

Photo by henry perks on Unsplash

Introduction

After successfully publishing a couple of apps on Google Play, I have experienced an unusual thing: the day after releasing one of my apps, I noticed that the newly released application was showing a gray screen in the place where Google Map was supposed to appear!

Since it is a travel app, and I am using Google Map to display locations of interesting objects nearby, it is pretty important to have it working.

Anyway, I connected my phone via ADB and in Logcat I saw the following error:

E/Google Maps Android API: Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.

My first thought was that I may have forgotten to check the API key for Google Maps for the release version. I immediately opened Android Studio and on my surprise both debug and release keys were there. Next, I opened Google API Console and voilà — only my debug key was listed there and release key was gone — which was causing my Google Map view to render gray background, as it was not properly defined.

I have read a couple of answers online, and none of them were actually enough to resolve my issue.

So, here it is — the proper way of setting API keys for Google Maps.

API keys

As you could conclude from the introduction, in order to use Google Maps in Android applications, we have to get those API keys. But in this post, I will take a step back and start by generating one Google Maps activity.

So, in your Android Studio project, click File -> New -> Activity -> Gallery -> Google Maps Activity. This action will add an activity in the AndroidManifest.xml file together with the following:

Also, it will generate boilerplate files like layout resource file & activity class. Besides, it will create a placeholder for API keys under values/google_maps_api.xml. Note that it creates two different files as explained in the comment section above.

\app\src\debug\res\values\google_maps_api.xml \app\src\release\res\values\google_maps_api.xml

Even though those two files are different, placeholder looks the same in both:

Keep in mind that in the Android project view, only debug google_maps_api.xml is shown.

You can see both files by changing your project view from default Android to Project view.

So what are these keys for? Basically, you can use the debug API key while developing and testing your app, but when you are ready for release, you have to add the API key for the release version.

Otherwise, you will end up facing the same problem as I talked about in the introduction.

Debug

In the \app\src\debug\res\values\google_maps_api.xml file, Android Studio will also provide you with a link that you can follow in order to generate your API key. Below that link are instructions for the manual key generation, for which you need a package name and SHA-1 certificate fingerprint.

If you have chosen the first option and clicked on the generated link, it will create a project for you on Google API Console, enable the Maps SDK for Android API and generate a key which will be restricted to your android app package name.

Whichever option you chose — generating manually or with the given link, you will end-up having generated a debug API key which you can now copy-paste under above-given placeholder ‘’YOUR_KEY_HERE’’.

After that, you can start developing and working with Google Maps in your Android app.

Note that it is not required for the debug key to be restricted, but it is a good practice to do so.

Release

Remember that before releasing your app on Google Play, you have to generate one more key for the release version.

To do so, go to Google API Console -> APIs & Services -> Credentials and click on Create Credentials -> API key. This option will generate a key that is not restricted to anything (yet). Copy-paste that key under the placeholder in \app\src\release\res\values\google_maps_api.xml.

Now, you will have two different keys for debugging and release versions. You can go ahead and publish your app.

After that part is done, go to Google Play Console and enter your app. Click on Release Management -> App signing. On that page, you will see information about different certificates.

If we start from below, under the Upload certificate section you will see the SHA-1 certificate fingerprint where it clearly says:

Use the certificate below to register your app signing key with your API providers for app testing purposes.

Also, this SHA-1 certificate fingerprint can be matched with the one used to generate your signed APK. That can be seen with the command:

$ keytool -list -v -keystore your_keystore.jks -alias your_key_alias

Now, let’s look at the above App signing certificate. There is another SHA-1 certificate fingerprint that is different from the previous one. There it says:

Use the certificate below to register your app signing key with your API providers.

Let’s copy that key and go to Google API Console. Open the API key which is used for the release version. Under Key restrictions, go to Application restrictions and select Android apps. Enter the application package name and paste the key copied from Google Play Console.

You can also restrict it to the desired API, under API restrictions. In this case, you can choose Maps SDK for Android and click Save.

It will take some time for the changes to take effect.

After that, you will have two different API keys, one for debug and one for release versions.

Conclusion

To conclude, even if it looks like it has many steps, it is actually an easy and straightforward process once you get familiar with Google API Console & Google Play Console

--

--