pyjnius icon indicating copy to clipboard operation
pyjnius copied to clipboard

"Class not found" when trying to access a class from .AAR imported with Gradle

Open st-den opened this issue 4 years ago • 2 comments

A bit of history: trying to make TensorFlow Lite work on Android. The way they recommend isn't working for like 9 months now, and currently isn't a priority for them (issue). So the only thing left to try is adding an .AAR file and using the classes through pyjnius. Guide 1, guide 2, .AAR downloaded from here.

And when I do that, I get jnius.jnius.JavaException: Class not found b'org/tensorflow/lite'.

It is worth noting that I get libpythonXXX.so not found when I try to build with any version of TFLite .AAR for armeabi-v7a, which may be a separate issue and is probably harder to debug than this one. When I build other stuff, both arm64-v8a and armeabi-v7a builds work on my phone fine.

Tried 5 different versions of TFLite, two newest versions of Buildozer, p4a master/develop, cleaned everything multiple times, but no luck. Will be grateful for any tips.

Also, I may just be doing it all wrong, since I haven't found much info for such a case. So excuse my ignorance, if I missed something obvious.

st-den avatar Jun 03 '20 15:06 st-den

I'm seeing very similar behavior with AAR or with Java classes that are directly in my project, with pyjnius 1.3.0. Classes are present in the generated APK (as seen with an APK decompiler), yet the error I run into at runtime is the following:

Service: Traceback (most recent call last):
Service:   File "/home/user/hostcwd/.buildozer/android/app/lib/service.py", line 920, in _check_firmware_revision
Service:   File "/home/user/hostcwd/.buildozer/android/app/lib/service.py", line 847, in update_firmware
Service:   File "/home/user/hostcwd/.buildozer/android/platform/build-arm64-v8a/build/python-installs/app/jnius/reflect.py", line 229, in autoclass
Service:   File "jnius/jnius_export_func.pxi", line 26, in jnius.jnius.find_javaclass
Service:   File "jnius/jnius_utils.pxi", line 91, in jnius.jnius.check_exception
Service: jnius.jnius.JavaException: JVM exception occurred: Didn't find class "org.company.package.FileTwo" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system/product/lib64, /system/lib64, /system/product/lib64]] java.lang.ClassNotFoundException

After a lot of digging, it seems that the issue comes from imports that are not in the main thread. Moving all my imports to the top of my files, so that they are executed when the app is loaded, appears to have fixed it.

lerela avatar Jun 29 '20 12:06 lerela

@lerela, I just wanted to say that you probably saved me days of work. I am working on a project that is multiplatform, and some of the android code ended up in a different thread and hadn't been tested for months. It was a complete mystery why pyjnius was failing, now that we were testing android again, and almost all of the dependencies for this project had changed, so I had a huge pile of things to go through.

I wouldn't have figured this out any time soon.

Thank you

rheasman avatar Mar 15 '22 23:03 rheasman

👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project. Let us know if this comment was made in error, and we'll be happy to reopen the issue.

github-actions[bot] avatar Oct 28 '23 02:10 github-actions[bot]