【问题标题】:Java classes for pyjniuspyjnius 的 Java 类
【发布时间】:2021-04-06 04:05:31
【问题描述】:

我已经创建了一个 Java 类以在 python 中使用 pyjnius 但我无法使用它,因为 pyjnius 找不到它,pyjnius 文档说我必须将 Java 类移动到 src/org 我已经完成了它但没有成功,有人可以告诉我如何将我的 Java 类与 pyjnius 一起使用。

【问题讨论】:

    标签: java python kivy kivymd pyjnius


    【解决方案1】:

    确保告诉 buildozer 你打包的 java 源代码在哪里。

    例如,如果您有java/org/test/TestClass.java,您可以这样做。

    android.add_src = java/
    

    确保您的 java 包与您希望从 jnius 导入的包匹配。

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

    一个完整的例子

    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;
        }
    }
    

    (可选,如果你想在桌面上测试你的java代码,在运行python app/main.py之前用ant allexport CLASSPATH=build/构建它)

    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>
    

    你可以在这个存储库https://github.com/tshirtman/android_jnius_custom_java找到这个完整的例子

    【讨论】:

    • 非常感谢兄弟,顺便说一下java文件夹是你创建的还是系统自带的,如果你在保存的地方创建的。
    • 这是我在项目文件夹中创建的目录(我从中运行 buildozer)。我给出的所有路径都是相对于项目目录的。
    • 哦,我明白了,也就是说我可以在桌面上创建我的项目文件夹,没有问题?
    • 我不确定我是否理解这个问题,但你可以在任何你想要的地方创建你的项目文件夹,并以你认为合适的任何方式组织 python/java 代码,只要你告诉 buildozer 在哪里找到你想要包含的java代码,你的java包信息和pyjnius期望的一致。
    • 可能看看我在 github 上链接的完整项目,都是相同的文件,但是看到所有文件组织正确会更容易理解。
    猜你喜欢
    • 2017-02-27
    • 2016-05-25
    • 2015-08-23
    • 2020-11-18
    • 2019-01-05
    • 2015-09-25
    • 2020-07-01
    • 2017-08-18
    • 2020-12-22
    相关资源
    最近更新 更多