17

Currently, my app is using

  • minSdkVersion 14
  • targetSdkVersion 23
  • compile 'com.google.android.gms:play-services-gcm:8.4.0'
  • compile 'com.google.android.gms:play-services-base:8.4.0'
  • compile 'com.google.android.gms:play-services-analytics:8.4.0'
  • compile 'com.google.android.gms:play-services-ads:8.4.0'
  • compile 'com.google.android.gms:play-services-drive:8.4.0'

I am using the following code to perform GoogleApiClient connection.

public class GoogleApiClientFragment extends Fragment implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    public interface ConnectionCallbacks {
        void onConnected(GoogleApiClient googleApiClient, int action);
        void onCancel(int action);
    }

    public static GoogleApiClientFragment newInstance(String accountName, int action) {
        GoogleApiClientFragment googleApiClientFragment = new GoogleApiClientFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_EXTRA_ACCOUNT_NAME, accountName);
        bundle.putInt(INTENT_EXTRA_ACTION, action);
        googleApiClientFragment.setArguments(bundle);
        return googleApiClientFragment;
    }

    /**
     * Handles resolution callbacks.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RequestCode.REQUEST_GOOGLE_API_CLIENT_CONNECT) {
            if (resultCode == Activity.RESULT_OK) {
                mGoogleApiClient.connect();
            } else {
                Activity activity = this.getActivity();
                if (activity instanceof ConnectionCallbacks) {
                    ConnectionCallbacks connectionCallbacks = (ConnectionCallbacks)activity;
                    connectionCallbacks.onCancel(action);
                }
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        Bundle bundle = this.getArguments();
        this.accountName = bundle.getString(INTENT_EXTRA_ACCOUNT_NAME);
        this.action = bundle.getInt(INTENT_EXTRA_ACTION);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())
                    .setAccountName(accountName)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addScope(Drive.SCOPE_APPFOLDER)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.i(TAG, "GoogleApiClient connected");

        Activity activity = this.getActivity();
        if (activity instanceof ConnectionCallbacks) {
            ConnectionCallbacks connectionCallbacks = (ConnectionCallbacks)activity;
            connectionCallbacks.onConnected(mGoogleApiClient, action);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "GoogleApiClient connection suspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i(TAG, "GoogleApiClient connection failed: " + connectionResult.toString());

        if (!connectionResult.hasResolution()) {
            Utils.showLongToast("debug two " + connectionResult.toString());

            // show the localized error dialog.
            GoogleApiAvailability.getInstance().getErrorDialog(this.getActivity(), connectionResult.getErrorCode(), 0).show();
            return;
        }
        try {
            connectionResult.startResolutionForResult(this.getActivity(), RequestCode.REQUEST_GOOGLE_API_CLIENT_CONNECT);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }

    private String accountName = null;
    private int action = -1;
    private GoogleApiClient mGoogleApiClient;

    public static final int ACTION_LOAD_FROM_CLOUD = 0;
    public static final int ACTION_SAVE_TO_CLOUD = 1;
    private static final String INTENT_EXTRA_ACCOUNT_NAME = "INTENT_EXTRA_ACCOUNT_NAME";
    private static final String INTENT_EXTRA_ACTION = "INTENT_EXTRA_ACTION";

    private static final String TAG = "GoogleApiClientFragment";
}

Certain users (Not all users) are hit with the following problem.

is having trouble with Google Play services. If the problem persists, please contact the developer for assistance.

enter image description here

The error dialog box is due to the GoogleApiAvailability.getInstance().getErrorDialog code in onConnectionFailed.

Whenever

        mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())
                .setAccountName(accountName)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

is executed, it always hit

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

When I print out connectionResult using toString, it gives

ConnectionResult{statusCode=INTERNAL_ERROR, resolution=null, message=null}

One of my users is having the following spec.

  • Sony Xperia z5
  • Android 6
  • Google Play Services 9.0.83
  • Only 1 Google account in his device

I even try to re-compile the APK with Google Play Services 9.0.1. But, the same problem still occur.

Any idea, on how I can resolve the problem?

BNK
  • 23,994
  • 8
  • 77
  • 87
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • About the latest version of the Google Play Service, I think there is a bug issue right there now. Try to check this [SO question](http://stackoverflow.com/questions/37333220/googlesignatureverifier-signature-not-valid-message-not-using-the-google-maps-a) if it can help you. – KENdi May 28 '16 at 14:20
  • @KENdi Doesn't seem like a same issue. But thanks anyway. – Cheok Yan Cheng Jun 01 '16 at 02:39
  • have you try to reconnect after getting error as i find in docs it says may be it will ok after trying reconncet this. as docs refers https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult#constants – Ankur1994a Jun 01 '16 at 09:37
  • Are u asking for permissions in android 6? In that case the app wont crash but the connectionResult will be fail. – JpCrow Jun 01 '16 at 20:30
  • @JpCrow, I've checked this, this is not the issue. You can check it by installing the app 'JStock' yourself. – jobbert Jun 02 '16 at 07:22
  • To avoid permission, I'm using `AccountPicker` to get account name - https://gist.github.com/anonymous/9bdf88d7d21a1fd96f2fab1445cab8b8 which I believe doesn't require GET_ACCOUNT permission. Then, I will pass the account name explicitly into `GoogleApiClient` via `setAccountName`. I avoid using account picker from GoogleApiClient. – Cheok Yan Cheng Jun 03 '16 at 09:27
  • @Yvette It works for most of the users. Minority user affected. – Cheok Yan Cheng Jun 03 '16 at 09:27
  • @Yvette Please refer to this http://stackoverflow.com/a/34706726/72437 too – Cheok Yan Cheng Jun 03 '16 at 09:31
  • Can you also share the Activity that is creating this fragment? Or the piece of code where you create and use it. – Mimmo Grottoli Jun 07 '16 at 11:38
  • Yes. Please see my posted answer. – Cheok Yan Cheng Jun 13 '16 at 18:02

5 Answers5

11

Given it's working on other Anrdoid 6.0 devices and you have specified the device you are having complaints on. I suspect, it's not your app. There is a known issue with Sony Xperia z5 on updating to Android 6.0.

Sony claims to have fixed the bug.

Re: "Google Play Store has Stopped" after Z5 Premium Marshmallow Update
April

Hi everyone, I got information today that this will be fixed in the next software update planned to be released soon.
Official Sony Xperia Support Staff

So ensure to advise the client to ensure all Sony updates are current, but this is still not working for all consumer. The bad news is, there is no fix aside from a factory reset, so back to Android 5 and sometimes this fails.

I recommend directing the customer to discuss this with Sony and if a factory reset does not fix the issue, this will be a consumer issue of whether the phone should be replaced.

This thread on the sony mobile website "Google Play Store has Stopped" after Z5 Premium Marshmallow Update discusses these issues at length (122 messages on it) and there are many suggestions, beyond factory reset, for example, using Sony PC companion.

Xperia Z5 Issues After Android Marshmallow Update: Here's A Fix From Sony

Sony Xperia Z5 users' excitement over receiving the Android 6.0 Marshmallow update seems to have been short-lived as users of the handsets are now complaining of issues after updating the device to the latest OS.

Users of the smartphone have taken to the Japanese company's online support page to vent their ire and complain that access to the Google Play Store had halted after updating to Android 6.0 Marshmallow.

Several Xperia Z5 users complained that their smartphones "broke" after installing the new OS. They were also greeted by a pop-up sign that says their "Google Play Store has stopped." The users were also unable to open the Play Store app.

"As soon as I updated to Marshmallow, I keep getting the "Unfortunately Google Play Store has stopped..." and the message keeps coming up every 5 seconds. I tried clearing the cache, deleting data, uninstalling, disabling the app, force closing, signing out of google and back on. Does not work. I keep getting the pop up non-stop and it happened as SOON as it updated to Marshmallow," noted a user on the support forum.

"Yup, having the same problem here. Tried uninstalling the updates in /settings/apps/google play store, also tried installing the .apk through chrome. Still getting the error message. Spoke with customer support and running the "repair" option from Sony Bridge was the only advice they offered. Creating a back-up now before I try running it..." said another.

Sony has acknowledged the problem and has only offered a factory reset.

While this resolves the issue for other Xperia Z5 users, some are still unable to access the Play Store despite performing the reset.

If this is happening on other devices, let me know, as at this point, I would suggest this is the problem without more differentiating details.

I've scoured high and low and given that it's working for most devices and not for a few it's difficult to understand what the problem is. I would suggest running it with the updated dependencies, as the android 6.0 phone uses Google Play Services 9.0.83, I understand you have run this, but it is better to stay abreast of this.

dependencies {
    compile 'com.google.android.gms:play-services:9.0.2'
}

Given there's known bugs with Google services, with Android 6.0 updates and there were many bugs between the Android 5.0 and it's 5.0.1 and 5.0.2 updates. Some more that were fixed in 5.1, it does make it hard to troubleshoot some issues.

I have also faced this issue with an app and people saying a such and such device is not working, and then attempting to trouble shoot it, ans sometimes there can be issues in the users phone settings that can also cause issues with an app and some of these settings vary between handsets.

I'd suggest to create your listeners explicitly within you Builder, and add more logging to check where exactly it is failing within the on failed connection, as you can have connection within the on failed connection and need to manage your connection within the connection call backs.

mGoogleApiClient = new GoogleApiClient.Builder(this.getContext())
                .setAccountName(accountName)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
            @Override
            public void onConnected(Bundle bundle) {

                if(mGoogleApiClient != null) {
                    Log.i(TAG, "GoogleApiClient connected");

                    Activity activity = this.getActivity();
                    if (activity instanceof ConnectionCallbacks) {
                        ConnectionCallbacks connectionCallbacks = (ConnectionCallbacks)activity;
                        connectionCallbacks.onConnected(mGoogleApiClient, action);
                    }
                }
            }

            @Override
            public void onConnectionSuspended(int i) {
                mGoogleApiClient.connect();
            }
        })
        .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(ConnectionResult connectionResult) {
                Log.i(TAG, "GoogleApiClient connection failed: " + connectionResult.toString());

                if (!connectionResult.hasResolution()) {
                    Utils.showLongToast("debug two " + connectionResult.toString());

                    // show the localized error dialog.
                    GoogleApiAvailability.getInstance().getErrorDialog(this.getActivity(), connectionResult.getErrorCode(), 0).show();
                    return;
                }
                try {
                    connectionResult.startResolutionForResult(this.getActivity(), RequestCode.REQUEST_GOOGLE_API_CLIENT_CONNECT);
                } catch (IntentSender.SendIntentException e) {
                    Log.e(TAG, "Exception while starting resolution activity", e);
                }
            })
        .build();

    mGoogleApiClient.connect();
}
    

I would also be adding your class members/variables:

private String accountName = null;
private int action = -1;
private GoogleApiClient mGoogleApiClient;
// etc

To the top of your class, as it's confusing having them at the bottom, and it's better to have readable code.

This question Multiple GoogleApiClient not firing connect() may also be of assistance.

Some side issues for completeness for others landing here:

There will also be the need to update across to Firebase at some point Migrate a GCM Client App for Android to Firebase Cloud Messaging.

From the Release Notes May 2016 - v.9.0

Google Cloud Messaging Google Cloud Messaging (GCM) is integrated with Firebase. Existing users of GCM can continue to use GCM without interruption, though we strongly recommend upgrading to the new and simplified Firebase Cloud Messaging (FCM) APIs, so that users can benefit from future releases of new features and enhancements. To learn more, see Migrate a GCM Client App for Android to Firebase Cloud Messaging.

Community
  • 1
  • 1
4

It could be caused by multiple problems, have you read the documentation of ConnectionResult? It says this:

A ConnectionResult that can be used for resolving the error, and deciding what sort of error occurred. To resolve the error, the resolution must be started from an activity with a non-negative requestCode passed to startResolutionForResult(Activity, int). Applications should implement onActivityResult in their Activity to call connect() again if the user has resolved the issue (resultCode is RESULT_OK).

There is also a lot of information about the error code you receive in this post: Error : ConnectionResult{statusCode=INTERNAL_ERROR, resolution=null}

The problem could also be that you haven't add the Drive API in your console. But then you should've gotten a logcat message.

Ok I've also tested the JStock app on:

Moto G3 with Android 6

And I am not able to reproduce the bug. But the connection bar at the bottom doesn't change when you set your phone to flight mode.

I'm not sure if this is any help to you, but let me know.

Community
  • 1
  • 1
jobbert
  • 3,297
  • 27
  • 43
  • Hey, sorry for not mentioning it clearly. To trigger `GoogleApiClient` from running, you need to go to "Open from Cloud" or "Save to Cloud". With such, it will use `GoogleApiClient` to access Google Drive. Note, the above 2 features are locked. However, you can easily unlock them, by activating 7 days free trial. Can you kindly let me know, whether you can reproduce the problem? (I'm not able to at my side, by using Nexus 5 and Nexus 4) – Cheok Yan Cheng Jun 02 '16 at 08:11
  • I will, I'll let you know :) – jobbert Jun 02 '16 at 08:33
  • Both saving and retrieving from cloud work for me. My first attempt to save to the cloud failed but the second one worked. Have you looked into my initial answer? Can I help you in any way? – jobbert Jun 02 '16 at 08:50
  • 1
    Thanks for testing. The SO link you post doesn't help in my case. The resolution for your posted SO link, is fixing on Google API console. But, not for my case. If there's problem with my Google API console, all users will fail. – Cheok Yan Cheng Jun 03 '16 at 02:02
3

After working closely with my user, I realize the solution is

1. Uninstall Google Play services

enter image description here

enter image description here

2. Update Google Play Services again

https://play.google.com/store/apps/details?id=com.google.android.gms&hl=en

enter image description here

It works fine after that. If it still doesn't, perhaps restarting your device might help?!

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • Did this solve the problem for good? In my case after I update to the latest version (9.0.83) the problem appears again at some point... – stan0 Jun 14 '16 at 14:26
  • I have an Android 5 user, and Android 6 user. Both of them able to solve this problem by uninstall GPS and re-install latest version. Perhaps restarting the device would help again? – Cheok Yan Cheng Jun 14 '16 at 15:38
  • Thanks for the help. I think my issue is slightly different, so my situation is not related. Thanks anyway – stan0 Jun 14 '16 at 15:39
1

Try to update minSdkVersion 14 to minSdkVersion 16 or above. cause its happened with me and after long searched, I changed it and solved m problem so may be it works for you.

parik dhakan
  • 787
  • 4
  • 19
0

I have exactly the same problem and my app also crash at the exact point ( OnConnectionFailed) when it show the error dialog. But if i dont click on button OK, it not crash, only it could not connect to Google Play Service. I have two apps with setup in graddle and everything almost the same and one crash, this one still run as it used to be. https://play.google.com/store/apps/details?id=com.softphan.services.smartcallrecorder

How ridiculous should it be? I run Google Nexus 5 so the problem should not bound to Sony products.

Cuong Phan
  • 311
  • 1
  • 4
  • 8