I am trying to use a Android Java library with Unreal JNI. Problem is that the library requires an Android application context, and I have not been able to find out how to get it in Unreal.
The GameActivitivity.java naturally has an context available, but passing that through c++ code to a library seems to either mangle the context reference so that it becomes invalid object or weak ref, or the method is not found due to false signature, I think.
Here is one trial
// Get UE4 GameActivity class
jclass game_activity_class = FAndroidApplication::FindJavaClass("com.epicgames.ue4.GameActivity");
// Use a method inserted in UPL.xml to get the context
jmethodID getActivity = env->GetMethodID(game_activity_class, "AndroidThunkJava_GetApplicationContext", "()Ljava/lang/Object;");
jobject main_ctx = env->CallObjectMethod(game_activity_class, getActivity);
// convert to global ref
globalGameCtx = reinterpret_cast<jobject>(env->NewGlobalRef(main_ctx));
// get the library class
jclass localRef = FAndroidApplication::FindJavaClass("com/comp/prod/lib/MyLibManager");
// get the initialize(...) method from the target library
jmethodID initialize_method = env->GetStaticMethodID(localRef, "initialize", "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)V");
// call the initialize(...) method in the target library
env->CallStaticVoidMethod(localRef, initialize_method, globalGameCtx, "TEST", "TEST");
This results into runtime error
: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
: Abort message: 'java_vm_ext.cc:534] JNI DETECTED ERROR IN APPLICATION: can't call java.lang.Object com.epicgames.ue4.GameActivity.AndroidThunkJava_GetApplicationContext() on instance of java.lang.Class<com.epicgames.ue4.GameActivity>'
Any ideas how to fix? Or a better way to obtain context?