2

I make applications using an engine that generates Xcode or Android Projects and I primarily use Xcode for my projects. However I am looking to use Android Studio for the first time.

In the generated Android Studio project I have a PTServicesBridge Class where I add my own code for additional features outside the engine. (In this instance I need to adjust a score variable up but 1).

PTServicesBridge Class:

public class PTServicesBridge
    public static PTServicesBridge instance() {
        if (sInstance == null)
            sInstance = new PTServicesBridge();
        return sInstance;
    }

    public static void initBridge(Cocos2dxActivity activity, String appId){
        //some initialisations
    }

    //...

    public static void adjustScoreUp(){
        //I need to call a function from here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    }
}

The project includes a main.cpp file that includes the method I need to call.

Main.cpp

#include "screens/PTPScoreController.h"

using namespace cocos2d;

extern "C"
{
    jint JNI_OnLoad(JavaVM *vm, void *reserved){
    JniHelper::setJavaVM(vm);
    return JNI_VERSION_1_4;
}

//...

void adjustScoreUpNow{
    PTPScoreController::scores().points.addCurrent(1); //This method <<<<<<<<<<
}

How can I call the adjustScoreUpNow from the PTServicesBridge Class?

Edit: I was told by the engines support to do it this way. I was hoping there was an easy solution to calling the .cpp method from the Java class.

Reanimation
  • 3,151
  • 10
  • 50
  • 88
  • Thank you for the link. I don't think it is a duplication as the `main.cpp` is already used. So I just need to know how a method in that file can be called from the java class. Thank you for posting. – Reanimation Sep 04 '19 at 19:48
  • The answers to the linked question at the top of the page describe how to use JNI to do what you want. – Code-Apprentice Sep 04 '19 at 20:50

1 Answers1

2

In PTServicesBridge define native java function. Something like:

private static native void  adjustScoreUpNow();

In main.cpp provide following implementation

extern "C" {
JNIEXPORT void JNICALL Java_<full_package_name>_PTServicesBridge_adjustScoreUpNow(JNIEnv *env, jclass clazz) {
   //Call native side conterpart 
   adjustScoreUpNow();     
}
}

Where <full_package_name> should be package name of PTServicesBridge with _ instead of ..

For more info about how JNI works please take a look at docs.

j2ko
  • 2,479
  • 1
  • 16
  • 29
  • Thank you for posting. Unfortunately, I get the following error in the console: E/art: No implementation found for void `com.secrethq.utils.PTServicesBridge.adjustScoreUpNow() (tried Java_com_secrethq_utils_PTServicesBridge_adjustScoreUpNow and Java_com_secrethq_utils_PTServicesBridge_adjustScoreUpNow__) D/AndroidRuntime: Shutting down VM` Which is confusing because the method is there. – Reanimation Sep 05 '19 at 07:31
  • @Reanimation are you sure that your method get to the library .so? Extract native library from you apk or find it in your local build then execute `objdump -TC | grep adjustScoreUpNow`. Please note that `objdump` should be from android NDK. – j2ko Sep 05 '19 at 09:26
  • Well the generated Android Studio project already has a working main.cpp. So I’ve just added the function to that. As you can tell I have no experience with Android or Java. Thanks. – Reanimation Sep 05 '19 at 12:30
  • @Reanimation I understand that you use cocos2d and I even have generated project on my side. If `main.cpp` get built it should contain `Java__PTServicesBridge_adjustScoreUpNow` function. Right? But java cannot load it so probably `.so` library haven't been updated. In order to verify that provide result of command I've mentioned above. Otherwise I will not be able to help you – j2ko Sep 05 '19 at 13:02
  • Thank you for trying to help. I'm unable to find the location and I don't know how to execute that command. I think this is a sign to forget Android :( – Reanimation Sep 06 '19 at 14:04
  • Depends on how you load your library, you may need this in your java class: System.loadLibrary("native-lib"); – iman kazemayni Apr 22 '21 at 12:16