Python onAndroid

Posted onbyAlexander Taylor

There are an increasing number of resources about different ways of running Python on Android. Kivy (and its subprojects) are commonly mentioned, as one of the most mature and popular ways to do so, but one thing that gets less attention is the details of what you can do with Python itself once it’s running on the device - what are the limitations of this? Can we use any Python module? What about calling Android APIs, can we perform all of the functions of a Java application? These are all somewhat leading questions, they are things addressed by Kivy or its associated projects, and in this post I’ll summarise some of the most interesting and importantdetails.

python-for-android

Before anything else, let’s look quickly at the tool Kivy actually uses to get Python on Android; the unimaginatively-namedpython-for-android project. The basic functionality of this tool is to first build adistribution, an Android project directory that includes all the components Kivy needs to run, compiled for Android by itsNDK. This includes in particular the Python interpreter itself, plus Kivy and the libraries it depends on - currently Pygame andSDLamongst others, although we are working to modernise this bit. The distribution also includes a Java bootstrap, a normal app structure whose job is to display Kivy’s OpenGL surface and to mediate between Kivy and Android. All these components can then be bundled into anAPKwith the user’s Python script and different settings (icon, name, orientation etc.) totaste.

This is only the basic procedure, theAPKcan (and does) include much more than just these essentials. Amongst other things, most of the Python standard library is built in by default, and pure Python modules can be included easily so in general you can perform tasks using just the same libraries you would on the desktop. Libraries with compiled components are more complex, but can be built and included as long as python-for-android has a compilation recipe for them (or you provide your own) - these are often quite simple, just setting some compilation flags and running the normal build procedure, although some modules need additional patching. Python-for-android includes quite a few recipes by default, including very popular modules like numpy, sqlite3, twisted and evendjango!

The above is the basics of how python-for-android works but is far from the whole story, and you can check the documentation for more information about building your own APKs - in particular, we recommend usingBuildozer, which gives python-for-android a more convenient interface and can manage some dependencies (in particular the AndroidSDKandNDK) automatically. This is also quite focused on Kivy itself, but we’re trying to move to make it easier for other projects to use the same toolchain - the core process of building and including Python should be similar, but there’s no need for the bootstrap app at the end to support only Kivy’s specificneeds.

Calling Android APIs withPyJNIus

In normal Android application development, interaction with the AndroidAPIis an important part of how your app behaves - getting sensor data, creating notifications, vibrating, pausing and restarting, or just about anything else. Kivy takes care of the essentials for you, but many of these are things you’ll still want to manage yourself from Python. For this reason we have thePyJNIusproject, also developed under the Kivy organisation, which automatically wraps Java code in a Pythoninterface.

As a simple example, here’s the Python code to have an Android device vibrate for10s:

from jnius import autoclass # We need a reference to the Java activity running the current # application, this reference is stored automatically by # Kivy's PythonActivity bootstrap: PythonActivity = autoclass('org.renpy.android.PythonActivity') activity = PythonActivity.mActivity Context = autoclass('android.content.Context') vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE) vibrator.vibrate(10000) # the argument is in milliseconds 

If you’re familiar with the AndroidAPI, you’ll notice that this is very similar to the Java code you’d use for the same task; PyJNIus just lets us call the sameAPIdirectly from Python. Most of the AndroidAPIcan be called from Python in the same way, letting you achieve the same things as a normal Javaapplication.

The main disadvantages of using PyJNIus directly are that it requires some understanding of how the AndroidAPIis structured, and that it is quite verbose - though the latter just reflects the nature of the equivalent Java code. For this reason, the Kivy project set includesPlyer.

Plyer: A platform-independentAPIfor platform-specificfeatures

ThePlyerproject takes a step back from the specific implementation details of individual platforms in order to try to create a simple, pythonic interface for a subset of (mostly) shared functionality. For instance, the vibration example above wouldbecome

from plyer.vibrator import vibrate vibrate(10) # in Plyer, the argument is in seconds 

Further, Plyer is not just for Android but would try to do something appropriate on any of its supported platforms - currently Android, iOS, Linux, Windows andOSX (on iOS,PyOBJusfulfils a similar role to PyJNIus on Android). The vibrator is actually a bad example as only Android is currently implemented, but other APIs such as checking the battery (from plyer import battery; print(battery.status)) or text-to-speech (from plyer import tts; tts.speak('hello world')) would already work on both desktop and mobile devices, and others such as the compass or gyroscope sensors or sendingSMSmessages would work on both Android andiOS.

Plyer is very much under development, with newAPIwrapper contributions very welcome, and is the subject of a (second) GSoC project this year. We hope that it will become increasinglyfeature-complete.

Not just forKivy

All of these tools have been shaped in their current form by the needs of Kivy, but are really more generic Python tools; Plyer specifically avoids any Kivy dependency, and PyJNIus only makes an assumption about how to access theJNIenvironment on Android. We hope that these tools can be more generally useful to anyone running Python on Android; for instance, you can already experiment with PyJNIus using theQPython Android app. Python-for-android is more tied to Kivy’s current toolchain but this is a detail under review, and we’re happy to discuss the details of Android compilation with anyoneinterested.

Overall, a lot is possible with Python on Android, despite how different the Python environment is to the Java development that is directly targeted. But there’s much more that could be done - if you’re interested, now is a great time to divein!

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android动作传感器
  2. 用android LinearLayout和RelativeLayout
  3. Android开发中使用矢量图
  4. 关于Android中的消息机制和异步
  5. android 屏幕分辨率问题
  6. Android(安卓)java层音频相关的分析与理
  7. Android(安卓)深色模式适配
  8. Android大赛首轮获奖作品解析。。。
  9. 有感于android安装的简单
  10. 解读Android(安卓)LOG机制的实现:(6)c/c++域