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 with Play Games Services Sign In v2

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

Add the Play Games Services SDK 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 depency as follows:

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

Define the Play Games Services 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()
  }
);