問題描述
我想使用 kivy 在 Python 中為 Android 編寫簡單的應(yīng)用程序.可悲的是,當我開始示例代碼時,我只看到閃屏,幾秒鐘后應(yīng)用程序完成工作.調(diào)試存在很大問題,因為 Linux Mint 上的 adb 沒有檢測到我的設(shè)備.
I want write simple application in Python for Android using kivy. Sadly when I start example code I see only splash screen and few second later application finish work. There is a huge problem with debugging because adb on Linux Mint does not detect my device.
誰能看看我的代碼并告訴我為什么?要構(gòu)建應(yīng)用程序,我使用 buildozer.您還可以查看 create_env 腳本來檢查所有依賴項.
Can someone look at my code and tell my why? To build application I use buildozer. You can also see create_env script to check all dependencies are there.
最好的問候.德拉昆
我開始調(diào)試我的應(yīng)用程序.結(jié)論:
I started debugging my application. Conclusion:
- buildozer + python3 + kivy 是個壞主意
- 如果我在 text 屬性為 str 時使用 kivy.uix.button.Button 則出現(xiàn)異常AttributeError: 'str' object has no attribute 'decode'"
- 如果我在 text 屬性為字節(jié)時使用 kivy.uix.button.Button,則會出現(xiàn)異常ValueError: Button.text accept only str"
看起來像沒有解決方案的循環(huán).想知道我什么時候應(yīng)該報告?
It looks like loop with no solution. Some idea when I should report it?
異常位于 .buildozer/android/platform/build/build/python-installs/pad/android/init.py" 文件中,因此它看起來不像 kivy 和/或 buildozer 異常.
Exception is in .buildozer/android/platform/build/build/python-installs/pad/android/init.py" file so it does not look like kivy and/or buildozer exception.
推薦答案
我用過python-for android工具,遇到了同樣的錯誤.但在我的情況下,應(yīng)用程序根本沒有運行 - 從啟動屏幕崩潰.最后,我找到了解決方案.你可以試試同樣的方法.
I've used python-for android tool and faced with the same errors. But in my case, app didn't run at all - crashed from splash screen. Finally, I've found a solution. You can try the same way.
所以我的管道是 python3 + python-for-android(p4a 工具,python-for-android,來自 master 分支)+ kivy (1.10.1)
So my pipeline was python3 + python-for-android (p4a tool, python-for-android, from master branch) + kivy (1.10.1)
有一個文件_android.pyx"用于 android 構(gòu)建配方(您可以通過命令 p4a recipes
查看可用的 p4a 配方的完整列表).該文件可能由 Buildozer 使用,并且在 APK 構(gòu)建過程中完全由 P4A 使用.你需要修復(fù)它.
There is a file "_android.pyx" for android building recipe (full list of avaliable p4a recipes you can see by command p4a recipes
). This file is, possibly, used by Buildozer, and exactly used by P4A during APK building procedure. You need to fix it.
您可以通過以下方式在 Ubuntu 中找到它的位置(例如):
You may find it's location in Ubuntu (for example) via:
sudo updatedb
locate _android.pyx
它的路徑應(yīng)該是這樣的:
It's path should be something like:
~/.local/lib/python3.6/site-packages/pythonforandroid/recipes/android/src/android/_android.pyx
應(yīng)該有一個字符串:
python_act = autoclass(JAVA_NAMESPACE.decode('utf-8') + u'.PythonActivity')
所以你應(yīng)該改變它 - 像這樣:
so you should change it - something like this:
python_act = autoclass(str(JAVA_NAMESPACE) + u'.PythonActivity'),
或者只是使用一些硬代碼:
or just use some hardcode:
python_act = autoclass("org/kivy/android/PythonActivity")
或者來源中可能有其他 decode() 用法.
Or there might be the other decode() usage in sources.
原因:Python2 和 Python3 之間的差異 - decode() 方法可用于 Python 2 或 3 中的等效二進制數(shù)據(jù)類型,但它不能被 Python 2 和 3 之間的文本數(shù)據(jù)類型一致地使用因為 Python 3 中的 str 沒有方法 decode 函數(shù)在 Python3 中有不同的實現(xiàn).更多細節(jié)在這里:pyporting 功能問題p4a的github
The reason: differences between Python2 and Python3 - the decode() method is usable on the equivalent binary data type in either Python 2 or 3, but it can’t be used by the textual data type consistently between Python 2 and 3 because str in Python 3 doesn’t have the method decode function has different realisation in Python3. More details are here: pyporting features issues p4a's github
希望對你有所幫助.
這篇關(guān)于Kivy 應(yīng)用程序無法在 Android 上運行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!