This was my very first attempt at integrating Google Play Game Services (GPGS) into a game built with Cocos2d-x using SonarCocosHelper. At first, everything seemed to be going smoothly. The game launched, ran well, and I could navigate through all my gameplay screens without issues.
My Initial Code Setup
Showing Achievements
In C++, I simply called the built-in method from SonarCocosHelper:
SonarCocosHelper::GooglePlayServices::showAchievements();
Signing In to Google Play
To check and sign in the user if needed:
if (!SonarCocosHelper::GooglePlayServices::isSignedIn())
SonarCocosHelper::GooglePlayServices::signIn();
Everything looked clean. But here’s what I got.
The Crash & Error Detail
When Showing Achievements:
java.lang.NullPointerException
at sonar.systems.framework.SonarFrameworkFunctions.showAchievements(SonarFrameworkFunctions.java:432)
When Checking Sign-In:
java.lang.NullPointerException
at sonar.systems.framework.SonarFrameworkFunctions.isSignedIn(SonarFrameworkFunctions.java:277)
This stumped me for a bit.
Root Cause of the Error
The NullPointerException
was a big red flag: something in the Java layer was being accessed before it was properly initialized.
After digging through the SonarCocosHelper documentation and scanning SonarFrameworkFunctions
, I figured out the main culprits:
Common Causes:
GoogleApiClient
or the Play Games sign-in client was not initialized.- The
SonarFramework
was not set up properly in the AndroidAppActivity
. - Google Play Services wasn’t connected or configured yet.
- My
app_id
orgoogle-services.json
might have been missing or misconfigured.
Fix the Issue
Properly Initialize SonarFramework in AppActivity.java
I opened my Java file: AppActivity.java
, and added the following to the onCreate()
method:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Important! This initializes the framework
SonarFrameworkFunctions.SetActivity(this);
SonarFrameworkFunctions.Initialize(this);
}
Without this step, the internal references to Google services remain null
, which is exactly what was causing those crashes.
Add and Link google-services.json
From the Google Play Console (or Firebase), I downloaded google-services.json
and placed it inside the app/
directory.
Then I updated my Gradle files:
Root build.gradle
:
dependencies {
classpath 'com.google.gms:google-services:3.1.0' // or latest version
}
App level build.gradle
:
apply plugin: 'com.google.gms.google-services'
Manifest Configuration
I ensured my AndroidManifest.xml
had the right metadata and permissions:
<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" />
In res/values/strings.xml
:
<string name="app_id">REPLACE_WITH_YOUR_APP_ID</string>
And added this permission:
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
More Functionality to Practice With
Once it was working, I explored more GPGS features:
Show Leaderboard
SonarCocosHelper::GooglePlayServices::showLeaderboard("LEADERBOARD_ID");
Submit Score to Leaderboard
SonarCocosHelper::GooglePlayServices::submitScore("LEADERBOARD_ID", 10000);
Unlock an Achievement
SonarCocosHelper::GooglePlayServices::unlockAchievement("ACHIEVEMENT_ID");
Increment Achievement Progress
SonarCocosHelper::GooglePlayServices::incrementAchievement("ACHIEVEMENT_ID", 5);
Sign Out
SonarCocosHelper::GooglePlayServices::signOut();
Best Practice I Learn
- Always check
isSignedIn()
before calling any GPGS feature:
if (SonarCocosHelper::GooglePlayServices::isSignedIn()) {
SonarCocosHelper::GooglePlayServices::showAchievements();
}
- Use logging tools like:
__android_log_print()
in JavaCCLOG()
in Cocos2d-x
- Debug with
adb logcat
this helped me trace exactly where things were failing.
Final Thought
This journey taught me that even well-packaged libraries like SonarCocosHelper require proper initialization especially when dealing with native bindings between C++ and Java. The NullPointerExceptions were a result of missing initialization, not a bug in the helper itself. Now that my game shows achievements, leaderboards, and handles sign-in/out smoothly, I can confidently say I’ve bridged the gap between Cocos2d-x and Google Play Game Services.