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 execute Java (Android) code from C++

Hi there,

I am wondering how I could access images in my Android game. Basically, the functionality I need is that through a click of a UMG button, the Androids' image gallery pops up and the user can pick an image from there.

I did the same thing in iOS (see this thread). Using iOS, I could create a cpp file and put my Objective-C code in there. I also needed to run my methods on the iOS main thread.

Using Android, I don't really know how to start. Looking at the GameActivity.java file I can see that there are native methods (e.g. public native boolean nativeIsShippingBuild();) which will get called from the java-file. The executing code then is written in a cpp-file (here AndroiudJNI.cpp) and defined as:

 extern "C" bool Java_com_epicgames_ue4_GameActivity_nativeIsShippingBuild(JNIEnv* LocalJNIEnv, jobject LocalThiz) { ... }

I noticed that I probably would need to use JNI and read about it in the Android docs. I created a new Java file and placed in D:\UE4\Engine\Build\Android\Java\src\`. Unfortunately I did not yet figure out how to make and instance of this class (with FindClass`?) within my project or in general how to simply call Android/Java methods from C++.

What would be the steps to get this going? Even the slightest hint can help...

Product Version: Not Selected
more ▼

asked Mar 25 '15 at 03:02 PM in Using UE4

avatar image

541 29 33 181

avatar image lukefwilson May 08 '15 at 03:55 AM

Hey trutty, did you end up figuring out how to do this? I'm trying to do something similar with Android mic input, and would love to hear how you got native Android code working!

avatar image trutty Jun 04 '15 at 02:35 PM

Hi lukefwilson, I didn't further try it because the game file size limitation of < 50 MB (no .obb support) in UE4.7 stopped me from further development for Android. However, with UE4.8 obb will be supported so I am still interested in this topic.

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

2 answers: sort voted first

The way we call in to Java from C++ is almost all inside of AndroidJNI.cpp. Search for "ShowConsoleWindow" in that file for all the bits needed to add new functions to be called, and even passing parameters in to Java code.


more ▼

answered Mar 25 '15 at 07:28 PM

avatar image

joshbadams STAFF
1.3k 19 7 46

avatar image trutty Mar 26 '15 at 02:02 PM

Hi Josh, thanks for your reply.

I added a function to GameActivity.java and the cpp part to AndroidJNI.cpp. Calling the cpp function from within my project works and the java call is also fine. However, is there a way to implement the whole thing by not modifying Engine files but in my own project? I feel kinda uncomftable when changing engine code ;)

Quick question: After the cpp call in AndroidJNI.cpp I need to run a function from a class of my project and therefore need to include the header file from the specfic class of my project. Doing so ("#include <MyActorA.h>", directory path is in PATH and VC++ Directories) results in "fatal error 'MyActorA.h' not found" when building for Android. I looked around but found nothing - how can I fix this real quick? I guess I am just missing on some environment path...

Thank you very much

avatar image joshbadams STAFF Mar 30 '15 at 07:28 PM

I haven't tested this, but you could possibly just add your own .java files into your project's Build/Android/JavaLibs directory, and I think it would compile them into the app. You can add the thunks into your own code, they don't have to be in AndroidJNI.cpp. You may need to duplicate some of the code tho. Mostly the AndroidJNI is just a wrapper for a bunch of junk.

I believe you could then find your java class from C++, call a static function on it to create an instance of your class, and then call member functions on that java object.

Again, I haven't tried that kind of thing, but seems likely to work in some way :)

If you added the Java stuff to GameActivity, but just added the thunks to your project's code, you'd be half way there, and can avoid including your Actor code from our stuff. Otherwise, you'll probably need to use a relative path to the include (#include "MyProject/Classes/MyActor.h" or whatever).


avatar image Schlabbermampf Apr 05 '15 at 10:05 AM

To call a wrapped function of the AndroidJNI.cpp from my game-code's class, which headers i have to include in my class? preparations in a .cs file?

avatar image omakke Sep 18 '17 at 01:43 AM

I need to know that too. I am getting linking errors.

avatar image GerardD Apr 22 '15 at 12:15 AM
  • for the above question. I would also like to know how to call the functions that have wrapped the thunks from my project classes including what setup is required

avatar image ZkarmaKun Nov 02 '15 at 06:32 PM

bump this too

avatar image Muzaheed Apr 28 '17 at 01:08 PM

were u able to add the android api through in your game code?

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

See attached WIP library for popping up a keyboard and getting text changes back from an invisible EditText component. Should show you how to run a C++ -> Java interface.

I'd remove the C style intermediate functions, they just add indirection (I copied the style when learning to setup C++ -> Java communication).

There are also functions defined in C that are called from Java - see:


This is declared in the Java (within the XML file) as:

 public native void CPPOnTextChange(String txt, int ref, boolean bDone);

link text

more ▼

answered Nov 30 '17 at 12:49 AM

avatar image

1.3k 61 68 271

(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