14

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • do you have proguard added to your project ? – Sahil Jul 03 '18 at 16:39
  • check for logs/error logs. If there is any configuration problem there might be log generated for sure. – Shubham AgaRwal Jul 03 '18 at 17:03
  • @Sahil I'm not certain. Would proguard affect the release behavior? – Tim Biegeleisen Jul 03 '18 at 18:12
  • @Killer This is generally good advice, but given that everything completely works from Android Studio, I'm not sure the logs would help. – Tim Biegeleisen Jul 03 '18 at 18:13
  • @TimBiegeleisen, first of all, you can test the application in release build variant. You don't have to wait for the application or need to download the production version. Since you stated that you didn't have any restriction and have already enabled the api. I would suggest you to enable place api, place api for sdk, map api and try once. And once again if there might be slight chance of configuration issue. You might get red lines in error log. Do have a look once – Shubham AgaRwal Jul 03 '18 at 18:36
  • 1
    @Killer I didn't know about building in release mode. I only verified I guess in debug mode. I will try your suggestions and then look for errors. Thanks for trying to help. – Tim Biegeleisen Jul 04 '18 at 05:57
  • In order to test application in release mode change build variant and add signing configuration in module level build.gradle file https://stackoverflow.com/questions/33802401/how-to-debug-the-android-app-in-release-mode-using-android-studio – Shubham AgaRwal Jul 04 '18 at 06:03
  • 1
    @Killer I have run the activity in question in debug mode under release build from Android studio onto my test phone. But, to my surprise, the Google Places autocomplete widget is actually working. Now I am at a loss at to why it is failing on the version downloaded from the app Store. – Tim Biegeleisen Jul 04 '18 at 15:23
  • check if you are maintaining different keys for debug or release mode. Or may be any restriction in console (However you are saying you don't have any). Let other developer see this post and might help you. – Shubham AgaRwal Jul 04 '18 at 17:32
  • Did you add the SHA-1 key generated from the keystore file you used to create your release version to console as well? You mentioned using the debug version and running in release mode, but signing your app with your own keystore creates a different SHA-1 key. – Sander van't Veer Jul 05 '18 at 14:47
  • @Sandervan'tVeer I ran `keytool -list -v -keystore your_keystore_name -alias your_alias_name` agains the JKS keystore file which was used to build and publish the APK file to the Play Store. If you think this could be the problem, then go ahead and post an answer, I'm happy to try whatever you want to suggest. – Tim Biegeleisen Jul 05 '18 at 14:53
  • @TimBiegeleisen Hmm alright it does sound like you did everything correct. I'll give your issue some more thought. I've had problems with the places API before but I'm not entirely sure anymore how I fixed it in the end. – Sander van't Veer Jul 05 '18 at 15:03
  • have you look at this answer ??? https://stackoverflow.com/a/44677785/9024123 – Rohit Chauhan Jul 11 '18 at 13:20
  • @RohitChauhan If you can post an exact answer to my problem, perhaps partly based on your link, then I'm happy to give you the bounty. Specifically, you should address why my entire setup works locally, but when I upload to the Play Store, it stops working. – Tim Biegeleisen Jul 12 '18 at 05:17
  • Have you checked by installing Signed APK generated in your PC's folder instead of running from release build variant? Is it working? Also, check by installing your Signed APK or from release build variant without applying Proguard to get sure that the issue is not related to Proguard. – Viraj Patel Jul 12 '18 at 07:31

5 Answers5

13

Your Statement in question ===>I also entered the correct package name and SHA-1 hash for my app

Problem: Actually till now you just sign your app with your upload key but for play store you required google created SHA-1 .

Here is more detailed explanation

How app signing by Google Play works.[Document Link]

Step 1: Create an upload key

Step 2: Prepare your release

Step 3: Upload your signed app

Step 4: Register your app signing key with API providers

Before the app is delivered to users, Google Play will remove your upload key signature and re-sign with a new key.

It means You sign your app with your upload key. Then, Google verifies and removes the upload key signature. Finally, Google re-signs the app with the original app signing key you provided and delivers your app to the user.

So to resolve your problem you required to do these additional steps after Upload your app to Google Play and app has been submitted and approved

  1. Select your app from Google Play Console,Go to Development Tools -> Release management -> App signing.

  2. Copy the first SHA-1 certificate which Google Play has issued after uploading the app.

  3. Go to Google Console and go to your project.

  4. Select your API-key, and paste the SHA-1 after package name.

NullPointer
  • 7,094
  • 5
  • 27
  • 41
  • The documentation you quoted says that Google Play signs with a _new_ key, _not_ the one you provide. But, other than this, your answer is closest to solving the problem. By the way, following your steps worked. – Tim Biegeleisen Jul 12 '18 at 16:01
  • Thanks.I tried to explain it in crisp manner.However statement i mentioned *Before the app is delivered to users, Google Play will remove your upload key signature and re-sign with a new key.* clarify that the **key** you have will work till before releasing on play-store. – NullPointer Jul 13 '18 at 00:24
  • Thank you so much, struggled with this for days. You Sir, saved the day :D – barotia Aug 07 '18 at 08:51
  • Here you're explqining the seteps for the release, what about the debug before uploading the app to the play store !? – SOAL ABDELDJALLIL Jun 10 '19 at 22:55
  • use above steps then also dont get near by places in android live app – Makvin Jul 30 '19 at 08:55
  • 1
    Note that on the new Google Play Console they changed the location of the SHA key. It's now under Release / Setup / App Integrity – Sam May 03 '21 at 05:50
3

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

you can try this different solutions, maybe it's work for you

Solution - 1

try to save log in text file for every event like onError, OnPlaceSelected, onConnectionFailed, OnConnection and then upload your app to playstore now run the app and check the log file i am sure if it's not working then i should display some problem in log file

Solution - 2

Release APK and debug APK has different SHA1 and different API keys for google services. Both of them must be added to console

Solution - 3

or now you can use intent for autocomplete feature

int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
...
try {
Intent intent =
        new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                .build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
} 

For More Detail look at here - https://developers.google.com/places/android-sdk/autocompleteand read carefully

Solution - 4 at last try to display place name as Toast with custom latitude and longitude without autocomplete to check whether we are receiving data from server or not

Hope this will be helpful for you

Rohit Chauhan
  • 1,119
  • 1
  • 12
  • 30
0

I believe the problem is with missing SHA-1 in your Google console, the place where You already activated api places. There should be place to add release SHA-1 (key). Then You will be able see Google Places on another devices.

To generate SHA-1 there is a gradle script in android studio. Click on Gradle bar which is placed on the right side of Android Studio. Select the name of Your app, open tasks>android>signingReport. It will output all SHA-1 keys.

enter image description here

deadfish
  • 11,996
  • 12
  • 87
  • 136
  • You are correct that the problem has to do with the signing. But the correct SHA-1 actually needs to come from the key used by Google Play to sign the release APK, and _not_ from Android Studio. – Tim Biegeleisen Jul 12 '18 at 16:03
  • Yes, You are right. But! If you have declared sign key in `build.gradle` in section `signingConfigs -> release` - executing that script will give You SHA-1 for release. – deadfish Jul 12 '18 at 17:00
-1

You can try this sample project where I use map and place api also wikipedia geo api.

https://github.com/XinyueZ/wikipedia-google-place-together

Of cos, you should fill a string @string/google_maps_key

Might help you.

TeeTracker
  • 7,064
  • 8
  • 40
  • 46