Search code examples
androidcrashgoogle-play-games

Google Play Sign-in: java.lang.SecurityException Not signed in when calling API


Since 1/17/22, we have been seeing a substantial (hundreds per day) number of crashes in our Android app with the following stack trace:

Fatal Exception: java.lang.SecurityException: Not signed in when calling API
       at android.os.Parcel.createExceptionOrNull(Parcel.java:2385)
       at android.os.Parcel.createException(Parcel.java:2369)
       at android.os.Parcel.readException(Parcel.java:2352)
       at android.os.Parcel.readException(Parcel.java:2294)
       at com.google.android.gms.internal.games.zzb.zzb(com.google.android.gms:play-services-games@@21.0.0:22)
       at com.google.android.gms.games.internal.zzaf.zza(com.google.android.gms:play-services-games@@21.0.0:259)
       at com.google.android.gms.games.internal.zzf.onConnectedLocked(com.google.android.gms:play-services-games@@21.0.0:752)
       at com.google.android.gms.common.internal.BaseGmsClient.zzp(com.google.android.gms:play-services-basement@@17.6.0:40)
       at com.google.android.gms.common.internal.BaseGmsClient.zzl(:10)
       at com.google.android.gms.common.internal.zzf.zza(com.google.android.gms:play-services-basement@@17.6.0:6)
       at com.google.android.gms.common.internal.zza.zzd(:6)
       at com.google.android.gms.common.internal.zzc.zze(com.google.android.gms:play-services-basement@@17.6.0:3)
       at com.google.android.gms.common.internal.zzb.handleMessage(com.google.android.gms:play-services-basement@@17.6.0:31)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:246)
       at android.os.HandlerThread.run(HandlerThread.java:67)

We did not change anything with our app during this time, so I believe this was triggered by some update on Google Play Game Services' end.

I see similar reports from a number of years ago, but the common suggestion such as the one in the accepted answer here seems to be using a different API than we do. Our implementation does not use a GoogleApiClient object, but rather a GoogleSignInClient object:

signInClient.silentSignIn().addOnCompleteListener(new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
   ...
}

This option does not allow for using the GoogleApiClient.disconnect() method, even via GoogleSignInClient.asGoogleApiClient().

Our GoogleSignInClient setup:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
    .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
    .requestServerAuthCode("...")
    .build();
signInClient = GoogleSignIn.getClient(mainApplication, gso);

Our sign-in handling:

mainApplication().runOnActivityResult(new ActivityResultListener() {
    @Override
    public void receivedActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_SIGN_IN) {
            OnSignInAttemptComplete(GoogleSignIn.getSignedInAccountFromIntent(data));
        } else if (requestCode == GameHelper.RC_GAME_SERVICES_ACTIVITY) {
            if (resultCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) {
                //They logged out
                account = null;
                listener.onSignedOut();
            }
        }
    }
});
mainApplication.startActivityForResult(signInClient.getSignInIntent(), RC_SIGN_IN);

Solution

  • We have not seen any instances of this crash since February 1st, so it seems safe to assume at this point that it was due to an error on GPGS' end which has since been fixed.