addApi(Games.API)已废弃,无替代提示且移除后报IllegalArgumentException求助
Games.API & IllegalArgumentException in Google Play Games Services Hey there! I totally get the frustration here—dealing with deprecated APIs that break when you try to remove them is the worst. Let's walk through how to replace that old GoogleApiClient setup with the modern Play Games Services SDK, which will fix both the deprecation warning and the IllegalArgumentException.
Why You're Seeing This Issue
The old GoogleApiClient approach and Games.API have been fully deprecated by Google. When you remove Games.API, the client doesn't know which Play Games features to initialize, hence the IllegalArgumentException. The solution is to switch to the modular client pattern that Google now recommends for Play Games Services.
Step 1: Ensure You're Using the Latest Dependency
First, make sure your app's module-level build.gradle has the most recent Play Games Services dependency:
implementation 'com.google.android.gms:play-services-games:23.1.0'
Feel free to check for the latest version if this is outdated, but this should work for most use cases.
Step 2: Replace the Deprecated GoogleApiClient Code
Instead of building a GoogleApiClient, we'll use Games.getSignInClient() to handle authentication and access to Play Games features. Here's the full replacement code that mirrors your original logic:
// Initialize the Play Games Sign-In Client GamesSignInClient gamesSignInClient = Games.getSignInClient(this); // Launch the sign-in flow and handle results gamesSignInClient.signIn() .addOnSuccessListener(unused -> { // Successfully connected to Play Games Services Log.d(TAG, "Connected to Play Games Services"); }) .addOnFailureListener(e -> { // Match your original failure handling logic Log.e(TAG, "Could not connect to Play games services", e); finish(); });
Step 3: Advanced Sign-In Control (Optional)
If you need more granular control over the sign-in flow (like checking if the user is already signed in), use the Activity Result API instead of listeners:
First, define the result contract in your Activity/Fragment:
private final ActivityResultLauncher<Intent> signInLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == Activity.RESULT_OK) { Task<SignInAccount> accountTask = Games.getSignInClient(this).getCurrentAccount(); accountTask.addOnSuccessListener(account -> { Log.d(TAG, "Connected with account: " + account.getDisplayName()); }); } else { Log.e(TAG, "Could not connect to Play games services"); finish(); } } );
Then trigger the sign-in flow when needed:
GamesSignInClient gamesSignInClient = Games.getSignInClient(this); signInLauncher.launch(gamesSignInClient.getSignInIntent());
Key Notes
- The modern SDK handles connection management behind the scenes, so you don't need
enableAutoManage()anymore. - All Play Games features (leaderboards, achievements, etc.) now use dedicated clients like
Games.getLeaderboardsClient()orGames.getAchievementsClient()instead of relying on the oldGoogleApiClient. - This approach eliminates the deprecation warning and fixes the
IllegalArgumentExceptionby explicitly initializing the Play Games sign-in flow.
内容的提问来源于stack exchange,提问作者Ventura De Lucas Sánche

