Migrate to Play Games Services sign-in v2

This guide describes how to migrate your client code from Play Games Services v1 to v2.

The new SDK contains four major changes to increase sign-in success which you should be aware of:

  1. Sign-in is triggered automatically when your game is launched. Instead of using GoogleSignIn SDK's GoogleSignInClient to perform sign-in, you can use GamesSignInClient.isAuthenticated() to fetch the result of the automatic sign-in attempt.
  2. Client Factory classes will no longer need a GoogleSignInAccount object passed in. Extra OAuth scopes cannot be requested (GAMES_LITE will be requested automatically).
  3. Authentication tokens are now provided using GamesSignInClient.requestServerSideAccess() within the Play Games Services SDK.
  4. The sign out method is removed, and we will no longer require an in-game button to sign in or sign out of Play Games Services.

Additionally, your game will experience additional logins due to automatic sign-in when the game launches. As a result, you should review your quota management to ensure that your game does not exceed the login request quota.

Target the new SDK Maven repository

If you are using the Gradle build system, this can be done by changing your dependency to the com.google.android.gms:play-services-games-v2:+ artifact in your module's build.gradle file. For example:

dependencies {
 implementation "com.google.android.gms:play-services-games-v2:+"
}

Initialize the SDK

Initialize the Play Games SDK in the onCreate(..) callback of your Application class. For example:

import com.google.android.gms.games.PlayGamesSdk;

...

@Override
public void onCreate(){
  super.onCreate();
  PlayGamesSdk.initialize(this);
}

Remove sign-in and sign-out calls

If you specify no extra scopes, migrating your use case should be straightforward.

  1. Remove sign-in calls using the GoogleSignIn API. Sign-in will always be performed at game launch. Instead, listen to the result of the automatic sign-in attempt using GamesSignInClient.isAuthenticated().

    GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(getActivity());
    
    gamesSignInClient.isAuthenticated().addOnCompleteListener(isAuthenticatedTask -> {
      boolean isAuthenticated =
        (isAuthenticatedTask.isSuccessful() &&
         isAuthenticatedTask.getResult().isAuthenticated());
    
      if (isAuthenticated) {
        // Continue with Play Games Services
      } else {
        // Disable your integration with Play Games Services or show a
        // login button to ask  players to sign-in. Clicking it should
        // call GamesSignInClient.signIn().
      }
    });
    
  2. Remove all calls to sign out, as account management is now contained within the OS and Play Games app settings.

  3. If the player is successfully signed in, remove the Play Games sign-in button from your game. If the user chooses not to sign in when the game launches, you may optionally choose to continue showing a button with the Play Games icon, and trigger the login process with GamesSignInClient.signIn().

  4. After verifying that the user is signed-in, you can retrieve the Player ID to identify the user.

    PlayGames.getPlayersClient(activity).getCurrentPlayer().addOnCompleteListener(mTask -> {
        // Get PlayerID with mTask.getResult().getPlayerId()
      }
    );
    
  5. Remove your dependency on the GoogleSignIn API, if no longer in use.

Update client class names

When creating clients (e.g. LeaderboardsClient or AchievementsClient) use PlayGames.getFooClient() rather than Games.getFooClient().

Request server side access

When requesting server side access use GamesSignInClient.requestServerSideAccess() rather than GoogleSignInAccount.getServerAuthCode().

GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(this);
gamesSignInClient
  .requestServerSideAccess(OAUTH_2_WEB_CLIENT_ID,
                           /*forceRefreshToken=*/ false)
  .addOnCompleteListener( task -> {
    if (task.isSuccessful()) {
      String serverAuthToken = task.getResult();
      // Send authentication code to the backend game server to be
      // exchanged for an access token and used to verify the
      // player with the Play Games Services REST APIs.
    } else {
      // Failed to retrieve authentication code.
    }
});

Remove extra scopes

With Play Games Services v2 you cannot request any additional scopes. If you still need to request additional scopes, then we recommend you use the Google Sign In SDK alongside Play Games Services.

Migration from GoogleApiClient

For older existing integrations your game may be depending on the GoogleApiClient API variation of the Play Games Services SDK. This was deprecated in late 2017 and replaced by "connectionless" clients. To migrate you can replace the GoogleApiClient class with a “connectionless” equivalent. You will then also have to follow the guidance on this page to migrate your game from v1 to v2. Here is a mapping of common classes:

com.google.android.gms.games.achievement.Achievements ->
    com.google.android.gms.games.AchievementsClient

com.google.android.gms.games.leaderboard.Leaderboard ->
    com.google.android.gms.games.LeaderboardsClient

com.google.android.gms.games.snapshot.Snapshots ->
    com.google.android.gms.games.SnapshotsClient

com.google.android.gms.games.stats.PlayerStats ->
    com.google.android.gms.games.PlayerStatsClient

com.google.android.gms.games.Players ->
    com.google.android.gms.games.PlayersClient

com.google.android.gms.games.GamesStatusCodes ->
    com.google.android.gms.games.GamesClientStatusCodes