Python on Android

Source: Internet
Author: User

Python on AndroidPosted on April by Alexander Taylor

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

Python-for-android

Before anything else, let's look quickly in the tool Kivy actually uses to get Python on Android; The Unimaginatively-named python-for-android project. The basic functionality of this tool are to first build a distribution, an Android project directory that includes All of the components are Kivy needs to run and compiled for Android by its NDK. This includes in particular the Python interpreter itself, plus Kivy and the libraries it depends on-currently D SDL amongst others, although we is 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 is bundled into a APK with the user ' s Python script and different settings (icon, name, Orientation etc.) to taste.

This is a basic procedure, The apk can (and does) include much more than just these Essentials. Amongst other things, most of the "The Python standard library is built" by default, and pure Python modules can be included Easily so in general you can perform tasks using just the same libraries your would on the desktop. Libraries with compiled components is more complex, but can is built and included as long as python-for-android has a COM Pilation recipe for them (or you provide your own)-these is 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 an D even django!

The above is the basics of what python-for-android works but are far from the whole stories, and you can check the Documentati Information about building your own apks-in particular, we recommend using Buildozer, which gives Python-for -android a more convenient interface and can manage some dependencies (in particular the Android Sdkand NDK) autom Atically. This was also quite focused on Kivy itself, but we ' re trying to move to make it easier for other projects to use the same t Oolchain-the core process of building and including Python should be similar, but there's no need for the bootstrap app At the end of support is only Kivy ' s specific needs.

Calling Android APIs with Pyjnius

In normal Android application development, interaction with the Android API is a important part of how your app Behav Es-getting sensor data, creating notifications, vibrating, pausing and restarting, or just about anything else. Kivy takes care of the essentials for your, but many of these is things you ' ll still want to manage yourself from Python. For this reason we have the Pyjnius project, also developed under the Kivy organisation, which automatically wraps Java Co De in a Python interface.

As a simple example, here's the Python code to any of the Android device vibrate for 10s:

FromJniusImportAutoclass# We need a reference to the Java activity running the current# application, this reference was stored automatically by# Kivy ' s pythonactivity Bootstrap:Pythonactivity=autoclass ( ' org.renpy.android.PythonActivity ' activity = pythonactivity. Mactivitycontext = autoclass ( " Android.content.Context ' ) vibrator = activity< Span class= "O". getsystemservice (context. Vibrator_service) vibrator. Vibrate (10000) # the argument is in milliseconds  

If you're familiar with the Android APIs, you'll notice that's very similar to the Java code you ' d use for the SA Me task; Pyjnius just lets us call the same API directly from Python. Most of the Android API can is called from Python in the same, letting you achieve the same things as a normal Jav A application.

The main disadvantages of using Pyjnius directly is, it requires some understanding of how the Android API is str Uctured, 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 includes Plyer.

Plyer:a platform-independentAPI for Platform-specific features

The Plyer project takes a step back from the specific implementation details of individual platforms in order to try to CR Eate a simple, pythonic interface for a subset of (mostly) shared functionality. For instance, the vibration example above would become

Vibratevibrate(ten# in Plyer, the argument are in seconds   

Further, Plyer is isn't just for Android but would try to do something appropriate on any of its supported PLATFORMS-CU rrently Android, IOS, Linux, Windows and os x (on Ios,pyobjus fulfils a similar role to Pyjnius on Android). The vibrator is actually a bad example as only Android are currently implemented, but other APIs such as checking the Batte Ry ( from plyer import battery, print (battery.status) ) or text-to-speech ( from Plyer import TTS; Tts.spe AK (' Hello World ') ) would already work on both desktop and mobile devices, and others such as the compass or Gyrosco PE sensors or Sending sms messages would work on both Android And ios.

Plyer is very much under development, with new API wrapper contributions very welcome, and is the subject of a (second ) GSoC Project this year. We hope that it'll become increasingly feature-complete.

Not just for Kivy

All of these tools has been shaped in their current form by the needs of Kivy, but is really more generic Python tools; Plyer specifically avoids any Kivy dependency, and Pyjnius only makes a assumption about what to access the JNI Enviro Nment on Android. We hope that these tools can is more generally useful to anyone running Python on Android; For instance, you can already experiment with Pyjnius using the Qpython Android app. Python-for-android are more tied to Ki Vy ' s current toolchain It's a detail under review, and we ' re happy to discuss the details of Android compilation WI th anyone interested.

Overall, a lot are possible with Python on Android, despite how different the Python environment are to the Java development That's directly targeted. But there's much more that could being done-if you ' re interested, now was a great time to dive in!

Python on Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.