Android FindJavaClass For Activity

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

Minor fix.

UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: Find class: Activity"));
	jclass classActivity = FAndroidApplication::FindJavaClass("com/epicgames/ue4/GameActivity");

This works!

	UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: SampleQuit Button Clicked!"));

#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("com/epicgames/ue4/GameActivity");

	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

FindJavaClass might not work for the Android types, but there’s a workaround by using an object to get the class. This works for me.

	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("com/epicgames/ue4/GameActivity");

	UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: Find method: getIntent"));
	jmethodID methodGetIntent = env->GetMethodID(classActivity, "getIntent", "()Landroid/content/Intent;");

	UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: Invoke method: getIntent"));
	jobject intent = env->CallObjectMethod(activity, methodGetIntent);

	UE_LOG(LogTemp, Log, TEXT("USampleBPLibrary: Find class: Intent"));
	jclass classIntent = env->GetObjectClass(intent);

Where did you add this piece of code? In your project, in a project plugin,…? What did you included in the build.cs to make this compile?

When I was building this, I had to build the engine from source for Android. Here is the old project I used this for. https://github.com/razerofficial/ue4-plugin-razer-sdk