How to convert kivy/kivymd app to apk in Windows?
Tags: python, android, kivy, kivymd
Published: 2021-09-01 16:31:44

> 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](https://colab.research.google.com/notebooks/intro.ipynb).

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:
```linux
!sudo apt update
```
```linux
!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
```
```linux
!pip3 install --upgrade Cython==0.29.19 virtualenv
```
```linux
!export PATH=$PATH:~/.local/bin/
```
```linux
!pip install buildozer
```
```linux
!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
```
```linux
!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
```
```linux
!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
```
```linux
!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:
```linux
!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](https://github.com/kivy/python-for-android) to avoid problems such as not showing some symbols.
```linux
!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:
```linux
!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](https://github.com/mehmet-karagoz/Learn-New-Language-With-Srt-Files) that I made using kivymd.

Mehmet Karagoz.

## Resources
[Buildozer Installation](https://buildozer.readthedocs.io/en/latest/installation.html)
[Spec file](https://buildozer.readthedocs.io/en/latest/specifications.html)
[Running](https://buildozer.readthedocs.io/en/latest/quickstart.html)
[p4a](https://buildozer.readthedocs.io/en/latest/contribute.html)
[in addition](https://towardsdatascience.com/3-ways-to-convert-python-app-into-apk-77f4c9cd55af)