PySide for Android

Update 2013: MartinK has improved upon this approach and released new instructions and new downloads for running PySide applications in Android, including Qt Components. Give it at try: MartinK's PySide for Android Page

If you want to write GUI applications for Android using Python, you did not really have many options, apart from generating and displaying HTML with SL4A. Until now. Combining the power of three projects, PySide for Android lets you write Qt or QML UIs for Android - all in pure Python. The projects on which PySide for Android builds upon are:

gPodder in Android Emulator




Python for Android

Mostly following the instructions of the Py4A project.

PySide for the Desktop

We need the Desktop build to run the API Extractor, Generatorrunner and Shiboken.

PySide for Android

We need this to build the bindings, libpyside and libshiboken natively for Android.

PySideLauncher (C++ project)

This is the C++ launcher that will include libpyside, libshiboken, libpython and will set everything up and finally load the from the SD card (or any other script).

Fixing up library dependencies

libpyside and the PySide binding modules have a shared library dependency on libshiboken. However, the dependency is not on the .so library itself, so the linker does not find it. To fix this, we have to patch the name of the libshiboken dependency in libpyside and also in all binding modules.

Getting updated/compatible Qt libs

Our PySide build is not compatible with the Qt libraries downloaded by Ministro, so we have to take the Qt libraries from the Necessitas SDK, strip them (otherwise they are huge and will not fit into your device's memory) and copy them over the files downloaded by Ministro. At least the Qt libraries and the Android platform plugin need to be replaced.

Building PySideLauncher with the Python and PySide libraries

I haven't been able to force the Necessitas Qt Creator to include extra shared libraries in the APK, so we'll have to "inject" these libraries manually into the Android project and then rebuild the Android project using ant on the command line (if we try to do it using Qt Creator, it will overwrite the libs.xml and remove the libraries).

After building PySideLauncher this way, we have an APK that includes libpython, libshiboken and libpyside - ready for deployment (the Python libraries, Qt libraries and PySide binding modules are installed directly to the system, and are not shipped with the APK).

Deploying to the emulator/device

Important: If you are using PySideLauncher to launch your application, you must have a file on your SD card, because that is what PySideLauncher opens. If you don't have a file on your SD card (or your fails for some reason), PySideLauncher will not show anything. In that case, please try the example to check if you have everything installed correctly, and if this works, then try modifying the file and/or load your code there. The way the example is structured is so that any errors that you get are written to the SD card, which helps in debugging (right now, errors are not logged in logcat).

Deploying to a rooted Nexus S (2012-02-10)

host$ adb shell
$ su
# mkdir /data/python
host$ adb push bin /data/python/bin
host$ adb push lib /data/python/lib
host$ adb shell
$ su
# chmod 755 /data/python/bin/python
# LD_LIBRARY_PATH=/data/python/lib/ data/python/bin/python
Python 2.6.2 (r262:71600, Jul  4 2011, 13:24:39)
[GCC 4.4.3] on linux-armv7l
Type "help", "copyright", "credits" or "license" for more information.
host$ adb push /sdcard/
host$ adb install PySideLauncher-debug.apk
(... click the PySide icon and install Ministro from Market ...)
(... make sure Ministro downloads the Qt libraries ...)
(... click the PySide icon again and it should display "Hello from PySide Cool!" ...)

It turns out that as of 2012-02-10 (and probably a bit earlier too) you don't need to extract the PySide-compatible Qt libraries from the SDK - the Ministro libraries are now up to date and can be used with the PySide modules :)


If you do not want to build everything yourself, here are prebuilt binaries for you to get started:

Here are some files required to build everything from source (make sure to fix the paths in the file to point to the correct location):

2011-07-07 Thomas Perl | contact information | up-dir |