Migrar para os serviços relacionados a jogos do Google Play v2 (Java ou Kotlin)

Este documento descreve como migrar jogos do games v1 SDK para o games v2 SDK.

Antes de começar

Você pode usar qualquer ambiente de desenvolvimento integrado de sua preferência, como o Android Studio, para migrar seu jogo. Siga estas etapas antes de migrar para a versão 2 dos jogos:

  • Baixar e instalar o Android Studio
  • Seu jogo precisa usar a v1 do SDK dos serviços relacionados a jogos do Google Play.
  • Você pode fazer upgrade do seu jogo para usar o SDK games v1 para com.google.android.gms:play-services-games:24.0.0. Não faça upgrade para a versão com.google.android.gms:play-services-games:25.0.0 porque a API v1 dos serviços relacionados a jogos foi removida.

Atualizar as dependências

  1. No arquivo build.gradle do módulo, encontre esta linha nas dependências no nível do módulo.

    implementation "com.google.android.gms:play-services-games:+"

    Substitua por este código:

    implementation "com.google.android.gms:play-services-games-v2:version"

    Substitua version pela versão mais recente do SDK de jogos.

  2. Depois de atualizar as dependências, conclua todas as etapas deste documento.

Definir o ID do projeto

Para adicionar o ID do projeto do SDK dos serviços relacionados a jogos do Google Play ao seu app, siga estas etapas:

  1. No arquivo AndroidManifest.xml, adicione o seguinte elemento <meta-data> e atributos ao elemento <application>:

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

    Defina a referência @string/game_services_project_id do recurso de string usando o ID do projeto do jogo nos serviços relacionados a jogos como o valor. O ID do projeto nos serviços relacionados a jogos pode ser encontrado abaixo do nome do jogo na página Configuração do Google Play Console.

  2. No arquivo res/values/strings.xml, adicione uma referência de recurso de string e defina o ID do projeto como o valor. Exemplo:

    <!-- 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>
    

Caminhos de migração

O caminho de migração correto para seu jogo depende de como ele implementa os serviços do Google Play Games v1 e processa a identidade do jogador. Para garantir uma transição tranquila e evitar a perda de dados do jogador, identifique o cenário que melhor corresponde à sua configuração atual e siga as etapas correspondentes.

Opção 1: para jogos em que o IGA está vinculado ao ID de jogador dos serviços relacionados a jogos do Google Play

Esse cenário se aplica a jogos que usaram o Player ID dos serviços relacionados a jogos do Google Play como o único identificador da conta no jogo (IGA, na sigla em inglês) de um jogador e que não solicitaram ou armazenaram um OpenID anteriormente. O principal desafio é vincular o IGA existente a um identificador principal (o OpenID) sem perder a conexão com o progresso do jogador.

O fluxo de migração inclui as seguintes etapas:

  1. Quando o jogo é iniciado, o SDK dos serviços do Google Play Games v2 autentica automaticamente e de forma silenciosa a plataforma.
  2. Apresente uma tela de login com um botão Fazer login com o Google, substituindo o botão do Google Play. Por exemplo, consulte CredManBridge.java.

    CredManBridge.java
    
    package com.wickedcube.trivialkart;
    import android.accounts.Account;
    import android.content.Context;
    import android.util.Log;
    import android.os.CancellationSignal;
    import androidx.credentials.CredentialManager;
    import androidx.credentials.GetCredentialRequest;
    import androidx.credentials.GetCredentialResponse;
    import androidx.credentials.exceptions.GetCredentialException;
    import androidx.credentials.exceptions.NoCredentialException;
    import com.google.android.libraries.identity.googleid.GetGoogleIdOption;
    import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
    import com.google.android.gms.auth.api.identity.AuthorizationClient;
    import com.google.android.gms.auth.api.identity.AuthorizationRequest;
    import com.google.android.gms.auth.api.identity.AuthorizationResult;
    import com.google.android.gms.common.api.ApiException;
    import com.google.android.gms.auth.api.identity.Identity;
    import com.google.android.gms.common.api.Scope;
    import com.unity3d.player.UnityPlayer;
    import java.util.Collections;
    import java.util.List;
    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;

    public class CredManBridge {

    // --- MODE 1: SILENT SIGN-IN (Called on Awake) --- // Tries to auto-select an authorized account. If it fails, it does NOT show UI. public static void signInSilent(Context context, String webClientId) { CredentialManager credentialManager = CredentialManager.create(context); CancellationSignal cancellationSignal = new CancellationSignal(); Executor executor = Executors.newSingleThreadExecutor();

    Log.d("CredMan", "Attempting Silent Sign-In...");
    
    GetGoogleIdOption silentOption = new GetGoogleIdOption.Builder()
        .setFilterByAuthorizedAccounts(true) // Strict: Only authorized accounts
        .setServerClientId(webClientId)
        .setAutoSelectEnabled(true)          // Auto-select if possible
        .build();
    
    GetCredentialRequest silentRequest = new GetCredentialRequest.Builder()
        .addCredentialOption(silentOption)
        .build();
    
    credentialManager.getCredentialAsync(
        context,
        silentRequest,
        cancellationSignal,
        executor,
        new androidx.credentials.CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
            @Override
            public void onResult(GetCredentialResponse result) {
                Log.d("CredMan", "Silent Sign-In Successful!");
                handleSignInResult(context, result, webClientId);
            }
    
            @Override
            public void onError(GetCredentialException e) {
                // Send a specific error code so Unity knows to just stay on the Start Screen
                Log.d("CredMan", "Silent sign-in failed. Keeping UI hidden.");
                UnityPlayer.UnitySendMessage("AuthManager", "OnSignInError", "SilentFailed");
            }
        }
    );
    

    }

    // --- MODE 2: INTERACTIVE SIGN-IN (Called on Button Click) --- // Forces the Account Selection / "Add Account" sheet to appear. public static void signInInteractive(Context context, String webClientId) { CredentialManager credentialManager = CredentialManager.create(context); CancellationSignal cancellationSignal = new CancellationSignal(); Executor executor = Executors.newSingleThreadExecutor();

    Log.d("CredMan", "Starting Interactive Sign-In...");
    
    GetGoogleIdOption interactiveOption = new GetGoogleIdOption.Builder()
        .setFilterByAuthorizedAccounts(false) // Show ALL accounts (and "Add Account")
        .setServerClientId(webClientId)
        .setAutoSelectEnabled(false)          // Force the UI to show
        .build();
    
    GetCredentialRequest interactiveRequest = new GetCredentialRequest.Builder()
        .addCredentialOption(interactiveOption)
        .build();
    
    credentialManager.getCredentialAsync(
        context,
        interactiveRequest,
        cancellationSignal,
        executor,
        new androidx.credentials.CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
            @Override
            public void onResult(GetCredentialResponse result) {
                Log.d("CredMan", "Interactive Sign-In Successful!");
                handleSignInResult(context, result, webClientId);
            }
    
            @Override
            public void onError(GetCredentialException e) {
                Log.e("CredMan", "Interactive Sign-In Canceled or Failed", e);
                UnityPlayer.UnitySendMessage("AuthManager", "OnSignInError", "Canceled");
            }
        }
    );
    

    }

    private static void handleSignInResult(Context context, GetCredentialResponse result, String webClientId) { try { GoogleIdTokenCredential credential = GoogleIdTokenCredential.createFrom(result.getCredential().getData()); String email = credential.getId();

        Account account = new Account(email, "com.google");
        // Requesting GAMES_LITE scope to check for pre-existing V1 grants
        List<Scope> requestedScopes = Collections.singletonList(new Scope("https://www.googleapis.com/auth/games_lite"));
    
        AuthorizationRequest authRequest = new AuthorizationRequest.Builder()
            .setRequestedScopes(requestedScopes)
            .setAccount(account)
            .requestOfflineAccess(webClientId)
            .build();
    
        AuthorizationClient authClient = Identity.getAuthorizationClient(context);
    
        authClient.authorize(authRequest)
            .addOnSuccessListener(authorizationResult -> {
                if (authorizationResult.getServerAuthCode() != null) {
                    // CASE 1: RETURNING USER (Success)
                    // The user has already granted GAMES_LITE in the past.
                    // We got the code directly without showing UI.
                    Log.i("CredMan", "PGS v1: Existing grant found. Returning user detected. Auth Code retrieved.");
                    UnityPlayer.UnitySendMessage("AuthManager", "OnSignInSuccess", authorizationResult.getServerAuthCode());
                }
                else if (authorizationResult.hasResolution()) {
                    // CASE 2: NEW USER (PendingIntent)
                    // The user has NOT granted GAMES_LITE before. The API returned a PendingIntent
                    // (authorizationResult.getPendingIntent()) to show the consent screen.
                    // As per your flow, we DISCARD this intent and do not show UI.
                    Log.i("CredMan", "PGS v1: No existing grant (PendingIntent returned). This is a NEW user or they revoked access.");
                    Log.i("CredMan", "PGS v1: Discarding PendingIntent. Proceeding as New User.");
    
                    // Notify Unity that this is a "New User" so it can trigger V2 logic instead of failing
                    UnityPlayer.UnitySendMessage("AuthManager", "OnSignInError", "NewUser_NoGrant");
                }
                else {
                    // Edge Case: No code and no resolution?
                    Log.e("CredMan", "PGS v1: Authorization success but no Auth Code or Resolution returned.");
                    UnityPlayer.UnitySendMessage("AuthManager", "OnSignInError", "No Auth Code returned");
                }
            })
            .addOnFailureListener(e -> {
                // CASE 3: GENERIC FAILURE
                Log.e("CredMan", "PGS v1: Authorization failed completely.", e);
                UnityPlayer.UnitySendMessage("AuthManager", "OnSignInError", "Authorization Failed: " + e.getMessage());
            });
    
    } catch (Exception e) {
        UnityPlayer.UnitySendMessage("AuthManager", "OnSignInError", "Parsing Error: " + e.getMessage());
    }
    

    } }

  3. Recupere dois identificadores distintos quando o jogador tocar no botão Fazer login com o Google e selecionar uma Conta do Google:

    • O OpenID, que é o identificador principal para vincular o IGA.
    • O Player ID dos serviços relacionados a jogos do Google Play, recuperado usando o escopo GAMES_LITE, para procurar o IGA do jogador no sistema de back-end e realizar a vinculação. Para mais informações, consulte Recuperar Player ID.
  4. Acesse o IGA pelo fluxo Fazer login com o Google em inicializações de jogos subsequentes, sem exigir que os jogos usem Player ID como um identificador principal.

Recuperar Player ID

Você pode realizar a etapa 3 usando uma implementação do lado do cliente do jogo.

  1. Chame a API Android Credential Manager para fazer login do usuário com uma Conta do Google.
  2. Depois que o usuário concluir o fluxo do recurso Fazer Login com o Google e selecionar uma Conta do Google, receba um objeto de resultado contendo o token de ID e o endereço de e-mail.
  3. Crie um objeto Account com base no endereço de e-mail.
  4. Chame a API Authorization com o escopo GAMES_LITE e a conta.
  5. Se a conta tiver uma concessão preexistente no escopo GAMES_LITE, a API Authorization vai retornar um token diretamente no objeto de resposta:
    1. Use o token de resposta para chamar os servidores dos serviços relacionados a jogos do Google Play e recuperar o Player ID desses serviços.
    2. Verifique se o Player ID dos serviços do Google Play Games está vinculado a uma conta no jogo.
      1. Isso indica um usuário recorrente dos serviços do Google Play Games v1.
    3. Vincule o novo ID da Conta do Google à conta anterior dos serviços do Google Play Games v1.
  6. Se a conta não tiver uma concessão preexistente no escopo GAMES_LITE, a API Authorization vai retornar um PendingIntent:
    1. Isso indica que o usuário não tem uma conta dos serviços do Google Play Games v1.
    2. Descarte o PendingIntent com segurança sem mostrar nenhuma interface.

Opção 2: para jogos que já vinculam o IGA ao OpenID

Os desenvolvedores desse grupo têm o caminho de migração mais simples. Se a conta do jogo já estiver vinculada principalmente ao OpenID, basta realizar a migração técnica padrão do SDK da v1 para a v2, conforme descrito nas etapas.

Migrar do Login do Google descontinuado

Substitua a classe GoogleSignInClient pela classe GamesSignInClient.

Java

Localize os arquivos com a classe GoogleSignInClient.

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;

// ... existing code

@Override
public void onCreate(@Nullable Bundle bundle) {
    super.onCreate(bundle);

    // ... existing code

    GoogleSignInOptions signInOption =
        new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build();
    
    // Client used to sign in to Google services
    GoogleSignInClient googleSignInClient =
        GoogleSignIn.getClient(this, signInOptions);
}

E atualize para o seguinte:

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

// ... existing code

@Override
public void onCreate(){
    super.onCreate();
    PlayGamesSdk.initialize(this);
    // Client used to sign in to Google services
    GamesSignInClient gamesSignInClient =
        PlayGames.getGamesSignInClient(getActivity());
}

Kotlin

Localize os arquivos com a classe GoogleSignInClient.

import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions

// ... existing code

val signInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN

// ... existing code

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val googleSignInClient: GoogleSignInClient =
        GoogleSignIn.getClient(this, signInOptions)
}

E atualize para o seguinte:

import com.google.android.gms.games.PlayGames
import com.google.android.gms.games.PlayGamesSdk
import com.google.android.gms.games.GamesSignInClient

// ... existing code

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    PlayGamesSdk.initialize(this)
    // client used to sign in to Google services
    val gamesSignInClient: GamesSignInClient =
        PlayGames.getGamesSignInClient(this)
}

Atualizar o código GoogleSignIn

A API GoogleSignIn não é compatível com o SDK games v2. Substitua o código da API GoogleSignIn pela API GamesSignInClient, conforme mostrado no exemplo a seguir.

Para solicitar um token de acesso do lado do servidor, use o método GamesSignInClient.requestServerSideAccess(). Para mais informações, consulte Atualizar as classes de acesso do lado do servidor.

Java

Localize os arquivos com a classe GoogleSignIn.

// Request code used when invoking an external activity.
private static final int RC_SIGN_IN = 9001;

private boolean isSignedIn() {
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    GoogleSignInOptions signInOptions =
    GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN;
    return GoogleSignIn.hasPermissions(account, signInOptions.getScopeArray());
}

private void signInSilently() {
    GoogleSignInOptions signInOptions =
        GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN;
    GoogleSignInClient signInClient = GoogleSignIn.getClient(this, signInOptions);
    signInClient
        .silentSignIn()
        .addOnCompleteListener(
            this,
            task -> {
            if (task.isSuccessful()) {
                // The signed-in account is stored in the task's result.
                GoogleSignInAccount signedInAccount = task.getResult();
                showSignInPopup();
            } else {
                // Perform interactive sign in.
                startSignInIntent();
            }
        });
}

private void startSignInIntent() {
    GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
        GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    Intent intent = signInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result =
        Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // The signed-in account is stored in the result.
            GoogleSignInAccount signedInAccount = result.getSignInAccount();
            showSignInPopup();
        } else {
            String message = result.getStatus().getStatusMessage();
            if (message == null || message.isEmpty()) {
                message = getString(R.string.signin_other_error);
        }
        new AlertDialog.Builder(this).setMessage(message)
            .setNeutralButton(android.R.string.ok, null).show();
        }
    }
}

private void showSignInPopup() {
Games.getGamesClient(requireContext(), signedInAccount)
    .setViewForPopups(contentView)
    .addOnCompleteListener(
        task -> {
            if (task.isSuccessful()) {
                logger.atInfo().log("SignIn successful");
            } else {
                logger.atInfo().log("SignIn failed");
            }
        });
  }

E atualize para o seguinte:

private void signInSilently() {
    gamesSignInClient.isAuthenticated().addOnCompleteListener(isAuthenticatedTask -> {
    boolean isAuthenticated =
        (isAuthenticatedTask.isSuccessful() &&
            isAuthenticatedTask.getResult().isAuthenticated());
        if (isAuthenticated) {
            // Continue with Play Games Services
        } else {
            // If authentication fails, either disable Play Games Services
            // integration or
            // display a login button to prompt players to sign in.
            // Use`gamesSignInClient.signIn()` when the login button is clicked.
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    // When the activity is inactive, the signed-in user's state can change;
    // therefore, silently sign in when the app resumes.
    signInSilently();
}

Kotlin

Localize os arquivos com a classe GoogleSignIn.

// Request codes we use when invoking an external activity.
private val RC_SIGN_IN = 9001

// ... existing code

private fun isSignedIn(): Boolean {
    val account = GoogleSignIn.getLastSignedInAccount(this)
    val signInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN
    return GoogleSignIn.hasPermissions(account, *signInOptions.scopeArray)
}

private fun signInSilently() {
    val signInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN
    val signInClient = GoogleSignIn.getClient(this, signInOptions)
    signInClient.silentSignIn().addOnCompleteListener(this) { task ->
        if (task.isSuccessful) {
            // The signed-in account is stored in the task's result.
            val signedInAccount = task.result
            // Pass the account to showSignInPopup.
            showSignInPopup(signedInAccount)
        } else {
            // Perform interactive sign in.
            startSignInIntent()
        }
    }
}

private fun startSignInIntent() {
    val signInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
    val intent = signInClient.signInIntent
    startActivityForResult(intent, RC_SIGN_IN)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == RC_SIGN_IN) {
        val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
        if (result.isSuccess) {
            // The signed-in account is stored in the result.
            val signedInAccount = result.signInAccount
            showSignInPopup(signedInAccount) // Pass the account to showSignInPopup.
        } else {
            var message = result.status.statusMessage
            if (message == null || message.isEmpty()) {
                message = getString(R.string.signin_other_error)
        }
        AlertDialog.Builder(this)
            .setMessage(message)
            .setNeutralButton(android.R.string.ok, null)
            .show()
        }
    }
}

private fun showSignInPopup(signedInAccount: GoogleSignInAccount) {
    // Add signedInAccount parameter.
    Games.getGamesClient(this, signedInAccount)
        .setViewForPopups(contentView) // Assuming contentView is defined.
        .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            logger.atInfo().log("SignIn successful")
        } else {
            logger.atInfo().log("SignIn failed")
        }
    }
}

E atualize para o seguinte:

private fun signInSilently() {
    gamesSignInClient.isAuthenticated.addOnCompleteListener { isAuthenticatedTask ->
        val isAuthenticated = isAuthenticatedTask.isSuccessful &&
        isAuthenticatedTask.result.isAuthenticated
        if (isAuthenticated) {
            // Continue with Play Games Services
        } else {
            // To handle a user who is not signed in, either disable Play Games Services integration
            // or display a login button. Selecting this button calls `gamesSignInClient.signIn()`.
        }
    }
}

override fun onResume() {
    super.onResume()
    // Since the state of the signed in user can change when the activity is
    // not active it is recommended to try and sign in silently from when the
    // app resumes.
    signInSilently()
}

Adicionar o código GamesSignInClient

Se o jogador for autenticado, remova o botão de login dos serviços do Google Play Games do seu jogo. Se o usuário optar por não fazer a autenticação quando o jogo for iniciado, continue mostrando um botão com o ícone dos serviços do Google Play Games e inicie o processo de login com GamesSignInClient.signIn().

Java

private void startSignInIntent() {
    gamesSignInClient
        .signIn()
        .addOnCompleteListener( task -> {
            if (task.isSuccessful() && task.getResult().isAuthenticated()) {
                // sign in successful
            } else {
                // sign in failed
            }
        });
  }

Kotlin

private fun startSignInIntent() {
    gamesSignInClient
        .signIn()
        .addOnCompleteListener { task ->
            if (task.isSuccessful && task.result.isAuthenticated) {
                // sign in successful
            } else {
                // sign in failed
            }
        }
  }

Remover código de saída

Remova o código de GoogleSignInClient.signOut.

Remova o código mostrado no exemplo a seguir:

Java

// ... existing code

private void signOut() {
    GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
    GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    signInClient.signOut().addOnCompleteListener(this,
    new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
           // At this point, the user is signed out.
        }
    });
}

Kotlin

// ... existing code

private fun signOut() {
    val signInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
    signInClient.signOut().addOnCompleteListener(this) {
    // At this point, the user is signed out.
    }
}

Verificar se a autenticação foi concluída

Inclua o código a seguir para verificar se você foi autenticado automaticamente e adicione a lógica personalizada, se estiver disponível.

Java

private void checkIfAutomaticallySignedIn() {
gamesSignInClient.isAuthenticated().addOnCompleteListener(isAuthenticatedTask -> {
boolean isAuthenticated =
    (isAuthenticatedTask.isSuccessful() &&
    isAuthenticatedTask.getResult().isAuthenticated());

    if (isAuthenticated) {
        // Continue with Play Games Services
        // If your game requires specific actions upon successful sign-in,
        // you can add your custom logic here.
        // For example, fetching player data or updating UI elements.
    } else {
        // Show a login button to ask  players to sign-in. Clicking it should
        // call GamesSignInClient.signIn().
        }
    });
}

Kotlin

private fun checkIfAutomaticallySignedIn() {
gamesSignInClient.isAuthenticated()
    .addOnCompleteListener { task ->
    val isAuthenticated = task.isSuccessful && task.result?.isAuthenticated ?: false

        if (isAuthenticated) {
            // Continue with Play Games Services
        } else {
            // Disable your integration or show a login button
        }
    }
}

Atualizar nomes e métodos de classes do cliente

Ao migrar para a versão 2 dos jogos, os métodos usados para receber os nomes das classes de cliente são diferentes. Use os métodos PlayGames.getxxxClient() correspondentes em vez dos métodos Games.getxxxClient().

Por exemplo, para LeaderboardsClient, use PlayGames.getLeaderboardsClient() em vez do método Games.getLeaderboardsClient().

Remova qualquer código relacionado às classes GamesClient e GamesMetadataClient, porque não há classes de substituição na v2 dos serviços relacionados a jogos.

Java

Localize o código de LeaderboardsClient.

import com.google.android.gms.games.LeaderboardsClient;
import com.google.android.gms.games.Games;

@Override
public void onCreate(@Nullable Bundle bundle) {
    super.onCreate(bundle);
        // Get the leaderboards client using Play Games services.
    LeaderboardsClient leaderboardsClient = Games.getLeaderboardsClient(this,
        GoogleSignIn.getLastSignedInAccount(this));
}

E atualize para o seguinte:

import com.google.android.gms.games.LeaderboardsClient;
import com.google.android.gms.games.PlayGames;

 @Override
public void onCreate(@Nullable Bundle bundle) {
    super.onCreate(bundle);
        // Get the leaderboards client using Play Games services.
        LeaderboardsClient leaderboardsClient = PlayGames.getLeaderboardsClient(getActivity());
}

Kotlin

Localize o código de LeaderboardsClient.

import com.google.android.gms.games.LeaderboardsClient
import com.google.android.gms.games.Games
// Initialize the variables.
private lateinit var leaderboardsClient: LeaderboardsClient

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    leaderboardsClient = Games.getLeaderboardsClient(this,
        GoogleSignIn.getLastSignedInAccount(this))
}

E atualize para o seguinte:

import com.google.android.gms.games.LeaderboardsClient
import com.google.android.gms.games.PlayGames
    // Initialize the variables.
private lateinit var leaderboardsClient: LeaderboardsClient

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    leaderboardsClient = PlayGames.getLeaderboardsClient(this)
}

Da mesma forma, use os métodos correspondentes para os seguintes clientes: AchievementsClient, EventsClient, GamesSignInClient, PlayerStatsClient, RecallClient, SnapshotsClient ou PlayersClient.

Atualizar as classes de acesso do lado do servidor

Para solicitar um token de acesso do lado do servidor, use o método GamesSignInClient.requestServerSideAccess() em vez do método GoogleSignInAccount.getServerAuthCode().

Para mais informações, consulte Enviar o código de autenticação do servidor.

O exemplo a seguir mostra como solicitar um token de acesso do lado do servidor.

Java

Localize o código da classe GoogleSignInOptions.

    private static final int RC_SIGN_IN = 9001;
    private GoogleSignInClient googleSignInClient;

    private void startSignInForAuthCode() {
        /** Client ID for your backend server. */
        String webClientId = getString(R.string.webclient_id);
        GoogleSignInOptions signInOption = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
            .requestServerAuthCode(webClientId)
            .build();

        GoogleSignInClient signInClient = GoogleSignIn.getClient(this, signInOption);
        Intent intent = signInClient.getSignInIntent();
        startActivityForResult(intent, RC_SIGN_IN);
    }

    /** Auth code to send to backend server */
    private String mServerAuthCode;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            mServerAuthCode = result.getSignInAccount().getServerAuthCode();
        } else {
            String message = result.getStatus().getStatusMessage();
            if (message == null || message.isEmpty()) {
                message = getString(R.string.signin_other_error);
            }
            new AlertDialog.Builder(this).setMessage(message)
                .setNeutralButton(android.R.string.ok, null).show();
        }
      }
    }
  

E atualize para o seguinte:

  private void startRequestServerSideAccess() {
      GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(this);
      gamesSignInClient
          .requestServerSideAccess(OAUTH_2_WEB_CLIENT_ID,
           /* forceRefreshToken= */ false, /* additional AuthScope */ scopes)
          .addOnCompleteListener(task -> {
              if (task.isSuccessful()) {
                  AuthResponse authresp = task.getResult();
                  // Send the authorization code as a string and a
                  // list of the granted AuthScopes that were granted by the
                  // user. Exchange for an access token.
                  // Verify the player with Play Games Services REST APIs.
              } else {
                // Authentication code retrieval failed.
              }
        });
  }
  

Kotlin

Localize o código da classe GoogleSignInOptions.

  // ... existing code

  private val RC_SIGN_IN = 9001
  private lateinit var googleSignInClient: GoogleSignInClient

  // Auth code to send to backend server.
  private var mServerAuthCode: String? = null

  private fun startSignInForAuthCode() {
      // Client ID for your backend server.
      val webClientId = getString(R.string.webclient_id)

      val signInOption = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
          .requestServerAuthCode(webClientId)
          .build()

      googleSignInClient = GoogleSignIn.getClient(this, signInOption)
      val intent = googleSignInClient.signInIntent
      startActivityForResult(intent, RC_SIGN_IN)
  }

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if (requestCode == RC_SIGN_IN) {
          val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
          if (result.isSuccess) {
              mServerAuthCode = result.signInAccount.serverAuthCode
          } else {
              var message = result.status.statusMessage
              if (message == null || message.isEmpty()) {
                  message = getString(R.string.signin_other_error)
              }
              AlertDialog.Builder(this).setMessage(message)
                  .setNeutralButton(android.R.string.ok, null).show()
            }
        }
  }
  

E atualize para o seguinte:

  private fun startRequestServerSideAccess() {
      val gamesSignInClient = PlayGames.getGamesSignInClient(this)
      gamesSignInClient
          .requestServerSideAccess(
              OAUTH_2_WEB_CLIENT_ID,
              /* forceRefreshToken= */ false,
              /* additional AuthScope */ scopes
          )
          .addOnCompleteListener { task ->
              if (task.isSuccessful) {
                  val authresp: AuthResponse = task.result
                  // Send the authorization code as a string and a
                  // list of the granted AuthScopes that were granted by the
                  // user. Exchange for an access token.
                  // Verify the player with Play Games Services REST APIs.
              } else {
                  // Authentication code retrieval failed.
              }
          }
  }
  

Migrar do GoogleApiClient

Para integrações anteriores, seu jogo pode depender da variação da API GoogleApiClient do SDK dos serviços relacionados a jogos do Google Play. Ela foi descontinuada no final de 2017 e substituída por clientes "sem conexão". Para migrar, substitua a classe GoogleApiClient por uma equivalente "sem conexão". A tabela a seguir lista os mapeamentos de classe comuns do games v1 para o games v2:

games v2 (atual) games v1 (legado)
com.google.android.gms.games.AchievementsClient com.google.android.gms.games.achievement.Achievements
com.google.android.gms.games.LeaderboardsClient com.google.android.gms.games.leaderboard.Leaderboard
com.google.android.gms.games.SnapshotsClient com.google.android.gms.games.snapshot.Snapshots
com.google.android.gms.games.PlayerStatsClient com.google.android.gms.games.stats.PlayerStats
com.google.android.gms.games.PlayersClient com.google.android.gms.games.Players
com.google.android.gms.games.GamesClientStatusCodes com.google.android.gms.games.GamesStatusCodes

Criar e executar o jogo

Para criar e executar no Android Studio, consulte Criar e executar o app.

Testar o jogo

Teste o jogo para garantir que ele funcione conforme o esperado. Os testes realizados dependem dos recursos do jogo.

Confira a seguir uma lista de testes comuns para executar.

  1. Login bem-sucedido.

    1. O login automático funciona. O usuário precisa fazer login nos serviços do Google Play Games ao iniciar o jogo.

    2. O pop-up de boas-vindas é exibido.

      Exemplo de pop-up de boas-vindas.
      Exemplo de pop-up de boas-vindas (clique para ampliar).

    3. As mensagens de registro bem-sucedidas são exibidas. Execute o seguinte comando no terminal:

      adb logcat | grep com.google.android.

      Uma mensagem de registro bem-sucedida é mostrada no exemplo a seguir:

      [$PlaylogGamesSignInAction$SignInPerformerSource@e1cdecc
      number=1 name=GAMES_SERVICE_BROKER>], returning true for shouldShowWelcomePopup.
      [CONTEXT service_id=1 ]
  2. Garantir a consistência dos componentes da interface.

    1. Pop-ups, placares e conquistas são exibidos de forma correta e consistente em vários tamanhos e orientações de tela na interface do usuário (IU) dos serviços do Google Play Games.

    2. A opção de sair não aparece na interface dos serviços do Google Play Games.

    3. Verifique se é possível recuperar o ID do jogador e, se aplicável, se os recursos do lado do servidor funcionam conforme o esperado.

    4. Se o jogo usar a autenticação do lado do servidor, teste minuciosamente o fluxo de requestServerSideAccess. Verifique se o servidor recebe o código de autenticação e pode trocá-lo por um token de acesso. Teste cenários de sucesso e falha para erros de rede e cenários de client ID inválidos.

Se o jogo usava algum dos recursos a seguir, teste-os para garantir que funcionem da mesma forma que antes da migração:

  • Placares: envie pontuações e veja os placares. Verifique se a classificação e a exibição dos nomes e pontuações dos jogadores estão corretas.
  • Conquistas: desbloqueie conquistas e verifique se elas são registradas e exibidas corretamente na interface do Play Games.
  • Jogos salvos: se o jogo usa jogos salvos, verifique se o salvamento e o carregamento do progresso funcionam perfeitamente. Isso é especialmente importante para testes em vários dispositivos e após atualizações do app.

Tarefas pós-migração

Conclua as etapas a seguir depois de migrar para a versão 2 dos serviços relacionados a jogos.

Publicar o jogo

Crie os APKs e publique o jogo no Play Console.

  1. No menu do Android Studio, selecione Build > Build Bundles(s) / APK(s) > Build APK(s).
  2. Publicar o jogo. Para mais informações, consulte Publicar apps particulares no Play Console.