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"

How to get application context for JNI library

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?

Product Version: UE 4.20
Tags:
more ▼

asked Oct 19 '18 at 11:03 AM in C++ Programming

avatar image

Teleporno
17 4 4 9

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

2 answers: sort voted first

I ended up solving this by calling java lib directly from UPL file, where you can get the context simply by

Get().getApplicationContext()

more ▼

answered Apr 11 '19 at 12:23 PM

avatar image

Teleporno
17 4 4 9

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

FAndroidApplication::GetGameActivityThis() ?

more ▼

answered Apr 05 '19 at 12:36 PM

avatar image

Mewster
35 4 8 11

avatar image Teleporno Apr 11 '19 at 12:25 PM

Thanks for the answer. This may work as well, but can not verify it anymore.

(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