Including a JAR via classpath on Android

110 views
Skip to first unread message

Roman Sí

unread,
Oct 16, 2023, 3:50:00 PM10/16/23
to Kivy users support
Hello
I am author of pywebview and am working on adding Android support based on Kivy/Pyjnius/Buildozer. I have some Java code that needs to be included with the library. It works fine as long as I add a jar via android.add_jars in Buildozer or original Java files via android.add_src in Buildozer. However, since this is a library, I would like to spare the end-user from extra configuration steps and include the library JAR automatically. I have looked into setting a classpath with jnius_config.add_classpath, but I get  this error

jnius.jnius.JavaException: JVM exception occurred: Didn't find class "com.pywebview.PyWebViewClient" on path: DexPathList[[zip file "/data/app/~~iFWGkcsFbJM8HyitgPBhcg==/com.pywebview.pywebview-ssFZsxemovo9okhxv_gqKw==/base.apk"],nativeLibraryDirectories=[/data/app/~~iFWGkcsFbJM8HyitgPBhcg==/com.pywebview.pywebview-ssFZsxemovo9okhxv_gqKw==/lib/arm64, /data/app/~~iFWGkcsFbJM8HyitgPBhcg==/com.pywebview.pywebview-ssFZsxemovo9okhxv_gqKw==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]] java.lang.ClassNotFoundException

The code I use is as follows. It is placed before importing jnius and the JAR filepath is correct.

import jnius_config
class_path = os.path.join(os.path.dirname(__file__), '..', 'lib', 'android.jar')
jnius_config.add_classpath('.', class_path)

Any idea what went wrong here? Is this something that can be done via classpath or maybe with the help of a recipe?


Robert

unread,
Oct 16, 2023, 5:54:19 PM10/16/23
to Kivy users support

The only way I know is for Gradle to see the Java (however packaged) at build time.
Which is not to say there isn't another way.

When faced with the same issue, I used p4a.hook and bundled a script and some java https://github.com/Android-for-Python/camerax_provider . Not beautiful but it works. One day I want to see if I can do the same trick in a recipe (I need to check reaching in for the gradle options).

FYI Kivy/Android Webview with no extra java or buildozer stuff   https://github.com/Android-for-Python/Webview-Example

Roman Sí

unread,
Oct 23, 2023, 4:58:06 PM10/23/23
to Kivy users support
Thanks for the reply, Robert.

Pure Python implementation would not unfortunately do the trick, as pywebview provides extra features that need extending classes, thus extra Java code.

I took a look into the hook, but it seems this approach is essentially the same as adding android.add_src to buildozer.spec. It would be great if gradle options could be incorporated into a recipe. This solution would address my case in full.

It seems to be possible to load jars dynamically into DexPathList. But alas this is against Play Store guidelines, so not quite a good solution for a library.

Robert

unread,
Oct 23, 2023, 5:34:05 PM10/23/23
to Kivy users support
I'd suggest posting an enhancement request here https://github.com/kivy/python-for-android/issues

I think what is required in p4a is the ability to (from a Recipe class) read and append the  "unknown_args " list in the ToolchainCL class.

Share a link to that hook as an example of the type of thing you want to do.

Robert

unread,
Oct 23, 2023, 9:47:50 PM10/23/23
to Kivy users support
I took a look at pywebview.
(even on a desktop) I would want to put it inside a Kivy widget rather than have it open its own window.

Is there any way to open a webview instance without the GUI?
Looking for a create_pixels('RGBA') rather than a create_window(), make any sense?

Robert

unread,
Oct 23, 2023, 9:59:51 PM10/23/23
to Kivy users support

Oh, wait, I would have to have a way to pass touch events in as well.
And that should probably be bytesIO not pixels above.
Reply all
Reply to author
Forward
0 new messages