Sign-in for Android games

In order to access Google Play Games Services functionality, your game needs to provide the signed-in player’s account. This documentation describes how to implement a seamless sign-in experience in your game.

The Play Games Services v2 SDK brings a number of improvements that increase the number of users signed into your game, and make development easier:

  • Improvements for users:
    • After selecting a default account, users are logged-in without needing to interact with a prompt.
    • Users no longer need to download the Play Games App to sign-in with Play Games Services or create a new account.
    • Users can now manage their Play Games Services accounts for multiple games from a single page.
  • Improvements for developers:
    • Client code no longer needs to handle the sign-in or sign-out flow, as login is automatically triggered when the game starts, and account management is handled in the OS settings.

New client integration

This section shows how to do a new client integration with Play Games Services Sign In v2.

Add the dependency

Add the Play Game Services SDK dependency to your app's root-level build.gradle file. If you are using Gradle, you can add or update the dependency as follows:

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

Define the project ID

To add the Play Games Services SDK project ID to your app, complete the following steps:

  1. In your app's AndroidManifest.xml file, add the following <meta-data> element and attributes to the <application> element:

    <manifest>
      <application>
        <meta-data android:name="com.google.android.gms.games.APP_ID"
                   android:value="@string/game_services_project_id"/>
      </application>
    </manifest>
    

    Define the String resource reference @string/game_services_project_id using your games’ Game services project id as the value. Your Games services project id can be found under your game name in the Configuration page on the Google Play Console.

  2. In your res/values/strings.xml file, add a string resource reference and set your project ID as the value. In Google Play Console, you can find your project ID under your game name in the Configuration page. For example:

    <!-- res/values/strings.xml -->
    <resources>
      <!-- Replace 0000000000 with your game’s project id. Example value shown above.  -->
      <string translatable="false"  name="game_services_project_id"> 0000000000 </string>
    </resources>
    

Initialize the SDK

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

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

...

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

Get the sign-in result

When your game launches, it will always attempt to sign in the user. To authenticate the user, you must verify that the user successfully signed in, and then get their Player ID.

To verify the sign in attempt, call GamesSignInClient.isAuthenticated() and use addOnCompleteListener to retrieve the results. For example:

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().
  }
});

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 attempt to sign in the user again by calling GamesSignInClient.signIn() if the user presses the button.

After verifying that the user is signed in, you can retrieve the Player ID to identify the user. For example:

PlayGames.getPlayersClient(activity).getCurrentPlayer().addOnCompleteListener(mTask -> {
    // Get PlayerID with mTask.getResult().getPlayerId()
  }
);

Migrate to Play Games Services Sign In v2

This section 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 via 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 above to migrate your game from v1 to v2. Below 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