1

I am able to view google maps on Emulators and on my device when USB debugging is enabled. But when I try to generate a debug / release apk and install the apk on my device, I am unable to view the maps.

I have done the following:

a) Generated API Key in https://console.developers.google.com/ and have copied it into google_maps_api.xml of my project.

b) I have generated the necessary SHA key using the following command.

keytool.exe -list -v -alias <myalias> -keystore <my path> -storepass <passwd> -keypass <passwd>

What I am not able to understand is, how do I use the generated SHA?

As per Release APK file not showing google maps I am supposed to add the SHA key into my API key on https://console.developers.google.com/. But, I see no such option to add it.

By installing the apk and then debugging the installed code on an emulator, I see the following exception:

API Key must not be empty.

My google_maps_api.xml is as follows:

<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Have added my api key here</string>

Have added my api key here --> This is where I have added the API ket that I have generated.

Have added this into my manifest file:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

Can someone help me out in understanding what is it that I am supposed to do?

mang4521
  • 742
  • 6
  • 22
  • Does this answer your question? [Android google maps release key?](https://stackoverflow.com/questions/41743234/android-google-maps-release-key) – Vepth Jun 25 '20 at 20:25
  • @Vepthy this link https://developers.google.com/maps/documentation/android-sdk/get-api-key (from the reference attached by you above) did help me out in referencing the API key to the SHA key. I made the necessary change on https://console.developers.google.com/. But, I still don't see the map with the debug/release apk. Where as, on USB testing it works fine. – mang4521 Jun 26 '20 at 11:58
  • Have you tried debugging the code? What error does it throw you? – Vepth Jun 26 '20 at 14:13
  • @Vepthy Yes, the problem is that I am able to view the map if I run the code on an emulator or try to debug the code from a physical device (use device USB debugging). But if I convert the project into an apk and then run it on a physical device, I am not able to view the map. – mang4521 Jun 26 '20 at 16:35
  • @Vepthy I tried something different and was able to debug the apk. I installed the apk on an emulator and later debugged the app. I found the following exception: "API Key must not be empty." I have updated my google_maps_api.xml content above. Could you help me out with this? – mang4521 Jun 26 '20 at 16:48
  • Have you tried to add your API key as described in the sample [here](https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/build.gradle). That is you must create a file ./secure.properties then add this line, where **YOUR_API_KEY** is your API key: **MAPS_API_KEY=YOUR_API_KEY** – smga08 Jun 29 '20 at 09:01
  • @smga08 is this even necessary? The API_KEY gets defined inside google_maps_api.xml. Why is there a necessity to define it inside strings.xml as suggested in the link provided by you? – mang4521 Jun 30 '20 at 18:32
  • I think it's because most are getting errors in Google Play(when developers upload their APKs) that the API key is exposed when it's only added in the Manifest file. That's why they've added an extra step to secure the API key. In addition, if your API key is restricted, you need to add both your package name and you SHA-1 signing certificate. Please note that SHA-1 debug and release might be different. In that case, you need to add your SHA-1 for debug as well as release. More info [here](https://developers.google.com/maps/documentation/android-sdk/get-api-key#restrict_key). – smga08 Jun 30 '20 at 23:18
  • Are you able to check if you've entered your API key within the google_maps_api.xml file in the release directory (/app/src/release/res/values) just like in your debug directory(/app/src/main/debug/values)? – jabamataro Jul 01 '20 at 07:03
  • @smga08 Tank you will ave a look at the link attached above. – mang4521 Jul 01 '20 at 09:48
  • @jabamataro Yes, This is something that I had missed initially, but included it later on. With this inclusion I was able to generate an apk with map being displayed here (on Android Studio): **Build > Build Bundle(s) / APK(s)**. But trying to Generate an apk from: **Build > Generate Signed Bundle / APK** does not lead to the intended result. I see no map after apk installation. – mang4521 Jul 01 '20 at 09:54
  • Are you using 1 API key for both debug and release build? Does that API key only have 1 restriction? Based on how you are generating SHA1 fingerprint, it looks like you are including a path on the command, release certificates does not include this. Make sure that you follow the guide on this [documentation](https://developers.google.com/maps/documentation/android-sdk/get-api-key#getting-the-certificate-information-yourself) on how to get a release certificate. – jabamataro Jul 02 '20 at 02:21
  • Does this answer your question? [Android Studio - Google map still blank on real Android device on release apk](https://stackoverflow.com/questions/30559602/android-studio-google-map-still-blank-on-real-android-device-on-release-apk) – Sweta Jain Nov 10 '21 at 13:08

1 Answers1

0

I was finally able to fix this issue and these were the steps I followed [on a windows machine, should be much more easier on a mac]:

a) cd into C:\Program Files\Java<jdk version>\bin [or wherever your jdk is installed].

b) Run: keytool -list -v -alias -keystore "{keystore ie., .jks file loc}" -storepass {your store password} -keypass {your keypass}

c) By running the keytool cmd, your SHA1 file would then be generated.

d) Login to https://console.developers.google.com/

e) In your project there will be a google_maps_api.xml file. In this file there is a commented url, something like this: https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r={your SHA1 key}%3B{your project package}.

f) In the link above, replace the default SHA1 key with the newely generated key and open the link on a browser from where you already have logged into google develover console.

h) Follow the steps as prompted on the browser and with this you should be able to generate an API key.

i) Paste the generated API key here:

<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">API key</string>

This is available on your google_maps_api.xml file.

j) Now go to {your project location}\app\src\release\res\values and open the google_maps_api file. Here again, replace your API key in the location mentioned in point (i) ie., the code snippet.

k) Finally go to Android Studio > Your Project > Build > Generate Signed Bundle / APK > {input keystore path and passwords as per point (b)} > select debug/release version > Finish

The debug or release apk generated will now allow you to access google maps.

mang4521
  • 742
  • 6
  • 22