Sonar Cocos Helper - Setting Up Google Play Game Services on Android Studio

Android Studio 2.3.3 (LATEST)

Cocos2d-x 3.15.1 (LATEST)

I tested on Debug.

It’s my first experience with Cocos2d-x Game Engine, I developed an Android Game using Cocos2d-x, all it’s fine but when I tried to show the achievements it shows me an error like this :

java.lang.NullPointerException
                            at sonar.systems.framework.SonarFrameworkFunctions.showAchievements(SonarFrameworkFunctions.java:432)
                            at org.cocos2dx.lib.Cocos2dxRenderer.nativeTouchesEnd(Native Method)
                            at org.cocos2dx.lib.Cocos2dxRenderer.handleActionUp(Cocos2dxRenderer.java:129)
                            at org.cocos2dx.lib.Cocos2dxGLSurfaceView$10.run(Cocos2dxGLSurfaceView.java:311)
                            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1486)
                            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1263)

My Code when I click to show the achievements :

SonarCocosHelper::GooglePlayServices::showAchievements();

When I want To sign In :

 java.lang.NullPointerException
           at sonar.systems.framework.SonarFrameworkFunctions.isSignedIn(SonarFrameworkFunctions.java:277)
           at org.cocos2dx.lib.Cocos2dxRenderer.nativeRender(Native Method)
           at org.cocos2dx.lib.Cocos2dxRenderer.onDrawFrame(Cocos2dxRenderer.java:105)
           at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1557)
           at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1263)

My Code for Sign In :

 if(!SonarCocosHelper::GooglePlayServices::isSignedIn())
    SonarCocosHelper::GooglePlayServices::signIn();

My Manifest File :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ilyo.test"
    android:installLocation="auto">

    <supports-screens android:anyDensity="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"/>

    <uses-feature android:glEsVersion="0x00020000" />

    <!-- Basic permission for Internet and don't allow turn of the screen -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:label="@string/app_name"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher">
        
        <!-- Tell Cocos2dxActivity the name of our .so -->
        <meta-data android:name="android.app.lib_name"
                   android:value="MyGame" />

        <!-- Required for Google Play Services -->
<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"/>


<activity
    android:name="org.cocos2dx.cpp.AppActivity"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

</application>
</manifest>

Build Gradle File :

enter image description here

Cocos Helper Frameworks Used :

enter image description here

My Google Play Achievement (I removed the ID from this photo):

enter image description here

I searched already for solutions in relationship with Android Studio but the results are very rare, I don’t know what is the solution and what this the reason for this error.

Thank you,

I founded a solution but now I guess it’s just a dirty fix, first the problem is in the GooglePlayServices.java and exactly in onStart method : Null pointer Exception because mHelper is Null.

@Override
	public void onStart() 
	{
		mHelper.onStart((Activity) ctx);
	}

Let us a little further why it generates null, so :

1. SonarFrameworkFunctions.java

when we want to call GooglePlayServices constructor from SonarFrameworkFunctions.java :

 //GOOGLE PLAY SERVICES
    if (SonarFrameworkSettings.USE_GOOGLE_PLAY_GAME_SERVICES)
    {
        String packageName = "sonar.systems.frameworks.GooglePlayServices.GooglePlayServices";
        googlePlayServices = (InstantiateFramework(packageName) != null) ? InstantiateFramework(packageName) : new Framework();
    }

2. Method : InstantiateFramework in SonarFrameworkFunctions.java

public static Framework InstantiateFramework(String packageName)
{
    Framework tmp = null;
    try
    {
        tmp = (Framework) Class.forName(packageName).getConstructor().newInstance();
        tmp.SetActivity(((SonarFrameworkActivity) app));
    } catch (InstantiationException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e)
    {
        e.printStackTrace();
        Log.e("Class not found", "class doesn't exist or bad package name " + packageName);
    }
    return tmp;
}

The constructor is empty so there is only a method called onCreate that instantiate and prepare the mHelper to access to all functionalities of Google Play Services.

Solution (Dirty Fix):

GooglePlayServices.java

public GooglePlayServices() 
	{
	   mHelper=getGameHelper();
	mHelper.setup(this);
	}
1 Like