使用 Python 进行 Android 编程的 QPython 或 Kivy - 生成可安装的 apk

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28001100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:35:13  来源:igfitidea点击:

QPython or Kivy for Android programming with Python - producing installable apk

androidpythonkivysl4aqpython

提问by icarus74

Having read several Q&A's on SO, I realize that one has 2 options i.e. QPython and Kivy to do programming for Android, however, apparently both take different approaches. I am trying to validate my understanding and see if I am missing some key piece of information.

在阅读了几个关于 SO 的问答后,我意识到有两个选项,即 QPython 和 Kivy 来为 Android 进行编程,但是,显然两者都采用不同的方法。我试图验证我的理解,看看我是否遗漏了一些关键信息。

  • QPython allows usage of Kivy library for developing graphical applications
  • QPython and Kivy both use SL4A, while QPython has expanded standard SL4A (or it's bindings for Python) by adding some NFC and similar functions
  • QPython is used to create python scripts that can use wide range of modules, libraries, but they need QPython installed to be executed on target device. There is no way to package script into an apk.
  • Kivy OTOH, allows developer to write applications that compile to apk, using their cloud based build system (alternative - local build system can be set up on Ubuntu Linux) [However, I noticed that most of the sample apk's that use Kivy are pretty large, in the 40MB range. Did I miss anything ?]
  • QPython apk has 2 version i.e. one for Python-2.7 and another one for Python-3.x. For Kivy, I'm not sure which version it is.
  • QPython example script (HelloWorld.py) doesn't seem to behave as expected, from latest QPython-3.x from Market, on an Android Kitkat (4.4.2) system. I get the dialog to enter text, but then I expect a Toast to popup, but nothing happens.
  • Get the impression that both QPython and Kivy are developed by a single developer each (or only one person is really active at present), and don't yet have a biggish community. [This is my biggest concern] I notice that there are 3-4 questions with 'qpython' tag on SO, and more than thousand with 'kivy'!
  • Also get the impression that at this moment Kivy development is somewhat more active (perhaps quite active), but for QPython I don't have a clear picture.
  • Kivy seems to be trying to expand the nature of application that could possibly be written using it, compare to QPython. There are API's like plyerand pyjniusthat help expand the possibilities. Perhaps quite significantly, compared to QPython.
  • Both QPython and Kivy seem to be heavily under development. Program (/ script) crashes (/ failures) seem to be reported on both set of tools.
  • QPython 允许使用 Kivy 库来开发图形应用程序
  • QPython 和 Kivy 都使用 SL4A,而 QPython 通过添加一些 NFC 和类似功能扩展了标准 SL4A(或它的 Python 绑定)
  • QPython 用于创建可以使用各种模块、库的 Python 脚本,但它们需要安装 QPython 才能在目标设备上执行。没有办法将脚本打包成 apk。
  • Kivy OTOH,允许开发人员使用基于云的构建系统编写编译为 apk 的应用程序(替代方案 - 可以在 Ubuntu Linux 上设置本地构建系统)[但是,我注意到大多数使用 Kivy 的示例 apk 都非常大,在 40MB 范围内。我错过了什么吗?]
  • QPython apk 有 2 个版本,即一个用于 Python-2.7,另一个用于 Python-3.x。对于 Kivy,我不确定它是哪个版本。
  • QPython 示例脚本 (HelloWorld.py) 在 Android Kitkat (4.4.2) 系统上似乎没有按预期运行,来自 Market 的最新 QPython-3.x。我得到了输入文本的对话框,但随后我希望弹出一个 Toast,但没有任何反应。
  • 感觉 QPython 和 Kivy 都是由一个开发人员开发的(或者目前只有一个人真正活跃),并且还没有一个庞大的社区。[这是我最关心的问题] 我注意到 SO 上有 3-4 个带有“qpython”标签的问题,而“kivy”则有超过 1000 个问题!
  • 还得到的印象是,此时 Kivy 的开发更加活跃(也许相当活跃),但对于 QPython 我没有清晰的图景。
  • 与 QPython 相比,Kivy 似乎试图扩展可能使用它编写的应用程序的性质。有像plyerpyjnius这样的API可以帮助扩展可能性。与 QPython 相比,可能相当重要。
  • QPython 和 Kivy 似乎都在大力开发中。两组工具似乎都报告了程序(/脚本)崩溃(/失败)。

Overall, the opinion as a result (of above points) appears to swing in favour of Kivy, a bit more. Is the understanding correct ? Did I miss any crucial point ? This is not a rhetorical question, and I am looking for factual answers only.

总的来说,结果(以上几点)的观点似乎更倾向于 Kivy。理解正确吗?我错过了任何关键点吗?这不是一个修辞问题,我只是在寻找事实答案。

采纳答案by inclement

QPython allows usage of Kivy library for developing graphical applications

QPython 允许使用 Kivy 库来开发图形应用程序

Yes, qpython is an interpreter + associated tools, and has some nice kivy integration. You can't compile the kivy code to a standalone apk with qpython+android alone though.

是的,qpython 是一个解释器 + 相关工具,并且有一些不错的 kivy 集成。但是,您无法单独使用 qpython+android 将 kivy 代码编译为独立的 apk。

QPython and Kivy both use SL4A, while QPython has expanded standard SL4A (or it's bindings for Python) by adding some NFC and similar functions

QPython 和 Kivy 都使用 SL4A,而 QPython 通过添加一些 NFC 和类似功能扩展了标准 SL4A(或它的 Python 绑定)

Kivy does not use SL4A. We achieve android api integration mainly through pyjnius, a library for automatically wrapping java classes with python, which lets you call the java api directly. We also have abstracted some standard things to a pythonic interface with plyer.

Kivy 不使用 SL4A。我们主要通过pyjnius来实现android api集成,pyjnius是一个用python自动包装java类的库,可以让你直接调用java api。我们还使用 plyer 将一些标准的东西抽象为一个 pythonic 接口。

(I saw later that you already have found these)

(我后来看到你已经找到了这些)

QPython is used to create python scripts that can use wide range of modules, libraries, but they need QPython installed to be executed on target device. There is no way to package script into an apk.

QPython 用于创建可以使用各种模块、库的 Python 脚本,但它们需要安装 QPython 才能在目标设备上执行。没有办法将脚本打包成 apk。

I don't use qpython much, but I think this is correct, although there may be some tools turn scripts to apks in some circumstances (e.g. you could use kivy's build tools if you have a kivy interface, or maybe sl4a has something for this).

我不经常使用 qpython,但我认为这是正确的,尽管在某些情况下可能有一些工具将脚本转换为 apk(例如,如果您有 kivy 界面,则可以使用 kivy 的构建工具,或者 sl4a 对此有一些帮助)。

Kivy OTOH, allows developer to write applications that compile to apk, using their cloud based build system (alternative - local build system can be set up on Ubuntu Linux) [However, I noticed that most of the sample apk's that use Kivy are pretty large, in the 40MB range. Did I miss anything ?]

Kivy OTOH,允许开发人员使用基于云的构建系统编写编译为 apk 的应用程序(替代方案 - 可以在 Ubuntu Linux 上设置本地构建系统)[但是,我注意到大多数使用 Kivy 的示例 apk 都非常大,在 40MB 范围内。我错过了什么吗?]

We have a basic cloud based build system but nothing else like that right now, almost everyone builds apks on their own machine using our build tools for android. These run on linux or OSX, and can easily be run in a virtual machine if necessary.

我们有一个基本的基于云的构建系统,但现在没有其他类似的系统,几乎每个人都使用我们的 android 构建工具在自己的机器上构建 apk。它们在 linux 或 OSX 上运行,如果需要,可以轻松地在虚拟机中运行。

A minimal app has about 7MB APK size due to the necessity of bundling the python interpreter and a lot of modules.

由于需要捆绑 python 解释器和许多模块,一个最小的应用程序有大约 7MB 的 APK 大小。

QPython apk has 2 version i.e. one for Python-2.7 and another one for Python-3.x. For Kivy, I'm not sure which version it is.

QPython apk 有 2 个版本,即一个用于 Python-2.7,另一个用于 Python-3.x。对于 Kivy,我不确定它是哪个版本。

Kivy itself supports python3, but our android build tools only support python2.7 for now.

Kivy 本身支持python3,但我们的android 构建工具目前只支持python2.7。

Get the impression that both QPython and Kivy are developed by a single developer each (or only one person is really active at present), and don't yet have a biggish community. [This is my biggest concern] I notice that there are 3-4 questions with 'qpython' tag on SO, and more than thousand with 'kivy'!

感觉 QPython 和 Kivy 都是由一个开发人员开发的(或者目前只有一个人真正活跃),并且还没有一个庞大的社区。[这是我最关心的问题] 我注意到 SO 上有 3-4 个带有“qpython”标签的问题,而带有“kivy”标签的问题则超过一千!

Kivy development is quite active with several regular contributors plus more sporadic ones. You can see for example recent commit activity on github. It's certainly a lot more than a single developer!

Kivy 的开发非常活跃,有几个定期贡献者和一些零星贡献者。例如,您可以在 github 上查看最近的提交活动。这当然不仅仅是一个开发人员!

Kivy seems to be trying to expand the nature of application that could possibly be written using it, compare to QPython. There are API's like plyer and pyjnius that help expand the possibilities. Perhaps quite significantly, compared to QPython.

与 QPython 相比,Kivy 似乎试图扩展可能使用它编写的应用程序的性质。有像 plyer 和 pyjnius 这样的 API 可以帮助扩展可能性。与 QPython 相比,可能相当重要。

I think qpython includes pyjnius. Not sure about plyer.

我认为 qpython 包括 pyjnius。不确定plyer。

Both QPython and Kivy seem to be heavily under development. Program (/ script) crashes (/ failures) seem to be reported on both set of tools.

QPython 和 Kivy 似乎都在大力开发中。两组工具似乎都报告了程序(/脚本)崩溃(/失败)。

I'm not sure what you're looking at, but any non-trivial project will have crashes/failures/bugs reported. I don't think kivy is particularly inherently unstable.

我不确定你在看什么,但任何非平凡的项目都会报告崩溃/失败/错误。我不认为 kivy 天生就特别不稳定。

Overall, the opinion as a result (of above points) appears to swing in favour of Kivy, a bit more.

总的来说,结果(以上几点)的观点似乎更倾向于 Kivy。

I would have said that qpython and kivy are quite different things. Qpython lets you write and run scripts, while kivy is a graphical framework and associated tools to create standalone apps. There's some overlap with tasks that could be achieved with both of them, but also plenty of things where they are not both suitable - for instance, I think qpython is probably much more convenient to make quick scripts with no gui for e.g. simple automation (I think this is possible), whereas I think kivy is a far better choice for creating standalone apps with non-trivial guis. Of course you can write kivy code in qpython, as discussed, but this isn't a great user experience for anything non-trivial and you need a desktop/laptop machine anyway to make a standalone apk.

我会说 qpython 和 kivy 是完全不同的东西。Qpython 允许您编写和运行脚本,而 kivy 是一个图形框架和相关工具,用于创建独立的应用程序。两者都可以实现的任务有一些重叠,但也有很多事情它们都不适合 - 例如,我认为 qpython 可能更方便地制作没有 gui 的快速脚本,例如简单的自动化(我认为这是可能的),而我认为 kivy 是创建具有非平凡 gui 的独立应用程序的更好选择。当然,正如所讨论的那样,您可以在 qpython 中编写 kivy 代码,但这对于任何重要的事情来说都不是很好的用户体验,无论如何您都需要一台台式机/笔记本电脑来制作独立的 apk。

回答by Tobias Kienzler

There are at least two more (though to my knowledge only theoretical) ways of getting Python code running on Android:

至少还有两种(尽管据我所知只是理论上的)让 Python 代码在 Android 上运行的方法:

  • Use jythonand convert the Java bytecode to Dalvik, as mentioned elsewhere. Unfortunately, not all CPython libraries, e.g. numpy, are available for jython, and if you use Java libraries keep in mind that Android doesn't provide the same ones as e.g. the Desktop JRE
  • Use nuitka, which generates C++ code from your Python code. Then use the android-ndk.
  • 使用jython并将 Java 字节码转换为 Dalvik,如别处所述。不幸的是,并非所有 CPython 库,例如numpy,都可用于jython,如果您使用 Java 库,请记住 Android 不提供与桌面 JRE 相同的库
  • 使用nuitka,它从您的 Python 代码生成 C++ 代码。然后使用android-ndk