You have completed an idea into a working Python code in desktop PC. If you wonder, whether the same code can be run in a mobile environment which is different in architecture, screen size, interaction method and file system, this post is for you. Here we explain various methods available to convert a python code to an app that will work in mobiles such as Android or iOS
First you need to decide whether you want to depend only on python alone as a programming language or you want to leverage other languages like Javaย or Kotlin. Based on your choice, you have two different approaches as listed below to convert python code to a mobile App
Table of Contents
Two procedures to convert python code to a mobile App
The available procedures to convert python code to a mobile app are
- Converting the python code to a working mobile app
- Calling python modules from Java or Kotlin through a plugin
Best thing about these two approaches is they are completely free.
First approach does not need you to learn any languages other than Python. You can even work on the gui part using python itself. There are python frameworks available such as Kivyย andย KivyMD which will provide native looking gui for mobile apps that have been converted from python code. Even the conversion does not need the use of Android Studio. The package builders such as Python for android and Buildozer exist to convert the python code to a working mobile App. While buildozer and python for android does the same thing, buildozer makes it easy for you by using a text file based entries. Buildozer is also using the python for android for the conversion. Buildozer collects all the entries and then call the python for android to convert the code. While using the buildozer is easier, using the python for android directly will give you much more control. Another important thing is that Mobile OS such as Android does not have python installed. But you want to run Python code in the mobile. Eventually, next question arises. How to install python in mobile . You need to make sure that the python interpreter is packed into the mobile app. While this looks heavy duty for a programmer, you do not need to worry. The package builders mentioned above will do the heavy duty for you and will do all the work necessary to build the app. You just need to focus on the python code alone. We have developed Apps for mobiles using python as backend. Please refer these pages also.
Unfortunately, buildozer is only available for the linux platform. If you are a windows user, you can either install a virtual machine inside windows to load Linux or you can use cloud machine called colab which is free to use. In Colab, you can install buildozer and convert your python code to apk for free from windows using browser itself. Watch the videos given below for more details . You also have an option of using Github Actions for the conversion. Watch the video on How to build and Package Kivy Android Application using GitHub Actions Workflow for a detailed explanation of converting the python code to a apk using github actions.
Following Videos discuss the conversion of python code to a mobile app using buildozer and python for android
Videos on converting python code to mobile apps
- How to Convert Python Code into an Android .apk file | Kivymd, Buildozer Tutorial – Published on 06 Jan 2022
- How I Made A Mobile App With Python – Published on 24 August 2023
- Build android app using python | Fork of instagram clone – Published on 12 April 2024
- Python Projects ~ Build Mobile App With Python ~ App Development From Scratch – Published on 01 Feb 2022
- Kivy/KivyMD to Android APK using GitHub Action | Colab Buildozer
- How to Convert Python Code into an Android .apk (that doesn’t crash!) | Kivymd, Buildozer Tutorial
- How To Convert Kivy And KivyMD File To APK Using Google Colab | Buildozer
- Convert Python to Android with WINDOWS & LINUX + Fix Common Bugs
Second approach is to use the plugin called Chaquopy. This is a plugin for the Android Studio. This will let you to integrate the python code with Java or Kotlin code. You can call the modules in the python code from the Java or Kotlin code. Advantage of this method is that you have access to all the features which the Android programmers get and the power of python can also be combined with the power of Java or Kotlin. Further, since the app is developed in Android studio, you can build the gui using native tools. First approach also solves this problem of accessing the Java classes from Python by using the library called Pyjnius or by using a python API such as Plyer to access the hardware features of mobile devices.
You also have another option to convert the python code to a standalone app by using the framework called Beeware . This will let you convert your python code to a variety of platforms such as iOS, Android , Windows, MacOS, Linux, Web, and tvOS. This also has a package builder called brief case and a gui toolgit called Toga. Beeware also has Rubicon Objective-C to access native libraries from python. Beeware will also make sure that the app will have pre-compiled builds of Python since Python installers do not exist in mobile platforms. Refer the Beeware tutorial for the conversion of python code to a mobile app.
We have tried the first approach and we were able to convert the python code to a mobile app successfully. Please refer this page for the full tutorial on converting the pure python code to a mobile app.
Other options to convert python code to a mobile app
appsgeyser.com says you can convert the python code to a mobile app online. We have not tried this as it is a paid option. Further, you may not want your code to live in a third party repository. Further, the free option in their website will display their ads over your app. You have to pay to get an ad free version. One should keep this in mind while using this option.
How to resolve the app crashes during opening
This is a famous scenario where you will get frustrated. When you open the app, it will crash immediately leaving you wondering what just happened. Every developer goes through this. The best way to resolve this in Android is to use the ADB logcat which will provide you display of the errors generated during the opening of the app. Posts on How to Install and Collect ADB logs on android device and Filtering Android adb logcat efficiently in bash command line will give detailed info on this.
Follow these simple steps to get the error or reason why the app crashes
- Install ADB
- Open a terminal
- Enable developer mode and use adb
- use the command adb logcat -s “python” . This will filter the output of error resulting from python.
Now you will know why the app crashes.
Example mobile apps that have been converted from python to mobile Apps
Following are the mobile apps which have been converted from python code
- rockpaperscissor-gui
- Electron Cash wallet for BCH
- Chaquopy: Python for Android
- Chaquopy Matplotlib
- Tic Tac Toeย developed using Beeware framework
- Drawing Appย developed using Beeware framework
- FlaskWithAndroidย developed using python flask framework
- Android ToDo Appย where python is used as backend
- Instagram Clone
- Math Games : Brain Sharpener
- 2048 Py