Skip to content Skip to sidebar Skip to footer

Google Play Games

Good day everyone. I'm trying to implement Achievements in a game I'm developing. I already set everything on google play console, got the app-id, put in the manifest the following

Solution 1:

I think you should check:

GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE).

And if there is no permissions in that account you should use

GoogleSignIn.getClient(this, gso).silentSignIn or GoogleSignIn.getClient(this, gso).getSignInIntent() 

with startActivityForResult to receive account with GAMES_LITE scope.

GoogleSignIn.hasPermissions also returns false for null account which could be also result of the getLastSignedInAccount.

Example:

GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);

if (GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE)) {
    onSignIn(account);
} else {
    signInClient
      .silentSignIn()
      .addOnCompleteListener(
          this,
          task -> {
            if (task.isSuccessful()) {
              onSignIn(task.getResult());
            } else {
              resetSignedIn();
            }
          });
}

Solution 2:

Have you added a dependency on Google Play Services in your manifest correctly? In the "common errors" section here

"4. When developing for Android, include the Play Games SDK as a library project, not as a standalone JAR Make sure that the Google Play services SDK is referenced as a library project in your Android project, otherwise this could lead to errors when your app is unable to find Google Play services resources. To learn how to set up your Android project to use Google Play services, see Setting Up Google Play Services."

Also, do you have in your manifest?

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>

Do you have in your gradle file dependencies?

compile "com.google.android.gms:play-services-games:${gms_library_version}" compile "com.google.android.gms:play-services-auth:${gms_library_version}"

Solution 3:

I had the same issue when showing a Leaderboard and found that Oleh's solution helped solve the issue. Requesting the right Scope is key. My code to build the GoogleSignIn client in onCreate is:

// Build a GoogleSignInClient with the options specified by gso.GoogleSignInOptionsgso=newGoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(clientId)
            .requestScopes(Games.SCOPE_GAMES_LITE)
            .build();
mGoogleSignInClient = GoogleSignIn.getClient(HomeActivity.this, gso);

Later, when showing the Leaderboard, I do it like so:

GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (null != account) {
    // check permissions, show Leaderboard when allowed to
    boolean hasGamesLitePermissions = GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE);
    if (hasGamesLitePermissions) {
        Games.getLeaderboardsClient(this, account)
            .getAllLeaderboardsIntent()
            .addOnSuccessListener(this)
            .addOnFailureListener(this);
}

My Activity does also implement two listeners for the sign-in process to work:

publicfinalclassHomeActivityimplementsOnSuccessListener<Intent>,
    OnFailureListener

(from the package com.google.android.gms.tasks)

And finally, in onSuccess I can show the Leaderboard

public void onSuccess(Intent intent) {
    startActivityForResult(intent, RC_LEADERBOARD_UI);
}

onFailure just displays an error to the user in my case. But be sure to have both listeners so you don't miss any useful details when debugging.

Post a Comment for "Google Play Games"