How do I call Java code in unreal c++ using JNI?

Hello, fellow game creators.
I’m working on this VR project in which a mobile device is used as a controller.
Currently I’m testing JNI to call Java code on UE4.
Only to fail, though…
So I referred to these two links to figure out what I messed up.

But still, when I build and run it on the device, it crashes at the certain function.
I can’t seem to figure out what’s wrong.
I could really use some help.

Below is the code.
(don’t mind Korean letters)

→ GameActivity.java
generated custom function AndroidThunkJava_Toast(String msg)

→ AndroidJNI.h
declare variable to save Java method ID in order to call Java function
(static jmethodID AndroidThunkJava_Toast;)

→ AndroidJNI.cpp
Add Java method ID

→ declare function to AndroidJNI.cpp

→ AndroidJNI.cpp
Add AndroidThunkCpp_Toast to call cached Jave Method ID

→ AndroidJNI.cpp
add to JNI_OnLoad automatically called by UE4 library load

→ MyActor.h
declare function
(void Toast(FString msg))

→ MyActor.cpp
Call java function

If somebody knows something, please go ahead and leave a comment.

Stay awesome.

did u find what was the issue?

I believe the reason could be you didnt add “Android” module to your Build.cs , but plz confirm what was the issue

Bumping. Also need answers for this. 안데브 have you learned how to do this?

I need this too. Documentation would be good.

UE4 used code obfuscation system called ProGuard on it’s java layer, because of that functions name are modified and C++ references fails due to that name change. You need to add exception to your function in ProGuard configuration, you can find engine directory \Engine\Build\Android\Java\proguard-project.txt. All other linking function that engine used are already there too which shows you a example

Also you don’t need to modify engine for this, you can do this in your own module using exacly same function that AndroidJNI class does.

Based on your experience, do you think its possible to wrapping java native call to access microphone data on android platform in real time in the same way as above example?

I’m asking bec AndroidVoiceModule has a bug I reported and epic games no clear information to fix it they always delay it

Link of the bug: Unreal Engine Issues and Bug Tracker (UE-62389)

This has capabilities of standard JNI so you should read the performance of that, like sending PCM stream using it. But why don’t you check AndroidVoiceModule source yourslef and see what it’s doing and try to replicate it potentially even thinking that module yourself, it might be something missing on resume. You might try stoping capture on app sleep and restart on wake up

along past month, and I’m working debugging the AndroidVoiceModule, actually its not written by epic games.

Since its developed using OpenSL ES, I read alot from documentation about it, its architecture…etc even more code samples.

When I debugged the app, when its closed, it fail to shutdown the audio driver, which always marked as used when you start the app again. also on closing, the catlog says “failed to kill 1 processes from group 1956 or some numbers”

I followed the instruction of destroying the ESEngine since at the given code its not “you have destroy recorder object and then the engine object” while the module destroy the engine Object first then the recoder, I did fix that but the problem still exist.

For VoiceCapture part , I made sure that I stop it and clear the buffer before exit, so when it shutdown no crashes or unexpected behaviuor, that make no difference.

Fun thing, is during runtime, I’m able to destroy VoiceCapture and create new one with no issue on android, but if I destory it, and then I close the app and start it agian, still the issue exit.

I dont know what else I suppose to do. than just skip it and find another way around, currently I’m investigating FMOD.

This the link for full thread just in case you want to have a look
https://forums.unrealengine.com/development-discussion/audio/1507971-ue-62389-android-mic-stops-capturing-audio-after-2nd-load-of-app

Edit : I used PlatformGameinstance to handle voice capture on background and foreground to make sure everything undercontorl, but still that doesnt fix the problem.