[HowTo] [Python/Kivy] Building APK With Buildozer

In the previous post, how to install Kivy and Buildozer on Ubuntu is examined. In this post, you will find stages making APK files on Ubuntu with Python and Kivy using Buildozer. If you have followed the previous tutorial, you have already installed Buildozer on your virtual environment; however, it is not ready to making APK files for now. To test this statement, create a folder with a very basic main.py file(example from Kivy.org mainpage):

mkdir codeToCompile
cd codeToCompile
nano main.py

You can also use your favourite editor to create and edit main.py file. In this file, put this content:

from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
    def build(self):
        return Button(text='Hello World')


This is a very basic app which contains a button and a text as “Hello World” on it. The button does nothing when you press it, only a background color change is made to indicate the button is pressed which is a part of Kivy’s default button interface. You are encouraged to test the code before trying to compile it due to the chance that there may errors on your code and this may effect its compilation or running on the phone.

After this step, you can initialize Buildozer in this folder:

buildozer init

At this point, you can find a file named buildozer.spec in current folder. You can change a lot of options for your app, compilation process and compilers. You are advised to spare some time to inspect this file.

The building for Android can be made as follows:

(venv)itugnu@gnu:~/compileCode$ buildozer android debug
# Check configuration tokens
# Ensure build layout
# Check configuration tokens
# Preparing build
# Check requirements for android
# Java compiler (javac) not found, please install it.

It is highly advised to change log_level in buildozer.spec to debug in order to see what is going wrong. In above, java compiler was not installed and it was the reason of error. But in time, you may face with different errors and debug mode will be very helpful to solve them. In buildozer.spec file, make the log_level part as follows:


# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
log_level = 2

The version control should be made in order to use Buildozer. It supports 2 different versioning numbers which you can see in buildozer.spec file. Here, one of them are used as follows:

# (str) Application versioning (method 1)
#version.regex = __version__ = ['"](.*)['"]
#version.filename = %(source.dir)s/main.py

# (str) Application versioning (method 2)
version = 1.2.0

Afterwards, there are some dependencies of Buildozer which should be installed as follows:

sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install build-essential ccache git lib32z1 libncurses5:i386 libstdc++6:i386 python2.7 python2.7-dev openjdk-7-jdk unzip zlib1g-dev zlib1g:i386
cd ..
venv/bin/pip install markupsafe

After this step, you should be ready to generate APK files from Python codes. Try running this:

source venv/bin/activate
cd ~/codeToCompile
buildozer -v android debug

For the first time, Buildozer gets SDK, NDK and other dependencies; moreover, it compiles Python for Android and necessary packages. This may take really long time. If there are any errors, you should fix them. The commands above are arranged not to give errors, hopefully.

In order to send APK code to your phone and install it, Buildozer has deploy parameter. It also supports running application from one command by utilizing run option. So if you want to compile, upload, install and run your code on your phone, you can use this command:

buildozer android debug deploy run

You can also set buildozer to open logcat automatically afterwards the app is started, or you can open it manually. Make sure that your phone’s developer options are activated so that adb finds your device and has the right of installing APK file on it.

Further reading & Resource:

Buildozer Readthedocs

comments powered by Disqus