1

I have created a Java class to use in python with pyjnius but I can't use it as pyjnius can't find it, the pyjnius documentation says that I have to move the Java classes to src/org And I have done it but have not been successful, could someone tell me how can I go about using my Java classes with pyjnius please.

yaxter
  • 85
  • 1
  • 8

1 Answers1

4

Make sure you tell buildozer where is the java source that you package.

for example, if you have java/org/test/TestClass.java you could do.

android.add_src = java/

make sure your java package matches what you expect to import from jnius.

package org.test;
from jnius import autoclass
autoclass('org.test.TestClass')

a full example

app/main.py

"""Demonstrate loading custom java code using jnius
"""
from kivy.app import App
from jnius import autoclass


class Application(App):
    """see module documentation
    """

    def test_jnius(self, name):
        """Lookup our test class, instanciate and call its method
        """
        cls = autoclass("org.test.TestClass")
        result = cls(name).get_result()
        self.root.ids.result_box.text = result


if __name__ == "__main__":
    Application().run()

app/application.kv

FloatLayout:
    BoxLayout:
        orientation: 'vertical'
        size_hint: .5, .5
        pos_hint: {'center': (.5, .5)}
        spacing: '20dp'

        Label:
            text: 'Please enter your name'

        TextInput:
            id: ti
            multiline: False
            size_hint_y: None
            height: self.minimum_height

        Button:
            text: 'hit me!'
            on_release: app.test_jnius(ti.text)
            size_hint_y: None
            height: '38dp'

        Label:
            id: result_box

buildozer.spec

[app]
title = Kivy With Java App
package.name = kivyjavaapp
package.domain = org.test
source.dir = app/
source.include_exts = py,png,jpg,kv,atlas
version = 0.1
requirements = python3,kivy
orientation = portrait
fullscreen = 0
android.add_src = java/
android.arch = armeabi-v7a
android.allow_backup = True
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.10.0
ios.codesign.allowed = false

[buildozer]
log_level = 2
warn_on_root = 1

java/org/test/TestClass.java

package org.test;
import java.lang.String;

public class TestClass {
    private String _name;

    public TestClass(String name) {
        _name = name;
    }

    public String get_result() {
        return "Hello " + _name;
    }
}

(optional, if you want to test your java code on desktop, building it with ant all and export CLASSPATH=build/ before running python app/main.py)

build.xml

<project>
    <property name="ant.build.javac.source" value="1.7" />
    <property name="ant.build.javac.target" value="1.7" />

    <target name="clean">
      <delete dir="build"/>
    </target>

    <target name="test-compile">
        <mkdir dir="build"/>
        <javac srcdir="java/" destdir="build"
               includeantruntime='false'
               encoding="UTF-8"/>
    </target>

    <target name="jar" depends="test-compile">
        <jar destfile="build/org.test.jar" basedir="build/">
        </jar>
    </target>

    <target name="all" depends="jar,test-compile"/>
</project>

you can find this full example in this repository https://github.com/tshirtman/android_jnius_custom_java

Tshirtman
  • 5,859
  • 1
  • 19
  • 26
  • Thank you very much brother, by the way the java folder was created by you or is from the system, and if you created it where you saved it. – yaxter Jan 02 '21 at 13:58
  • It's a directory that i created in the project folder (where i run buildozer from). All paths i gave are relative to the project directory. – Tshirtman Jan 02 '21 at 14:00
  • Ooh I see, that is to say that I can create my project folder on the desktop and there would be no problem? – yaxter Jan 02 '21 at 14:02
  • I'm not sure i understand the question, but you can create your project folder anywhere you want, and organize the python/java code any way you see fit, as long as you tell buildozer where to find the java code you want to include, and your java package info is consistent with what pyjnius expects. – Tshirtman Jan 02 '21 at 14:04
  • Probably have a look at the full project i linked on github, it's all the same files, but it's easier to understand seeing all the files organized correctly. – Tshirtman Jan 02 '21 at 14:05
  • You are right, by the way is there any way to test my java classes on Android, you see I use Pydroid3 to interact with the hardware of my Android mobile but I have tried to do it with pyjnius but can't find the class – yaxter Jan 02 '21 at 14:11
  • That's exactly what the example does, and the explanation in the answer says how to do, is there something in the explanation/example that is not clear? – Tshirtman Jan 02 '21 at 15:36
  • Everything you have explained to me is clear, what I wanted to say is that if there is any way to test my java classes on Android without compiling, Pydroid3 also allows you to use Android classes With pyjnius and I what I want is to run my java classes on Android without compiling the app, the problem is that I don't know where to save my java classes in the memory of my Android so that Pydroid3 acknowledge, also thank you very much for your explanation, it is very good. – yaxter Jan 02 '21 at 17:58
  • Sadly i don't think there is a way, you need the java code to be compiled for android, and if pydroid doesn't hive you a way to do that, it's not going to be able to load it. – Tshirtman Jan 03 '21 at 12:17