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
1条答案
按热度按时间yebdmbv41#
确保您告诉buildozer您打包的java源代码在哪里。
例如,如果你有
java/org/test/TestClass.java
你能做到的。确保您的java包与您希望从jnius导入的内容相匹配。
一个完整的例子
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
"""
if name == "main":
Application().run()
`app/application.kv`
FloatLayout:
BoxLayout:
orientation: 'vertical'
size_hint: .5, .5
pos_hint: {'center': (.5, .5)}
spacing: '20dp'
`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;
}
您可以在这个存储库中找到这个完整的示例https://github.com/tshirtman/android_jnius_custom_java