x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: UE 4.21
Tags:
more ▼

asked Nov 15 '18 at 05:36 AM in C++ Programming

avatar image

tgraupmann
112 16 23 81

avatar image Mewster Apr 05 '19 at 01:10 PM

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?

avatar image timg_razer Apr 05 '19 at 09:26 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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
more ▼

answered Nov 15 '18 at 05:45 AM

avatar image

tgraupmann
112 16 23 81

avatar image tgraupmann Nov 15 '18 at 06:05 AM

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);
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question