kivy / pyjnius

Access Java classes from Python
https://pyjnius.readthedocs.org
MIT License
1.4k stars 255 forks source link

Error when importing any package outside the default dependencies, autoclass error and NativeInvocationHandler error #645

Closed JoaoEmanuell closed 1 year ago

JoaoEmanuell commented 1 year ago

My error occurs when importing any package outside the standard dependencies and even in them, as is the case of NativeInvocationHandler of jnius.

Basically almost any package imported through autoclass ends up generating an error, in this case the error is always the same and I couldn't fix it.

The error was reported when trying to import a java class that is placed inside the project, in the same place as main.py and it was added to buildozer.spec.

This error even prevents using plyer, since it depends on NativeInvocationHandler and it cannot be imported.

In general the error is always the same:

jnius.jnius.JavaException: JVM exception occurred: Didn't find class "org.jnius.NativeInvocationHandler" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]] java. lang.ClassNotFoundException

All errors occur on android, tested on 5.1.1 and 11

Below is a list of successful imports:

  1. org.kivy.android.PythonActivity
  2. android.app.NotificationManager

Now a list with the imports that gave error

  1. ProgressBar # Class stored locally, I tried all ways to import it, the only way that "worked" was using a . before it, the problem is that this generates a runtime error and crashes the application.
  2. android.support.v4.app.NotificationCompat # In this case it is installed and added to buildozer.spec, the problem is that it always gives the error of ClassNotFound
  3. org.jnius.NativeInvocationHandler # As previously reported

In all cases none of the solutions worked, the tested solutions were:

  1. Add class_path through jnius_config, in this case I added both absolute and relative path
  2. Using set_classpath also from jnius_config, didn't work either.

Using get_classpath from jnius_config, it was demonstrated that they were indeed added to classpath, but it did not seem to solve the problem.

Excerpts from buildozer.spec proving the addition of the java class and v4 support:

# (list) List of Java files to add to the android project (can be java or a
# directory containing the files)
android.add_src = ProgressBar.java

# (list) Gradle dependencies to add (currently works only with sdl2_gradle
# bootstrap)
android.gradle_dependencies = com.android.support:support-v4:26.1.0

Tested solutions in code form:

from pathlib import Path
from jnius_config import set_classpath, get_classpath
path = str(Path().absolute()) + '/*'
print(path)
set_classpath('.', path)
print(get_classpath())
from jnius import autoclass      
print('Android Notification!')
mActivity = autoclass("org.kivy.android.PythonActivity").mActivity
context = mActivity.getApplicationContext()
path_to_progress = str(Path().absolute()).replace('/data', '')[1::].replace('/', '.')
print(path_to_progress)
SetProgress = autoclass(f"{path_to_progress}.ProgressBar") # app crash here

# Other soluction
from jnius import autoclass
AndroidNotification = autoclass('android.app.NotificationManager')
print(AndroidNotification)
NotificationCompact = autoclass('android.support.v4.app.NotificationCompat') # app crash here

# Other soluction

from jnius import autoclass
autoclass('org.jnius.NativeInvocationHandler') # App crash here
import plyer
plyer.notification.notify(title='Test', message='Notification using plyer') # If dont add autoclass('org.jnius.NativeInvocationHandler') App crash here

Error reported:

jnius.jnius.JavaException: JVM exception occurred: Didn't find class "org.jnius.NativeInvocationHandler" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]] java.lang.ClassNotFoundException

I feel that java is to blame, but I have no idea how to fix it.

Thank you for reading this far, if it can help my problem I really appreciate it.

github-actions[bot] commented 1 year ago

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