首頁>Program>source

我建立了一个Java類以在pyjnius的python中使用,但是我無法使用它,因為pyjnius找不到它,pyjnius文件說我必须將Java類移至src / org,我已经完成了 它但是没有成功,有人可以告诉我如何將我的Java類与pyjnius一起使用。

最新回復
  • 6月前
    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代碼,請使用 ant all进行構建 和 export CLASSPATH=build/ 在執行 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>
    

    您可以在此儲存庫中找到完整的示例https://github.com/tshirtman/android_jnius_custom_java

  • apache:htaccess用雜湊重定向到子域URL
  • python:達到特定倍數後从範圍中获取下一个值