Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places for Android API key does not work on app from Play Store

I recently launched an Android app which makes use of the Google Places autocomplete widget/API. As part of the configuration for this, I added the API key to my Android's manifest file:

<application>
    ...
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="MY_KEY_HERE"/>
</application>

Furthermore, in the Google Developers console, I enabled this API key for Android devices only, but did not restrict to any particular API. I also entered the correct package name and SHA-1 hash for my app. Now I have the following problem:

  • When I deploy the app onto an Android phone directly from Android Studio, the Google APIs all work. This is true whether I use the debug or release mode variant.
  • When I publish an APK from the same exact code to the Play Store, and then install the app, everything works except the Google autocomplete API is broken. When trying to access it, it closes right away.

I am certain that what I deployed to the Play Store is in fact the same app running locally, because I had also recently made some minor UI patches, and those also showed up on the store version. I verified several times that the correct key is what appears in the manifest file.

I am at a loss to explain this. This problem is particularly difficult because everything works from Android Studio, so I can't do something like go into debug and try to catch an exception.

If you have some expertise with Android and autocomplete, and you have seen a problem similar to this, then I welcome your answer.

like image 961
Tim Biegeleisen Avatar asked Jul 03 '18 16:07

Tim Biegeleisen


People also ask

How can I get Google Map API key for mobile app?

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.

Where is Google API key stored Android?

According to Google Documentation to store API keys securely it is recommended to use local properties. If you don't have the file local. properties in your project, it should be located in the android project root directory. Make sure to add it to intentionally untracked files in your version control tool.


1 Answers

The answer by @NullPointer is functionally correct, and does indeed fix my problem. But to give a more direct answer to my own question, the root cause of the problem has to do with substantially different procedures for configuring Google APIs for a local debug version of an Android app and a release version of the same app.

Part of the confusion here has to do with the Google console itself which says:

Then use the following command to get the (SHA-1) fingerprint:

keytool -list -v -keystore mystore.keystore

When building and testing an app locally, in debug mode, in fact running keytool against the debug keystore file, and pasting the SHA-1 hash into the Google console will get the API to work. After sitting on a codebase for several months, or longer, and the APIs appearing to be very stable, it then comes as a surprise what happens next when repeating these steps with the release APK.

It is a surprise, because following the same steps won't work for the release app. This is because the Google Play Store actually resigns your APK with a different key, which therefore has a different SHA-1 hash. To find the SHA-1 hash which needs to be used, one may visit Release management -> App signing in the Google Play console. Do this after publishing your app and waiting perhaps 10-15 minutes for it to refresh. Then, just paste this SHA-1 back into the Google API console, and off you go.

By the way, my question is very similar to Published App on Play Store can't communicate with Google Maps API and Facebook API, though not much emphasis was given there for why a Google API might be working in debug mode, but not on the Play Store. Incidentally, I give credit to @RohitChauhan who pasted a comment containing a link to this question.

like image 146
Tim Biegeleisen Avatar answered Sep 23 '22 23:09

Tim Biegeleisen