问题
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.
回答1:
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
Select your app from Google Play Console,Go to Development Tools -> Release management -> App signing.
Copy the first SHA-1 certificate which Google Play has issued after uploading the app.
Go to Google Console and go to your project.
Select your API-key, and paste the SHA-1 after package name.
回答2:
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
回答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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/51159372/google-places-for-android-api-key-does-not-work-on-app-from-play-store