LibGDX中Google Play游戏服务实现求助:BaseGameUtils已移除
Hey there, I’ve run into this exact issue before when updating my LibGDX projects—since Google deprecated and removed BaseGameUtils from their android-basic-samples, we need to use the official Google Play Games Services SDK v2 directly instead. It’s actually more flexible once you get the hang of it, so here’s a step-by-step breakdown to get it working:
1. Add Dependencies to Your Android Module
First, open your Android project’s build.gradle (the one inside the android folder) and add these dependencies to the dependencies block. I recommend using specific version numbers instead of + to avoid unexpected breaking changes:
dependencies { // Google Play Games Services SDK v2 implementation "com.google.android.gms:play-services-games-v2:2.2.0" // Optional: For additional authentication options implementation "com.google.android.gms:play-services-auth:20.7.0" }
2. Configure the AndroidManifest.xml
Next, update your AndroidManifest.xml to include required metadata and permissions:
- Add the internet permission (since game services require network access):
<uses-permission android:name="android.permission.INTERNET" />
- Add metadata for your game’s App ID (you’ll get this from the Google Play Console) and Play Services version:
<application ...> <!-- Other app components --> <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>
Don’t forget to create a string resource named app_id with your actual game ID from the Play Console.
3. Implement Platform-Specific Game Services Logic
LibGDX is cross-platform, so we’ll use an interface in the core module to abstract game services, then implement it in the Android module.
Step 3.1: Create a Core Interface
In your LibGDX core module, define an interface for the game services functionality you need (adjust methods based on your use case):
public interface GameServices { void signIn(); void signOut(); boolean isSignedIn(); void unlockAchievement(String achievementId); void incrementAchievement(String achievementId, int steps); void showAchievementsUI(); void submitScore(String leaderboardId, long score); void showLeaderboardsUI(); }
Step 3.2: Implement the Interface in Android
In your Android module, create a class that implements this interface using the Play Games SDK v2:
import android.app.Activity; 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; import com.google.android.gms.games.Games; import com.google.android.gms.games.GamesClient; public class AndroidGameServices implements GameServices { private final GamesClient gamesClient; private final GoogleSignInClient signInClient; public AndroidGameServices(Activity activity) { // Configure sign-in options for game services GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) .requestEmail() .build(); signInClient = GoogleSignIn.getClient(activity, signInOptions); gamesClient = Games.getGamesClient(activity, signInClient); } @Override public void signIn() { gamesClient.signIn().addOnCompleteListener(task -> { if (task.isSuccessful()) { // Handle successful sign-in (e.g., notify your core game logic) } else { // Handle sign-in failure (e.g., show an error message) } }); } @Override public void signOut() { signInClient.signOut().addOnCompleteListener(task -> { // Handle sign-out completion }); } @Override public boolean isSignedIn() { return GoogleSignIn.getLastSignedInAccount(gamesClient.getContext()) != null; } @Override public void unlockAchievement(String achievementId) { if (isSignedIn()) { gamesClient.unlockAchievement(achievementId); } } @Override public void incrementAchievement(String achievementId, int steps) { if (isSignedIn()) { gamesClient.incrementAchievement(achievementId, steps); } } @Override public void showAchievementsUI() { if (isSignedIn()) { gamesClient.getAchievementsIntent() .addOnSuccessListener(intent -> ((Activity) gamesClient.getContext()).startActivityForResult(intent, 1001)); } } @Override public void submitScore(String leaderboardId, long score) { if (isSignedIn()) { gamesClient.submitScore(leaderboardId, score); } } @Override public void showLeaderboardsUI() { if (isSignedIn()) { gamesClient.getLeaderboardsIntent() .addOnSuccessListener(intent -> ((Activity) gamesClient.getContext()).startActivityForResult(intent, 1002)); } } }
Step 3.3: Integrate with Your LibGDX Game
In your AndroidLauncher class, initialize the AndroidGameServices instance and pass it to your core game class:
import android.os.Bundle; import com.badlogic.gdx.backends.android.AndroidApplication; import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; public class AndroidLauncher extends AndroidApplication { @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); // Initialize game services GameServices gameServices = new AndroidGameServices(this); // Pass the game services instance to your core game initialize(new MyLibGDXGame(gameServices), config); } }
Then, in your core game class, you can use the GameServices interface to call the platform-specific methods whenever you need to interact with Google Play Games.
4. Final Setup & Testing
- Make sure you’ve properly configured your game in the Google Play Console: linked your app, set up achievements/leaderboards, and added your app’s SHA-1 fingerprint.
- Test on a real Android device (emulators can have issues with Google Play Services authentication).
- Handle edge cases like network errors or sign-in state changes (e.g., if the user signs out of their Google account outside your app).
内容的提问来源于stack exchange,提问作者Harry

