I’m Aron, from Byte::Debugger();, and in this post we are going to see how to compile Python targeting Android with Ubuntu(tested on Ubuntu 14.04) and Buildozer!! This process works with others Linux distros, but I’ll need to find the appropriate package. So, let’s start.
I – Installing buildozer
Buildozer itself doesn’t depend on any library, and works on Python 2.7 and >= 3.3. I recommend you to use Python 2.7.
First, we need to download PIP, a tool for installing and managing Python packages.
# apt-get install python-pip
Now, to install buildozer run:
pip install --upgrade buildozer
If everything works fine, you will see the output: Successfully installed buildozer
II – Targeting Android
If you target Android, you must install at least Cython, few build libs, and a Java SDK. Some binaries of the Android SDK are still in 32 bits, so you need few 32bits libraries available.
First, we need to install Cython:
# apt-get install python2.7-dev # pip install --upgrade cython
Now, make the 32 bits lib available:
# dpkg --add-architecture i386
After it, you can update and install the necessary packages:
# apt-get update # apt-get install build-essential ccache git lib32z1 libncurses5:i386 libstdc++6:i386 python2.7 openjdk-7-jdk unzip zlib1g-dev zlib1g:i386
Ok, now we have everything to start with Python development for Android!! Let’s try it!
III – Simple example
Let’s create a hello world to test if everything is working. In this hello world, I’m going to use kivy. If you don’t have Kivy installed, run the following command:
# apt-get install python-kivy
Now, create a new file with the name main.py, and make sure to add the line __version__ = “1.0” in your file, because buildozer will try to find this line to determine the app version.
To test it, write this simple hello world:
__version__ = “1.0” from kivy.app import App from kivy.uix.button import Button class Hello(App): def build(self): btn = Button(text='Hello World') return btn Hello().run()
Now, open your terminal and go to the main.py folder. After it, create a buildozer.spec file, with:
You can open the buildzoer.spec file and change your app settings, as the name, developer, and this kind of stuff.
To build your project, run:
buildozer -v android debug
This command will download python-for-android, Android SDK and Android NDK for you. If it’s the first time that you are building, you can go and drink some coffee, because it need to download a big chain of libs and build it, so if your computer/internet is slow, it can take a long time. I’m going to drink a cup of coffee, I hope to see you soon! If you don’t have coffee or are tired of waiting, take a look in my blog, here you can find some tutorials and examples about coding: Python Mobile, Java Mobile, Processing IDE, Firefox OS, Arduino, and this kind of stuff.
After it, you everything works fine, you will see the message:
# Android packaging done!
# APK MyApplication-1.2.0-debug.apk available in the bin directory
Now, you can get the APK in the bin folder and install it in your device! If your device is connected in the USB, just run:
adb install bin/MyApplication-1.2.0-debug.apk
To install it!
The example app:
That’s all, if you have any problem, suggestion, or anything, fell free and comment here! Thx for reading!