Development of mobile applications in kivy, kivymd using buildozer. Installation, launch and problem solving

On the Internet, you can often see that developers of mobile applications using kivy and kivymd encounter certain difficulties. This article provides step-by-step instructions for developing mobile applications in kivy and kivymd. It shows the use of buildozer to build an *.apk file and the use of the adb (Android Debug Bridge) package to debug a mobile application on the phone. The article consists of two sections. The first one is about creating a *.apk file. The second considers the option of debugging a mobile application on a phone.

The instructions are relevant for Ubuntu 20.04 and 22.04 (64bit). Perhaps this will also work on newer versions of Ubuntu.

All commands were executed in the terminal of the PyCharm development environment. Python interpreter version 3.10 was used

First section
The first section provides step-by-step instructions for installing kivy, kivymd and buildozer, as well as the commands and necessary settings for creating a working *.apk file.

Install the kivy software package: pip3 install kivy

Install the kivymd software package: pip3 install kivymd

Instructions for installing buildozer are given in

Key points from the instructions:

3.1 Install the latest version of buildozer using the command: pip3 install --user --upgrade buildozer (if you are using a virtual environment, remove --user)

3.2 Next, execute the following commands:

sudo apt update

sudo apt install -y git zip unzip openjdk-17-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev libssl-dev pip3 install --user --upgrade Cython==0.29 .33 virtualenv (if a virtual environment is used, remove --user)

3.3 It is recommended to add the following line to the end of the file: export PATH=$PATH:~/.local/bin/ (I did not add it). My file is located in the ..venv/lib/python3.10/site-packages/pexpect directory

If there are problems with openjdk-17, you can install the minimally stable supported openjdk-11

In the terminal, go to the project folder (with the cd command) and create the buildozer.spec settings file with the command: buildozer init

Since we are using the kivymd package, it must be added to the buildozer.spec file. To do this, in the buildozer.spec file we find the line requirements = python3,kivy and add kivymd separated by commas. If you use any other software packages, then register them too (this is not always necessary, sometimes it works without adding).

If you need access to the Internet in applications, then in the buildozer.spec file we find the line android.permissions = ... and write INTERNET

Run the command: buildozer android clean

Run the command: buildozer android debug. We are waiting for the *.apk file to be ready. The first time compilation takes a long time.

Copy the finished *.apk file to your phone and install the application (you may need to enable “Developer Mode” on your phone (see below))

As a rule, installing the necessary packages, launching the application in the development environment (for example, in PyCharm), compiling the *.apk file and installing the application on the phone itself go without problems.
Problems start when launching the application on the phone. We launch the application - but it does not start. Why? Unclear. You can find many solutions on the Internet related to
installing different versions of kivy, kivymd and buildozer. It didn't help me.

Second section
To understand why the application does not start, it is best to run it in debug mode using the adb software package.

The whole process of how to do this can be viewed here.

Key points from the instructions:

In the buildozer.spec file we find the line p4a.branch and instead of master we write develop

Install the adb software package: sudo apt install adb

We perform the following settings on the phone (using the example of Redmi Note 9):

3.1 Set the phone to “Developer Mode”. To do this, go to “Settings” -> “About phone” -> “MIUI version” (Press with a delay four times in a row). The message “You have become a developer” should appear.

3.2 Go to “Advanced settings” -> “For developers” and enable the following options:

"Keep the screen on"

"USB Debugging"

"Installation via USB". It is very convenient not to copy the *.apk file, but to install directly via USB

"USB Debugging (Security Settings)"

In the terminal, enter the command: adb devices. Your phone device should be displayed in the List of devices attached line

In the terminal, go to the bin folder (with the cd bin/ command). Our *.apk file should be in the bin folder. Let's install the application on the phone using the command: adb -s name_device install *.apk. Where name_device is the name of the phone device from step 4, *.apk is the full name of the apk file. There is also a very useful command: buildozer android debug deploy run. It will compile the *.apk file, install the application on the phone itself and launch it immediately. The command is run in the project folder.

Now enter the command for debugging: adb -s name_device logcat *:S python:D. Next, the adb software package will be active
Made on