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][1]). 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][2]. 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…

How to access iOS photo library / use Objective-C properly? - Plugins - Epic Developer Community Forums
[2]: JNI tips  |  Android NDK  |  Android Developers

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.

Josh

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 :wink:

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

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 :slight_smile:

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).

Josh

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?

+1 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

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!

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.

bump this too

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

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

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:

Java_com_epicgames_ue4_GameActivity_CPPOnTextChange.

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

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

[link text

221926-androidkeyboard.zip (20.6 KB)