Pages

Packaging and deploying a python App for Android

Deploying an application for real-time users is the most crucial part of software development. If anything goes wrong at this point, your entire effort to write the lengthy code will go in vain. In our previous blog, we wrote code to design a basic mobile app with the Kivy Python Framework. Now we will move ahead and learn to deploy the same on an Android device. 


The only way to share your code with others is to deploy it and turn it into an Android application. We will start installing a buildozer package with pip:


$ pip install buildozer


Now we have to create a new folder to navigate it in the terminal. Inside the terminal, run the below command to create a buildozer.spec file that we’ll use to configure our build. 


$ buildozer init


We can also do this by editing the first few lines of the spec file as we have done in the below command:


[app]


# (str) Title of your application

title = KvCalc


# (str) Package name

package.name = kvcalc


# (str) Package domain (needed for android/ios packaging)

package.domain = org.kvcalc



You are getting very close to being ready to build your application at this point; however, before you get started, you will need to make sure that buildozer’s dependencies are set up properly. Once those have been installed, rename your calculator application's file to main.py and then copy it into your new folder after installing it. This is a prerequisite for using buildozer. The build will fail if you do not have the file named correctly in the source code. 


Run the following command on your device to run the build. 

$ buildozer -v android debug


The build will take around 15 minutes or a little longer, depending on the configuration of your device. Meanwhile, Buildozer will automatically download the required Android SDK pieces. If everything goes according to plan, you will see a kvcalc-0.1-debug.apk. named file in your bin folder. 


Now it's time to connect your Android device to the computer and copy the apk file onto the Android device. Open the file browser on your Android phone and click on the apk file to run it. Allow access to the device to install the application. 

Ignore the installation warning. You'll get it anyway because you're downloading it from somewhere other than Google Play.


Once the app is installed, you can run the calculator app on your device, and it will look somewhat like the below image.



Final thoughts:

This is the easiest way, in my opinion, to put a Python app on Android. Other than this, Python developers can also use Python-for-Android (P4A), which is an excellent tool designed specifically for this purpose. P4A is a Python-to-Android compiler that allows you to package a Python app into an Android APK file. It supports most popular Python libraries, including Kivy and Pygame. It also provides a simple GUI and command line interface, making it easy to package a Python app for Android. In this article we have used Kivy Python Framework for coding. 



No comments:

Post a Comment

Make new Model/Controller/Migration in Laravel

  In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here w...