[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):

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

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:

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:

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:

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:

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

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

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:

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