How to configure Google Maps API keys for Android

Image for post
Image for post
Photo by henry perks on Unsplash

Introduction

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

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.

Image for post
Image for post

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

Image for post
Image for post

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

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.

Image for post
Image for post

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

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.

Image for post
Image for post

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
Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

Conclusion

Software Developer at Ericsson. Short portfolio: https://draganavucic.carrd.co

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store