I’m trying to do some C++ JNI programming that’s called from a BP method, but JNI is unable to find the class. Here’s a little test code, to find the android/app/Activity class and then find the “finish” method.
#if PLATFORM_ANDROID
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Exiting UE4");
UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: GetGameActivityThis()"));
jobject activity = FAndroidApplication::GetGameActivityThis();
UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: GetJavaEnv()"));
JNIEnv* env = FAndroidApplication::GetJavaEnv();
UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: Find class: Activity"));
jclass classActivity = FAndroidApplication::FindJavaClass("android/app/Activity");
UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: Find method: finish"));
jmethodID methodFinish = env->GetMethodID(classActivity, "finish", "()V");
UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: Invoke method: finish"));
env->CallVoidMethod(activity, methodFinish);
UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: SampleQuit complete"));
#endif
Unfortunately, the game crashes when it can’t find the activity class.
I’ve made this work in the past by adding a NDK plugin that detects classes in the special JNI init callback. But this time I’d just like to find a class when the BP function is called.
I have a hunch that I need to use the class loader to find the Activity class.
My goal is to get the Activity class, get the Bundle, so I can detect some extra data that is passed to the Activity.
This sample code once working will be enough to figure the rest out.
So my question is why is this line failing?
jclass classActivity = FAndroidApplication::FindJavaClass("android/app/Activity");