How to convert kivy/kivymd app to apk in Windows?

In this article, I will explain how we can convert a cross-platform application made using kivy/kivymd in python on Windows to an APK file without the need for a Linux machine. For this, I will use the online platform called Google Colab.

After logging into google colab, let's prepare the environment for the conversion process. Do not forget to upload the project you are working on. Follow these commands step by step:

!sudo apt update
!sudo apt install -y git zip unzip openjdk-8-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev libssl-dev
!pip3 install --upgrade Cython==0.29.19 virtualenv
!export PATH=$PATH:~/.local/bin/
!pip install buildozer
!sudo apt-get install -y \
    python3-pip \
    build-essential \
    git \
    python3 \
    python3-dev \
    ffmpeg \
    libsdl2-dev \
    libsdl2-image-dev \
    libsdl2-mixer-dev \
    libsdl2-ttf-dev \
    libportmidi-dev \
    libswscale-dev \
    libavformat-dev \
    libavcodec-dev \
    zlib1g-dev
!sudo apt-get install -y \
    python3-pip \
    build-essential \
    git \
    python3 \
    python3-dev \
    ffmpeg \
    libsdl2-dev \
    libsdl2-image-dev \
    libsdl2-mixer-dev \
    libsdl2-ttf-dev \
    libportmidi-dev \
    libswscale-dev \
    libavformat-dev \
    libavcodec-dev \
    zlib1g-dev
!sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev zlib1g-dev libssl-dev openssl libgdbm-dev libgdbm-compat-dev liblzma-dev libreadline-dev libncursesw5-dev libffi-dev uuid-dev libffi6
!sudo apt-get install libffi-dev

Now our environment is ready. We can start the conversion process. We will use buildozer for this. Run this command to start Buildozer:

!buildozer init

This command will create a configuration file named buildozer.spec for us. Now it's time to edit this file. At least you should change the;
title, package.name, package.domain.

After changing these, my suggestion is to make some more changes to avoid some problems. Let's look at source.dir first, if your main.py file is in the same directory, there is no need to change it, but if not, you should write the path here.

Afterwards, if you have used separate files such as png, txt, csv in the program, you should add the extension to source.include_exts.

Now let's change the most important, requirements. If you only used kivy, python3,kivy==2.0.0rc4 will be enough to do it this way.If you also used kivymd then you should add this https://github.com/kivymd/KivyMD/archive/master.zip. If you have used other libraries, run the pip install <library-name> command, and it will show the downloaded packages. You can add them without specifying the version.

If you want to use icon you can uncomment icon.filename and you can edit the path.

If you want your app to be fullscreen, you can make fullscreen 1.

We will need to clone python-for-android to avoid problems such as not showing some symbols.

!git clone <git-link-of-forked-repo>

Then we need to edit it in spec file.p4a.source_dir = /path/to/your/python-for-android and p4a.branch = develop.

Finally, if you need permissions on android you have to write them.We are done with spec file for now.

Now we can start the conversion process by this command:

!buildozer -v android debug

When you first run this process, it may take up to 15-20 minutes depending on your internet speed. When the process is finished, your bin/*.apk file is ready and you can download it to the phone.

If the application does not work, you can create a log file from the phone. For this, try to run the application after clicking on the settings / developer option / bug report. After waiting for a while, it will also show the process in the notification bar. You can now see the errors by sharing it to your computer.

You can also check out my project that I made using kivymd.

Mehmet Karagoz.

Resources

43