An actual history of Python on Android

An actual history of Python on Android – Kivy Blog

This ZDNet article was published a few days ago about how "Python apps might soon be running on Android". It summarises some recent developments in Android support for CPython, but disappointingly it’s highly misleading about some key points. In particular the article states that "apps written in Python may one day run natively on iOS and Android devices", but in fact people have been doing this since at least 2011.
这个 ZDNet 文章几天前被发布关于如何  Python apps 可以马上再Android上运行。 它总结了一些当前发展再Android支持 CPython, 但是令人失望的是,它是一个高度地误导关于一些关键点上。 尤其是文章表明:  写在python里的文章哪天可能在IOS 和Android设备上天生就可以运行,然而事实是人们自从2011年就开始干这事到现在还没成功。

I thought I’d write some short details about the actual history of Python on Android from my own perspective. It’s something I’ve been involved in on some level for about 8 years, and for a significant part of that I’ve been a primary maintainer of python-for-android. This is a build tool for creating APKs from Python applications, originally created for Kivy but now more generic so that it can also support e.g. flask running on the device with a webview gui, or more recently Pygame via its upcoming SDL2 support.

我想从自己的角度简要介绍一下Python在Android上的实际历史。这件事我已经在某种程度上参与了大约8年,并且在这段时间的很大一部分时间里,我都是python-for-android的主要维护者。这是一个从Python应用程序创建APK的构建工具,最初是为Kivy创建的,但现在更通用,因此它还可以支持   例如 使用带有Webview gui的flask    在设备上运行,或者最近通过其即将推出的SDL2支持来使用Pygame。

Kivy app screenshots

Example Python apps for Android. Large version. From left to right: Flat JewelsColourBlindKognitivoLazy BadukPyonic interpreterBarly (original Python version).
python app 给android系统的案例。  巨大的版本。 从左到右的app: 平宝石快,  颜色绑定,  Kognitivo, 懒汉围棋, py相关的解释器, 巴里小伙子

Notes on the term "native"   注意  期限上的 天生的 

A key problem point around discussing Python on Android in that the term "native" is overloaded to mean two different things.
在讨论Android上的Python时,一个关键问题点是“原生”这个词被赋予了两种不同的含义。

The first refers to simply compiling CPython to run "natively" with the Android kernel and libraries, just as it does on desktop platforms, and this is what the ZDNet article refers to with e.g. the quote "Android devices are now fast enough, and the Android kernel permissive enough, to run CPython itself". This is not a recent development, it has been technically possible for many years, although the article is correct that it comes with disadvantages and not everything works the same as on desktop.
首先,“原生”指的是将CPython(一种Python的实现)简单地编译为可以在Android内核和库上“原生”运行,就像它在桌面平台上所做的那样。ZDNet的文章中提到的就是这个意思,例如引用“Android设备现在速度足够快,Android内核足够宽容,可以运行CPython本身”。这并不是一个最近的发展,虽然在技术上已经可能实现很多年了,但文章正确地指出,它也有缺点,并不是所有东西都能像在桌面上那样工作。

The second is using the "native" GUI toolkit for the platform. On Android that means using the same widget toolkit as normal Java-based development. This is the part that is difficult and not yet widely supported, see below for more discussion about the recent developments. I’m actually not sure if the ZDNet article intends to focus on this, but perhaps fails because the author doesn’t understand the distinction, certainly the article focuses on CPython technical details.

第二种是指使用平台的“原生”GUI工具包。在Android上,这意味着使用与基于Java的正常开发相同的组件工具包。这部分既困难又尚未得到广泛支持,下面将更详细地讨论最近的发展。我实际上不确定ZDNet的文章是否打算关注这一点,但可能是因为作者没有理解这种区别而未能做到,当然,文章主要关注CPython的技术细节。

It’s absolutely true that having Python applications seamlessly using the native GUI toolkit is essential to making Python a serious alternative to Java for generic Android development. However, there are huge areas of Python development where this simply isn’t a big concern, e.g. everything from games (Pygame, Kivy, Renpy), to scientific visualisation (matplotlib, Vispy, other toolkits), to novel user interfaces (Jupyter notebooks or other apps that don’t care about the native GUI toolkit for whatever reason). All of these things are tremendously popular on the desktop without using the native GUI toolkit, because that detail is either irrelevant or specifically at odds with what they want to do.

让Python应用程序无缝使用原生GUI工具包,对于使Python成为通用Android开发中Java的需要重视的替代品很重要,这一点绝对正确。然而,在Python开发的广阔领域中,这根本不是一个大问题,例如从游戏(Pygame、Kivy、Renpy)到科学可视化(matplotlib、Vispy、其他工具包),再到新型用户界面(Jupyter笔记本或其他出于某种原因不关心原生GUI工具包的应用程序)。所有这些东西在桌面上都非常受欢迎,而且都没有使用原生GUI工具包,因为这个细节(native GUI toolkit)要么无关紧要,要么与它们想要做的事情背道而驰。

History of Python for Android projects 
Android上Python项目的历史

The following projects are ones I remember as interesting and historically important in terms of creating Python applications on Android, in rough date order. This list undoubtedly isn’t complete and is strongly based on events as I remember them, I might have missed other important projects and I’m certainly missing details from before about 2012! I’ve also focused on application build tools, not the many individual patches and Python contributions that made them possible.

以下项目是我记忆中在Android上创建Python应用程序方面有趣且历史上重要的项目,大致按时间顺序排列。这个列表无疑是不完整的,并且强烈依赖于我记忆中的事件,我可能错过了其他重要的项目,而且我肯定错过了2012年之前的细节!我还专注于应用程序构建工具,而不是使它们成为可能的许多个别补丁和Python贡献。

Pygame Subset for Android (PGS4A)   
Android的Pygame子集(PGS4A)

Pygame gained rudimentary Android support fairly early in Android’s history. I’m not sure exactly what happened when, but this old release announcement is from February 2011 and isn’t the first version.

在Android的早期历史中,Pygame就获得了基本的Android支持。我不确定确切的时间,但这份旧版的发布公告是2011年2月的,而且不是第一个版本。

PGS4A worked by compiling CPython for Android, with a modified version of SDL (the gui library that Pygame uses), and some Java code to get the app to start and display a surface that pygame could draw to. I believe there were some limitations to what you could do (there’s a reason it was a "subset" of Pygame), but I’m not sure what these were.
PGS4A通过将CPython编译为Android,使用SDL(Pygame使用的GUI库)的修改版本,以及一些Java代码来启动应用程序并显示一个pygame可以绘制到其上的表面来工作。我相信你在这里能做的事情有一些限制(这就是为什么它是Pygame的一个“子集”),但我不确定这些限制是什么。

I remember people were actively using PGS4A around 2012 when I started looking at it myself, but its popularity slowly diminished over time, probably due to lack of developer support combined with its limitations.

我记得在2012年左右,人们开始积极使用PGS4A,当时我自己也开始关注它,但它的受欢迎程度却随着时间的推移而逐渐降低,这可能是由于缺乏开发者的支持以及其自身的限制。

Renpy

Ren’Py is a visual novel engine with great cross-platform support. Actually, I suspect it’s quietly one of the most successful Python game engine projects, e.g. you can find quite a few Renpy-made games available via Steam.
Ren'Py是一个具有强大跨平台支持的可视化小说引擎。实际上,我怀疑它是迄今为止最成功的Python游戏引擎项目之一,例如,你可以在Steam上找到不少用Ren'Py制作的游戏。

Renpy gained Android support a long time ago, maybe in 2011 or earlier. I believe that as with PGS4A, it worked by combining pygame with a modified version of SDL and some custom Java code to display an app surface that Renpy could draw to. I’m not sure if the Renpy build tools actually had history in common with PGS4A, but I don’t think they were the same project at any point I remember.
Ren'Py很久以前就获得了Android支持,可能是在2011年或更早。我相信和PGS4A一样,它是通过将pygame与SDL的修改版本以及一些自定义的Java代码结合起来,以显示Ren'Py可以绘制到的应用程序表面来工作的。我不确定Ren'Py的构建工具是否与PGS4A有共同的历史,但我认为在我记忆中它们并不是同一个项目。

Renpy’s Android support hasn’t been static, it’s evolved since 2011. At some point around 2014-2015 it switched to use a fork of Pygame using SDL2, with corresponding updates to the Android build process. This makes sense because SDL2 itself supports Android properly, removing a huge maintenance burden. Note that this pygame_sdl2 project is not the same as the SDL2 support currently under development from the core Pygame team, I don’t think Renpy’s pygame_sdl2 is in wide use and I don’t know what its limitations are.
Ren'Py的Android支持并非一成不变,自2011年以来一直在发展。大约在2014-2015年,它转向使用基于SDL2的pygame分支,并对Android构建过程进行了相应的更新。这是有道理的,因为SDL2本身完全地支持Android,这大大减轻了维护负担。请注意,这个pygame_sdl2项目与Pygame核心团队目前正在开发的SDL2支持不是同一个项目,我认为Ren'Py的pygame_sdl2并没有得到广泛使用,我也不知道它的限制是什么。

Kivy and python-for-android
Kivy和python-for-android

Kivy is easily the most well known Android-supporting Python toolkit I’m aware of, and has been since around 2012 when I came across it myself after failing to get a Java Android tutorial working. Kivy is a graphical toolkit that was not specifically designed for mobile support, but instead focused on being generically cross-platform and supporting novel user interfaces. This turned out very timely, as these properties made it very well suited for Android and iOS support, although Kivy also supports desktop use. Kivy draws its GUI using OpenGL, which has the advantage of working essentially the same on all different platforms, but this can also be a disadvantage in that it means not using the native GUI toolkit as discussed above.
Kivy是我所知道的最著名的支持Android的Python工具包,自2012年左右我遇到它以来一直如此,当时我无法让Java Android教程正常工作。Kivy是一个图形工具包,它不是专门为移动支持而设计的,而是专注于成为通用的跨平台工具包并支持新颖的用户界面。事实证明,这一特性非常及时,因为这些特性使Kivy非常适合Android和iOS支持,尽管Kivy也支持桌面使用。Kivy使用OpenGL绘制其GUI,这在不同平台上基本相同的工作方式具有优势,但这也可能是一个缺点,因为它意味着不使用上面讨论的原生GUI工具包。

My understanding is that Kivy’s Android support was originally based on Renpy’s Android build tools, which through some amount of collaboration and changes from different places ended up being the genesis for Kivy’s python-for-android project, first commit November 2011. Renpy’s Android build project then shifted to use a fork of python-for-android at some point, after which the projects have been developed separately.
我的理解是,Kivy的Android支持最初是基于Ren'Py的Android构建工具,通过不同地方的协作和改变,最终成为了Kivy的python-for-android项目的起源,该项目的第一次提交是在2011年11月。随后,Ren'Py的Android构建项目在某个时刻转向了使用python-for-android的一个分支,之后这两个项目就分别独立发展了。

Python-for-android was originally quite Kivy-specific, but was totally rewritten in 2015 to be a more generic and modular build tool, coinciding with Kivy’s own transition to SDL2 as a default backend (in fact this is where I really got involved). Since then its breadth of support has increased dramatically.
python-for-android最初是针对Kivy的,但在2015年进行了全面重写,成为了一个更加通用和模块化的构建工具,这与Kivy自身向SDL2作为默认后端的转变相吻合(事实上,我就是在这个时候真正参与进来的)。自那以后,它的支持范围显著增加。

Beeware

BeeWare is a collection of tools and libraries for building Python applications across different platforms, both desktop and mobile. These projects have a particular focus on manipulating the native graphical toolkits of a given platform, e.g. on Android they want to use the same "native" GUI widgets as a normal Java-built application. Its toga toolkit provides a platform-independent GUI abstraction for this, in combination with platform-specific toolkits for each individual target.
BeeWare是一系列用于在不同平台上(包括桌面和移动)构建Python应用程序的工具和库的集合。这些项目特别注重于操作给定平台的原生图形工具包,例如,在Android上,它们希望使用与正常Java构建的应用程序相同的“原生”GUI小部件。它的toga工具包为此提供了一个与平台无关的GUI抽象,并与每个单独目标的平台特定工具包相结合。

I believe the BeeWare developers (or at least the core developer Russell Keith-Magee) did some initial experimentation using CPython on Android somewhere around 2015. The idea there would be to create and manipulate the normal Java-native GUI widgets using Java Native Interface (JNI). This is actually possible, it’s something we also support in Kivy and is occasionally useful to e.g. display a webview. Unfortunately it has some key disadvantages including that Python is still quite slow to start, and in particular that Android used to enforce a fairly low limit on the number of JNI references that could be simultaneously maintained, which makes building a full GUI impractical. My understanding is that a combination of these factors made CPython use impractical for Beeware on Android.
我相信BeeWare的开发者(或者至少是核心开发者Russell Keith-Magee)在2015年左右,曾在Android上使用CPython进行了一些初步的实验。当时的想法是使用Java本地接口(JNI)来创建和操作正常的Java原生GUI小部件。这实际上是可行的,我们在Kivy中也支持这一功能,并且偶尔很有用,比如显示一个网页视图。但不幸的是,它有一些关键的缺点,包括Python启动仍然很慢,特别是Android曾经对可以同时维护的JNI引用数量实施了相当低的限制,这使得构建完整的GUI不切实际。我的理解是,这些因素的综合作用使得CPython在BeeWare的Android平台上变得不切实际。

这里插一下:

make something 后面加 形容词
 

在英语中,当“make something”后面需要添加形容词来描述被制作或创造出来的东西的特质或状态时,通常这个形容词会用来直接修饰“something”,但它并不是直接跟在“make”后面,而是跟在“something”之后。这是因为“make”是一个及物动词,它需要一个宾语(通常是名词或代词)来完整表达其意义,而形容词则用来进一步描述这个宾语。

这里有几个例子来说明如何正确使用:

  1. Make something beautiful:制作美丽的东西。这里,“beautiful”是形容词,用来修饰“something”。

  2. Make a cake delicious:制作一个美味的蛋糕。同样,“delicious”是形容词,修饰“a cake”。

  3. Make the room cozy:使房间变得舒适。这里,“cozy”是形容词,用来描述“the room”的状态。

  4. Make a plan effective:制定一个有效的计划。在这里,“effective”是形容词,修饰“a plan”。

需要注意的是,在某些情况下,我们可能会使用被动语态或不同的动词结构来表达相似的意思,但这并不影响形容词的位置。例如,“The cake was made delicious”表示“蛋糕被做得美味了”,这里“delicious”仍然是用来修饰“cake”的形容词。

另外,当形容词短语或从句用作修饰语时,它们也是放在被修饰的名词之后,例如:“Make something that is both useful and attractive”制作既实用又吸引人的东西。这里,“both useful and attractive”是形容词短语,用来修饰“something”。

BeeWare instead switched to creating VOC, a Python code to Java bytecode transpiler. This converts the Python input into genuine Java bytecode that can run as a normal app without the above limitations. I haven’t tried this for some time, but I understand it works fine. However, it seems the difficulty of supporting the full breadth of Python libraries has been a barrier (at least, that’s been my impression from watching discussions about it, I think it’s still under active development and working well).
相反,BeeWare转而创建了VOC,这是一个Python代码到Java字节码的转译器。它将Python输入转换成真正的Java字节码,可以像普通应用一样运行,而不会受到上述限制。我已经有一段时间没有尝试过了,但我听说它工作得很好。然而,支持完整的Python库库的难度似乎是一个障碍(至少,这是我从观看相关讨论中得到的印象,我认为它仍在积极开发中并且运行良好)。

Fortunately Android itself has improved, and in particular the limitation on JNI references is no longer present in recent versions. In 2019 BeeWare switched back to targeting CPython on Android, supported by a PSF grant. It is the outcome of this grant that led to the discussions and article I linked at the top. See the end of this post for a brief summary.
幸运的是,Android本身已经得到了改进,特别是在最近的版本中,对JNI引用的限制已经不再存在。2019年,BeeWare在Python软件基金会(PSF)的资助下,重新将目标转向了在Android上使用CPython。正是这笔资助的结果,才导致了我在文章开头所链接的讨论和文章。请在本文的末尾查看简要总结。

Chaquopy

Chaquopy provides build tools for both including Python code in Java applications, and building apps entirely in Python. I’m not sure about the technical details, but I’ve been consistently aware of it as an active project since about 2017, so it may be useful to anyone interested in this sort of thing. I guess there must be some overlap with what python-for-android does, but Chaquopy’s integration of Python and Java code seems to be more of a focus.
Chaquopy提供了构建工具,既可以在Java应用程序中包含Python代码,也可以完全用Python构建应用程序。我不确定具体的技术细节,但自从大约2017年以来,我一直意识到这是一个活跃的项目,因此它可能对任何对这类事情感兴趣的人都有用。我猜想它与python-for-android所做的工作肯定有一些重叠,但Chaquopy对Python和Java代码的集成似乎更加关注。

pyqtdeploy

The popular Qt graphical framework supports Android. Python bindings to this framework are quite popular, so it’s not a huge surprise that there’s some level of Python for Android support using Qt for the GUI. As far as I’m aware pyqtdeploy is the primary build tool for this, using the PyQt bindings, but I may not be up to date about it. I’ve never seen this to be very popular, but I don’t know if there’s a reason for this beyond its relative obscurity.
流行的Qt图形框架支持Android。该框架的Python绑定非常受欢迎,因此,使用Qt作为GUI来提供一定程度的Python for Android支持并不令人意外。据我所知,pyqtdeploy是使用PyQt绑定进行此操作的主要构建工具,但我也许不是最新的信息。我从未见过它非常流行,但我不知道这是否只是因为它的相对默默无闻。

What are actually the recent developments in CPython on Android?
CPython在Android上的最新发展是什么?

Various contributors have driven improvements in CPython’s Android support over the years. I would give more details but honestly I’ve never found the time to get deeply involved so the historical summary is limited by my own ignorance! This has brought CPython to the point of being fairly easy to compile for Android as of about version 3.6. For instance, python-for-android’s CPython build recipe applies no essential patches to the Python 3 source, just an appropriate set of build arguments.
多年来,许多贡献者推动了CPython在Android上的支持改进。我会给出更多细节,但说实话,我从未找到深入参与的时间,所以历史概述受到了我自己无知的限制!这已经将CPython带到了比较容易为Android编译的程度,大约从3.6版本开始。例如,python-for-android的CPython构建配方没有对Python 3源代码应用任何必要的补丁,只是应用了一组适当的构建参数。

The key recent development is the BeeWare project’s switch to CPython explained above. They’ve made a specific goal of understanding where CPython’s Android support can be improved, and getting involved to resolve these problems. This means attacking both individual technical issues (e.g. getting Python’s test suit passing correctly), and longer-term structural problems (e.g. the suggestion described in the ZDNet article to create a stripped-back Python kernel for mobile use).
最近的关键发展是BeeWare项目上述的转向CPython。他们已经设定了一个明确的目标,即了解CPython在Android上的支持可以在哪些方面得到改进,并参与其中解决这些问题。这意味着既要解决单个技术问题(例如,使Python的测试套件正确通过),又要解决长期的结构性问题(例如,ZDNet文章中提出的为移动使用创建一个简化的Python内核的建议)。

These recent developments are great, and hopefully will lead to huge improvements in the ease of deploying Python applications for Android, especially addressing the missing functionality of using the native Java GUI toolkit. However, let’s not forget the history of CPython on Android, people have been creating applications for both business and pleasure for many years.
这些最近的发展是巨大的,并有望极大地简化在Android上部署Python应用程序的便利性,特别是解决了使用原生Java GUI工具包时缺少的功能。然而,我们不要忘记CPython在Android上的历史,多年来,人们一直在为商业和娱乐创建应用程序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xinzheng新政

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值