Di chuyển sang Dịch vụ trò chơi của Play phiên bản 2 (Java hoặc Kotlin)

Tài liệu này mô tả cách di chuyển các trò chơi hiện có từ SDK games phiên bản 1 sang SDK games phiên bản 2.

Trước khi bắt đầu

Bạn có thể sử dụng bất kỳ IDE nào mà bạn muốn, chẳng hạn như Android Studio, để di chuyển trò chơi của mình. Hãy hoàn tất các bước sau trước khi bạn di chuyển sang games phiên bản 2:

  • Tải xuống và cài đặt Android Studio
  • Trò chơi của bạn phải sử dụng SDK games phiên bản 1.
  • Bạn có thể nâng cấp trò chơi của mình để sử dụng SDK games phiên bản 1 thành com.google.android.gms:play-services-games:24.0.0. Bạn không nên nâng cấp lên com.google.android.gms:play-services-games:25.0.0 vì API games phiên bản 1 đã bị xoá.

Cập nhật các phần phụ thuộc

  1. Trong tệp build.gradle của mô-đun, hãy tìm dòng này trong các phần phụ thuộc ở cấp mô-đun.

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

    Thay thế bằng mã sau:

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

    Thay thế version bằng phiên bản mới nhất của SDK games.

  2. Sau khi bạn cập nhật các phần phụ thuộc, hãy đảm bảo bạn hoàn tất tất cả các bước trong tài liệu này.

Xác định mã dự án

Để thêm mã dự án SDK dịch vụ trò chơi của Play vào ứng dụng, hãy hoàn tất các bước sau:

  1. Trong tệp AndroidManifest.xml, hãy thêm phần tử <meta-data> và các thuộc tính sau vào phần tử <application>:

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

    Xác định tham chiếu tài nguyên chuỗi @string/game_services_project_id bằng cách sử dụng mã dự án Dịch vụ trò chơi của trò chơi làm giá trị. Bạn có thể tìm thấy mã dự án Dịch vụ trò chơi trong tên trò chơi tại trang Cấu hình trên Google Play Console.

  2. Trong tệp res/values/strings.xml, hãy thêm một tham chiếu tài nguyên chuỗi và đặt mã dự án làm giá trị. Ví dụ:

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

Đường dẫn di chuyển

Đường dẫn di chuyển chính xác cho trò chơi của bạn phụ thuộc vào cách trò chơi đó triển khai Dịch vụ trò chơi của Play phiên bản 1 và xử lý danh tính người chơi. Để đảm bảo quá trình chuyển đổi diễn ra suôn sẻ và ngăn ngừa tình trạng mất dữ liệu người chơi, hãy xác định tình huống phù hợp nhất với chế độ thiết lập hiện có của bạn và làm theo các bước tương ứng.

Lựa chọn 1: Đối với Trò chơi mà IGA được liên kết với Mã nhận dạng người chơi của Dịch vụ trò chơi của Play

Tình huống này áp dụng cho các trò chơi đã sử dụng Player ID của Dịch vụ trò chơi của Play làm giá trị nhận dạng duy nhất cho Tài khoản trong trò chơi (IGA) của người chơi và trước đây chưa yêu cầu hoặc lưu trữ OpenID. Thử thách chính là liên kết IGA hiện có với giá trị nhận dạng chính (OpenID) mà không làm mất kết nối với tiến trình của người chơi.

Quy trình di chuyển bao gồm các bước sau:

  1. Khi trò chơi khởi chạy, SDK Dịch vụ trò chơi của Play phiên bản 2 sẽ tự động và âm thầm xác thực nền tảng.
  2. Hiển thị màn hình đăng nhập có nút Đăng nhập bằng Google, thay thế nút Google Play. Ví dụ: hãy xem 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. Truy xuất hai giá trị nhận dạng riêng biệt khi người chơi nhấn vào nút Đăng nhập bằng Google và chọn một Tài khoản Google:

    • OpenID, đây là giá trị nhận dạng chính để liên kết IGA.
    • Player ID của Dịch vụ trò chơi của Play, được truy xuất bằng cách sử dụng phạm vi GAMES_LITE, để tra cứu IGA của người chơi trong hệ thống phụ trợ của bạn và thực hiện việc liên kết. Để biết thêm thông tin, hãy xem phần Truy xuất Player ID.
  4. Truy cập vào IGA bằng quy trình Đăng nhập bằng Google trong các lần khởi chạy trò chơi tiếp theo mà không yêu cầu trò chơi sử dụng Player ID làm giá trị nhận dạng chính.

Truy xuất Player ID

Bạn có thể thực hiện bước 3 bằng cách triển khai phía máy khách của trò chơi.

  1. Gọi API Trình quản lý thông tin đăng nhập Android để đăng nhập người dùng bằng Tài khoản Google.
  2. Sau khi người dùng hoàn tất quy trình Đăng nhập bằng Google và chọn một Tài khoản Google, hãy nhận một đối tượng kết quả chứa mã thông báo mã nhận dạng và địa chỉ email.
  3. Tạo một đối tượng Tài khoản từ địa chỉ email.
  4. Gọi API Uỷ quyền bằng phạm vi GAMES_LITE và Tài khoản.
  5. Nếu tài khoản có một cấp quyền hiện có trên phạm vi GAMES_LITE, thì API Uỷ quyền sẽ trả về trực tiếp một mã thông báo trong đối tượng phản hồi:
    1. Sử dụng mã thông báo phản hồi để gọi các máy chủ của Dịch vụ trò chơi của Play và truy xuất Player ID của Dịch vụ trò chơi của Play.
    2. Xác minh xem Player ID của Dịch vụ trò chơi của Play có được liên kết với một tài khoản trong trò chơi hay không.
      1. Điều này cho biết người dùng cũ đang quay lại từ Dịch vụ trò chơi của Play phiên bản 1.
    3. Liên kết mã nhận dạng gaia mới với tài khoản Dịch vụ trò chơi của Play phiên bản 1 trước đó.
  6. Nếu tài khoản không có một cấp quyền hiện có trên phạm vi GAMES_LITE, thì API Uỷ quyền sẽ trả về PendingIntent:
    1. Điều này cho biết người dùng không có tài khoản hiện có từ Dịch vụ trò chơi của Play phiên bản 1.
    2. Huỷ PendingIntent một cách an toàn mà không hiển thị bất kỳ giao diện người dùng nào.

Lựa chọn 2: Đối với Trò chơi đã liên kết IGA với OpenID

Nhà phát triển trong nhóm này có đường dẫn di chuyển đơn giản nhất. Nếu tài khoản trong trò chơi của trò chơi đã được liên kết chủ yếu với OpenID, thì bạn chỉ cần thực hiện quy trình di chuyển SDK kỹ thuật tiêu chuẩn từ phiên bản 1 sang phiên bản 2 như được nêu trong các bước.

Di chuyển từ tính năng Đăng nhập bằng Google không dùng nữa

Thay thế lớp GoogleSignInClient bằng lớp GamesSignInClient.

Java

Tìm các tệp có lớp 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);
}

Và cập nhật nó như sau:

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

Tìm các tệp có lớp 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)
}

Và cập nhật nó như sau:

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)
}

Cập nhật mã GoogleSignIn

GoogleSignIn API không được hỗ trợ trong SDK games phiên bản 2. Thay thế mã API GoogleSignIn bằng API GamesSignInClient như trong ví dụ sau.

Để yêu cầu mã truy cập phía máy chủ, hãy sử dụng phương thức GamesSignInClient.requestServerSideAccess(). Để biết thêm thông tin, hãy xem Cập nhật các lớp truy cập phía máy chủ.

Java

Tìm các tệp có lớp 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");
            }
        });
  }

Và cập nhật nó như sau:

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

Tìm các tệp có lớp 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")
        }
    }
}

Và cập nhật nó như sau:

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

Thêm mã GamesSignInClient

Nếu người chơi đã xác thực thành công, hãy xoá nút đăng nhập vào Dịch vụ trò chơi của Play khỏi trò chơi của bạn. Nếu người dùng chọn không xác thực khi trò chơi khởi chạy, hãy tiếp tục hiển thị một nút có biểu tượng Dịch vụ trò chơi của Play, và bắt đầu quy trình đăng nhập bằng 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
            }
        }
  }

Xoá mã đăng xuất

Xoá mã cho GoogleSignInClient.signOut.

Xoá mã trong ví dụ sau:

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.
    }
}

Kiểm tra xem quá trình xác thực có thành công hay không

Thêm mã sau để kiểm tra xem bạn đã tự động xác thực hay chưa và thêm logic tuỳ chỉnh nếu bạn có logic đó.

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 void 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
        }
    }
}

Cập nhật tên và phương thức của lớp ứng dụng

Khi bạn di chuyển sang games phiên bản 2, các phương thức dùng để lấy tên lớp ứng dụng sẽ khác nhau. Hãy sử dụng các phương thức PlayGames.getxxxClient() tương ứng thay vì các phương thức Games.getxxxClient().

Ví dụ: đối với LeaderboardsClient hãy sử dụng PlayGames.getLeaderboardsClient() thay vì phương thức Games.getLeaderboardsClient().

Xoá mọi mã liên quan đến các lớp GamesClientGamesMetadataClient vì chúng tôi không có lớp thay thế nào trong games phiên bản 2.

Java

Tìm mã cho 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));
}

Và cập nhật nó như sau:

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

Tìm mã cho 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))
}

Và cập nhật nó như sau:

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)
}

Tương tự, hãy sử dụng các phương thức tương ứng cho các ứng dụng sau: AchievementsClient, EventsClient, GamesSignInClient, PlayerStatsClient, RecallClient, SnapshotsClient hoặc PlayersClient.

Cập nhật các lớp truy cập phía máy chủ

Để yêu cầu mã truy cập phía máy chủ, hãy sử dụng phương thức GamesSignInClient.requestServerSideAccess() thay vì phương thức GoogleSignInAccount.getServerAuthCode().

Để biết thêm thông tin, hãy xem Gửi mã xác thực máy chủ.

Ví dụ sau đây cho thấy cách yêu cầu mã truy cập phía máy chủ.

Java

Tìm mã cho lớp 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();
        }
      }
    }
  

Và cập nhật nó như sau:

  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

Tìm mã cho lớp 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()
            }
        }
  }
  

Và cập nhật nó như sau:

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

Di chuyển từ GoogleApiClient

Đối với các chế độ tích hợp hiện có từ trước, có thể trò chơi của bạn đang phụ thuộc vào biến thể API GoogleApiClient của SDK Dịch vụ trò chơi của Play. Biến thể này đã ngừng hoạt động từ cuối năm 2017 và được thay thế bằng các ứng dụng "không có kết nối". Để di chuyển, bạn có thể thay thế lớp GoogleApiClient bằng một lớp tương đương "không có kết nối". Bảng sau đây liệt kê các bản đồ lớp phổ biến từ games phiên bản 1 sang games phiên bản 2:

games phiên bản 2 (Hiện tại) games phiên bản 1 (Cũ)
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

Tạo và chạy trò chơi

Để tạo và chạy trên Android Studio, hãy xem Tạo và chạy ứng dụng.

Kiểm thử trò chơi

Đảm bảo trò chơi của bạn hoạt động như thiết kế bằng cách kiểm thử trò chơi đó. Các bài kiểm thử mà bạn thực hiện phụ thuộc vào các tính năng của trò chơi.

Sau đây là danh sách các bài kiểm thử thường chạy.

  1. Đăng nhập thành công.

    1. Tính năng tự động đăng nhập hoạt động. Người dùng phải đăng nhập vào Dịch vụ trò chơi của Play khi khởi chạy trò chơi.

    2. Cửa sổ bật lên chào mừng sẽ xuất hiện.

      Cửa sổ bật lên chào mừng mẫu.
      Cửa sổ bật lên chào mừng mẫu (nhấp để phóng to).

    3. Thông báo nhật ký thành công sẽ xuất hiện. Chạy lệnh sau trong thiết bị đầu cuối:

      adb logcat | grep com.google.android.

      Thông điệp nhật ký thành công sẽ xuất hiện trong ví dụ sau:

      [$PlaylogGamesSignInAction$SignInPerformerSource@e1cdecc
      number=1 name=GAMES_SERVICE_BROKER>], returning true for shouldShowWelcomePopup.
      [CONTEXT service_id=1 ]
  2. Đảm bảo tính nhất quán của thành phần giao diện người dùng.

    1. Cửa sổ bật lên, bảng xếp hạng và thành tích hiển thị chính xác và nhất quán trên nhiều kích thước và hướng màn hình trong giao diện người dùng (UI) của Dịch vụ trò chơi của Play.

    2. Tuỳ chọn đăng xuất không xuất hiện trong giao diện người dùng của Dịch vụ trò chơi của Play.

    3. Đảm bảo bạn có thể truy xuất Mã nhận dạng người chơi thành công và nếu có thể, các chức năng phía máy chủ sẽ hoạt động như dự kiến.

    4. Nếu trò chơi sử dụng tính năng xác thực phía máy chủ, hãy kiểm thử kỹ quy trình requestServerSideAccess. Đảm bảo máy chủ nhận được mã uỷ quyền và có thể đổi mã đó để lấy mã truy cập. Kiểm thử cả tình huống thành công và không thành công đối với lỗi mạng, tình huống client ID không hợp lệ.

Nếu trò chơi của bạn đang sử dụng bất kỳ tính năng nào sau đây, hãy kiểm thử các tính năng đó để đảm bảo chúng hoạt động giống như trước khi di chuyển:

  • Bảng xếp hạng: Gửi điểm số và xem bảng xếp hạng. Kiểm tra thứ hạng chính xác và cách hiển thị tên và điểm số của người chơi.
  • Thành tích: Mở khoá thành tích và xác minh rằng thành tích được ghi lại chính xác và hiển thị trong giao diện người dùng của Play Games.
  • Trò chơi đã lưu: Nếu trò chơi sử dụng trò chơi đã lưu, hãy đảm bảo rằng việc lưu và tải tiến trình chơi hoạt động hoàn hảo. Điều này đặc biệt quan trọng để kiểm thử trên nhiều thiết bị và sau khi cập nhật ứng dụng.

Các việc cần làm sau khi di chuyển

Hoàn tất các bước sau khi bạn đã di chuyển sang games phiên bản 2.

Phát hành trò chơi

Tạo(các) tệp APK và phát hành trò chơi trong Play Console.

  1. Trong trình đơn Android Studio, hãy chọn Build > Build Bundles(s) / APK(s) > Build APK(s) (Tạo > Tạo gói/tệp APK > Tạo APK).
  2. Phát hành trò chơi của bạn Để biết thêm thông tin, hãy xem bài viết Phát hành ứng dụng riêng tư bằng Play Console.