Custom java called from c++ causes ClassNotFoundException
I'm currently trying to extend the java part of a game i'm developing for Android. We need to access photo gallery and camera from the device. So we have added a few java classes to Build/Android/src/.../ folder to call from c++.
However, when we tried to access the classes, we are receiving a not found error on jni. We created a simple java class, and tried to call it from an emptry project with also no luck.
We need help for solving this problem... :)
I added a few lines to proguard-project.txt to keep the class if it was being removed, yet it was not. I can see that the class exists in dex file.
There is also another problem. We cannot add any files under Build/Android folder (custom proguard file, build file, extended manifest, etc), as it crashes the UnrealBuildTool with a "pak file not found error", or sometimes, "file already exists"... but that is a different problem
So... here are the codes that you can use to re-create the circumstances: proguard-project.txt (C:\Program Files\Epic Games\4.13\Engine\Build\Android\Java)
This is our simple test class:
And this is the code we are using to call the class (exception occurs in FindClass line):
From the crash log...
And at last, this is from the dexdump of classes.dex:
Decompiling the apk proves again that the Tools class exists.
and the static method signature we are using is true
asked Dec 28 '16 at 08:22 AM in Packaging & Deployment
While looking what was going on, I found out that the environment reference that was returning from vm was not the main activity's environment. It was somewhat a bogus system enviroment that can never be able to access the dex classes. I must get the environment from the active context, which it is not possible outside of JNI_Onload...
While checking the source for Android on UE4 I found out that Epic choose a way to get class loader class from the main activity while JNI_OnLoad is first executed and they keep a reference to it plus to the methodid for FindClass.
I am so blind... Epic already thought what I was trying to do and implemented a static method. This is what my code is now:
Android/AndroidApplication.h has already a method for calling classes and works as expected. So simple
Yet now I have another problem (which can be solved on java class)... I always thought that blueprint was working on GUI thread. Yet calling a blueprintable method shows me that JNI is working on another thread. So I need to run my java code in an activity's runOnUiThread runnable class...
answered Jan 10 '17 at 02:33 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here