Embedding Python in Android

Tutorial series

April 21, 2014

In the last months I have been implementing a Python library for a cross-platform application of mine. I've chose Python for many reasons: it is cross-platform, allows for fast development, has very concise code and great standard library, has many extensions, it is quite popular, and allows to interface with C/C++ using projects such as Cython. Cython lets you write extensions in C/C++ or interface with existing C/C++ libraries in a relatively easy way.

For GUI on the desktops, I've been using PySide, which are the Python wrappers for the Qt framework. For GUI on Android and iOS, I have successfully used Kivy which is a Python framework that allows you to write cross-platform applications very fast. However, on one hand, PySide for desktop is pretty much a dead end. It is stuck on Qt 4.8, and it seems no one will ever port it to Qt 5. I guess it is because no one will get to the trouble of learning how to use the pretty much unknown Shiboken generator. PyQT is not an alternative for indie commercial applications because of their licensing scheme. On the other hand, regarding Android/iOS, Kivy is not really suitable for what we call "business applications". The real problem of Kivy is that you can not use the Android/iOS GUI API's, which pretty much leaves you without native GUI interfaces on those Operating Systems. In my point of view, if you need native graphical user interfaces, don't use Kivy, else, use it definitely!

I've taken the decision that I do want to use native GUIs on my application, on the desktop and on smartphones/tablets. One hypothesis was to rewrite my library entirely in C++, so I could interface directly with Qt in the desktop (Qt is written in C++), and with Java in Android (through JNI) and Objective-C in iOS (through Objective-C++). However, I've decided that I really like Python, and instead of rewrite everything in C++, I will keep my library and all possible common code in Python, create a Python interface to be called from C++ code, and interface my C++ code with the desktop/tablet/phone GUI's. The idea is something like the following:

So, I want to have the Python interpreter embedded in C/C++ with all the common code I need for my application. Then for each platform, I will use glue code to be able to interface the GUI with my C/C++ code (and my Python embedded library/libraries). For the desktops, the interface is immediate. I just need to have an application written with Qt accessing directly the C/C++ code. For iOS, I will need to write my GUI in Objective-C and use Objective-C++ to call C/C++ code. In many cases, Objective-C++ is just a renaming of ".cpp" to ".mm" and a recompile. For Android, I will just need to write my GUI in Java, and call my C/C++ common code through JNI.

In the following articles, I will show how to embed a Python interpreter in Android, and how to interface it with Java code using JNI.

  1. Java Native Interface
  2. Embedding the Python Interpreter
  3. Adding log to the Python Interpreter
  4. Include Python Standard Library
  5. Compile C++ Cython Extensions
  6. Compile C Cython Extensions