3 Ways to Convert Python App into APK

Concluding Building Android Apps in Python Series!

Kaustubh Gupta
Towards Data Science

--

In August 2020, I started a medium blog series called Building Android App in Python, where I explained the usage of Kivy and Kivymd. These libraries allow you to create cross-platform apps using Python. In that series, I described how android apps are configured in Python, it’s limitations, and various key elements that build up an android app. At the end of the series (3rd part), I mentioned that I will convert an app into APK and deploy it on a cloud platform. Here is the article about the same! Here I will discuss three ways in which this conversion can be performed. If you haven’t followed this series or you are unaware of this library then I highly recommend you that check out the first part of the series:

The Challenge

The Python apps build with Kivy cannot be directly transferred to android phones as these devices only support APK (Android Application Package) and we need to package them properly. This conversion process is only possible on a Linux system (for now) as the main components of this conversion, buildozer and python-for-android are currently supported on Linux based systems only. This adds up a challenge for budding developers who generally who use a windows machine for all the coding purpose. Other challenges include failed app conversions, app crashing on the start, or not able to connect to the internet. While some issues need extra attention from your side, I will provide you 3 different ways to successfully convert the Python app to APK.

Before moving ahead, let’s look at the flow of the conversion:

  1. Making sure that the app entry point file is named as main.py
  2. Installing the dependencies.
  3. Initialize the buildozer
  4. Edit the specs file
  5. Start the process

We will see 1,2 and 4 steps in detail in upcoming sections as they don’t require any explicit change but it is necessary to understand how to configure the buildozer specs properly.

The buildozer spec file is automatically generated while initializing the buildozer. The file contains the whole configuration and the app is built following this only. There are a few lines that need to be modified in that file before proceeding with the next steps (There are a total of 339 lines in the actual file):

These lines are not in actual order and have been picked from the generated file

Give your app a suitable title, a package name which can simply be the lower case, concatenated version of the title, package domain which can start with com or org, then list the requirements of your app and finally uncomment the internet permissions line in case your app requires it. A small piece of advice, use kivy and kivymd defined modules to avoid any app crashing error.

1. The Hard Way: Setup Virtual Linux

The first very obvious way is to set up a virtual machine, spin up a Linux distribution, and install all the dependencies. This way is a little tricky because:

  1. Not all of us have high-performance machines that can handle virtualization. To run a virtual machine, at least 4GB of RAM would be required to be allotted to this virtual system to properly fire up the machine.
  2. The process of configuring the system which involves installing the Linux, setting up the dependencies, transferring the data from the host system to the virtual machine is time-consuming.

You can set up the virtual box and install any Linux distribution in that. The kivy developers have simplified to install all the dependencies at one go by running a bash script that installs python and other requirements to build the app. In the terminal,

wget https://github.com/HeaTTheatR/KivyMD-data/raw/master/install-kivy-buildozer-dependencies.sh
chmod +x install-kivy-buildozer-dependencies.sh

./install-kivy-buildozer-dependencies.sh

After the script is done installing, navigate to your app directory and run:

buildzoer init

This will create the buildozer spec file which consists of all configurations you can modify. After you are done editing this file, run:

buildozer android debug

For the first time, this command will take longer than usual to execute (14–19 minutes) and after that, you will have a bin folder in the app directory with the APK. Transfer the app to any android device and see if works!

If you opt this way then you will have a configured VM that can be used for other apps to be converted to APKs in less time. You can also run these installations in WSL (Windows Subsystem Linux) in Windows 10 if you don’t want to opt for a virtual box setup.

2. The Google Colab Way!

The conversion requires a Linux environment and Google provides it for free! If you belong to Data Science or Deep learning background then you must be aware of this platform. It provides you a virtual machine with 75GB space, 12GB RAM, and around 12GB GPU power! You can use this platform to perform model training, checking logs, or running Python codes.

As this is a Linux based system, now the only thing required is to install the dependencies and initiate the process. You can directly use my colab notebook where you just need to run all the cells, that’s it!

The notebook code is originally provided by Machine Learning Hub. Check out their video!

Before running the cells, make sure to upload your app code to the colab notebook and after running the bulldozer init command, make sure to edit the specs file generated and nothing else!

This is the easiest and most convenient way to build apps without the need for an actual system! But can we go even more simple? Let’s see

3. The GitHub Action Way!

Ahh, it also provides a Linux system but only when the process is getting executed. Don’t know much about this? After you are done with this article make sure to check out this article!

Github Actions are the piece of software that lets you build, test, and deploy your code right from your GitHub repository. You can schedule jobs for various events occurring in your GitHub repository such as pushing new code, commits, or opening an issue. You can schedule either on basis of these events or use a corn job to set a timer. These GitHub actions run on a Linux system and therefore you can configure the various parameters for the conversion.

The best part is that the developers of Kivy are kind enough that they have created this action too! You just need to include this workflow in your .github/workflows/<any name>.yml file:

In this method, you need to place the buildozer spec file also with your app files. You can find any spec file online and after that just copy-paste that, edit it, upload the files to the repository and then add this workflow. After you run this workflow, your app will be created as an artifact that can be easily downloaded. You can also configure it to upload the APKs on another branch of the repository. Check out the official GitHub page for all the configurable parameters.

Any Examples?

I have prepared two apps for this article. One of them is a Machine Learning Deployment which predicts a song genre based on the given parameters. The second one is a very basic authentication app we build in the second part of the series. Here is the video where I am running these apps on my personal Android phone:

GIF by Author

The source code for both the apps (Python file, APK, and buildozer spec) can be found at this GitHub repository.

If you don’t want to use the APK generated into your devices then you simply host them online using a free service called Appetize.io which allows you to run android apps in browsers!

Image by Author. Try this app here.

Conclusion

In this article, we saw how to configure the deployment file needed to convert the Python file into APK. Then we saw 3 different ways to do this conversion. A local machine is the best option but if don’t want to mess with your system then I would suggest Google Colab as it has great processing power. If you are looking for CI/CD type solution then GitHub actions will suit your requirements. With this, we come to an end of this article as well as the development series.

If you don’t want to miss such articles then make sure to follow me on medium to receive all the notifications. With that said, Sayonara!

You can connect with me here:

My other popular articles:

--

--

Data Analysis | Python-Dev — Working on Data related Projects, Python, interested in real-world applications | 0.8M+ views