1

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);
            }
        });

    }
}
Tho Bui Ngoc
  • 763
  • 1
  • 11
  • 36
  • Please define *"not working"*. This could range from application crash when you try to call the function to 'signInIntent returns `SIGN_IN_CANCELLED`'. – Alex Cohn Oct 31 '18 at 08:44
  • Hello Alex Cohn, "not working" that mean, the function is not called. This is just a demo function, all other functions which call from cocos2d-x to the Java side are not triggered ( app don't crash ) – Tho Bui Ngoc Oct 31 '18 at 08:52
  • 1
    The first possibility to isolate, is that ProGuard obfuscates the names of Java callback methods, and **getStaticMethodInfo()** fails to find them. – Alex Cohn Oct 31 '18 at 09:36
  • 1
    Thank you Alex Cohn, after using "Analyse APK", I finally find out name of function in the Java class is obfuscated by ProGuard. Then I have to add some command to the ProGuard file to keep these function names. – Tho Bui Ngoc Nov 02 '18 at 02:37

1 Answers1

1

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.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307