Search code examples
java-native-interfacecocos2d-xcocos2d-x-3.0cocos2d-androidcocos2d-js

Call JNI from cocos2d-x to Android is not triggered in Release Mode


I have a problem with call function from cocos2d-x (C++) to the Android Native (Java) in release mode.

In cocos2d-x, I have a function: logInToGamePlayServices, which will call a function in Java side start login to the Game Play Service. It's working normally in debug mode. But when I make a release build to upload to store, this function is not triggered.

My build Settings:
Compile Sdk Version : API 27: Android 8.1 (Oreo)
Target Sdk Version: API 27: Android 8.1 (Oreo)
Min Sdk Version: API 15: Android 4.0.3 (IceCreamSandwich)
Build Tool Version: 28.0.3
NDK: android-ndk-r16b
Cocos2d-x: v3.17

C++:

#include "platform/android/jni/JniHelper.h"
#include <jni.h>

USING_NS_CC;
void NativeHelper::logInToGamePlayServices() {

    JniMethodInfo methodPlayGame;
    if (JniHelper::getStaticMethodInfo(methodPlayGame, "games/core/CoreActivity", "logInToGamePlayServices", "()V")) {
        methodPlayGame.env->CallStaticVoidMethod(methodPlayGame.classID, methodPlayGame.methodID);
    }

}

Android Native:

package games.core;

public class CoreActivity extends Cocos2dxActivity {

       public static void logInToGamePlayServices() {

        _shareInstance.runOnUiThread(new Runnable() {
            public void run() {
                Intent signInIntent = _shareInstance.mGoogleSignInClient.getSignInIntent();
                _shareInstance.startActivityForResult(signInIntent, RC_SIGN_IN);
            }
        });

    }
}

Solution

  • By default, release build invoves ProGuard obfuscation. If you plan to use some Java callbacks from JNI or via reflection, you must explicitly excude these methods from obfuscation. Some more examples can be found in https://stackoverflow.com/a/7881522/192373.