2

1) In my kivy's project I created \platforms\android\test.java with following content:

class Test {
    public int test() {
        return 300;
    }
}

And compiled it to .jar file (in same directory).

2) In buildozer.spec I added line:

android.add_jars = %(source.dir)s/platforms/android/*.jar

3) In projects main.py I'm trying to use it:

import logging
from kivy.utils import platform

if platform == "android":
    from jnius import autoclass, cast

    Test = autoclass('test.Test')
    logging.info(Test().test())

4) apk builds fine, but I get error:

jnius.jnius.JavaException: Class not found 'test/Test'

I assume problem with package name I used autoclass('test.Test'). What name should I use to make things work?

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159

2 Answers2

2

Instead of adding .jar file it's easier to add java source file that p4a will handle to be used on Android.

1) We have file

/java_folder/some/test.java

with following content:

package some;

class Test {
    public int test() {
        return 300;
    }
}

2) In buildozer.spec we add:

android.add_src = %(source.dir)s/java_folder/

3) In projects:

autoclass('some.Test')

works just fine.

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
1

I have some editings in your solution:

  1. /java_folder/some/test.java => /java_folder/some/Test.java
  2. class Test { => public class Test {

After that all will be work.

  • Pleawe provide a link to the answer you are building this on. Otherwise you risk losing the reference once there is more than one other. – Yunnosch May 30 '22 at 14:37