Search icon

You can build a mobile app in Python, but packaging it risks a storm of bugs

Python is a very popular programming language due to certain desirable features it has such as readability, ease of learning, flexibility, etc.

Therefore, it has been widely adopted in various fields or aspects of computer programming such as scripting, web development, cyber security, and machine learning.

Python has many libraries and frameworks that make it suitable for use in these different fields, for example, Django and Flask are Python frameworks used in web development; Pandas, Numpy, Scikit-learn, PyTorch, etc., are used in machine learning, while Python libraries like Scapy, Beautiful Soup, and Cryptography are used in cybersecurity.

One field of programming where Python is not widely used is mobile development.

This can be attributed to the fact that Python is relatively slow to execute (hence Python apps may struggle on low-spec mobile devices) and also to the fact that Python, being a high-level interpreted language, is far from and may not play nicely with the native language of mobile operating systems (i.e. Java for Android and Objective-c/Swift for iOS), which are typically mid-level compiled languages.

However, this shortcoming of Python in mobile development is a problem for many programmers as Python is basically the most popular first language for beginners in programming, and they might want to delve into mobile development which has become very profitable due to the billions of mobile devices being used all over the world.

Additionally, Python professionals who are already using the language for web development, artificial intelligence, or cybersecurity may also want to convert their software into mobile apps for one reason or another.

This was certainly a problem I faced after learning Python and deciding to create mobile apps to get some of the scripts I had written to users who might find them useful.

However, while it is not popular to use Python for mobile development, it is not impossible. In fact, there are a couple of frameworks and libraries for mobile development with Python, and some have quite a large community.

Some of them are Python for Android, Pymob, PyQT (pyqtdeploy), Kivy, RenPi and Beeware.

Some of these frameworks also inherit Python’s user-friendly syntax for UI development (e.g., the Kivy design language). This makes Python a very useful mobile app development language when used for small apps or rapid prototyping.

However, there are many issues that arise when trying to compile or package a finished Python application for mobile devices. At least, this was my experience with Kivy, and my research showed that it was a common problem.

One reason for this problem is likely the fact that lightweight Python mobile libraries often fail to take advantage of Android Studio’s sophisticated debugging facilities to the extent that popular frameworks like Flutter and React Native do.

This article further explores mobile development with Python as it relates to this packaging problem and best practices to overcome it.

Some popular mobile development frameworks in Python

  1. Kivy – This is actually a cross-platform Python framework that allows users to build applications for Windows, Mac, Linux, Android, and iOS. Kivy uses a clean design language to develop a similar user interface across all platforms. It uses pure Python for application programming and packaging (e.g. buildozer is used to deploy code to specific platforms).

  2. Pyqtdeploy – Provides a set of tools for packaging PyQt CLI and GUI applications or libraries on mobile and desktop devices. Projects packaged with this tool can be commercialized thanks to its free license.

  3. Beeware – This is a set of tools for building Python applications, including Toga, a cross-platform UI toolkit, Batavia, a Javascript driver, and Briefcase for packaging mobile and desktop applications. Beeware applications have a platform-specific look and feel.

The packaging problem

While the development process is usually smooth and straightforward, packaging a Python application for any platform is often problematic. Apart from mobile, I also experienced this problem when trying to package a Python exe file using Pyinstaller.

The problem is further exacerbated when third-party Python libraries are used in the application.

In my experience, this storm of application packaging errors boils down to two main reasons:

  1. Python mobile development libraries have little to no support for developing real-time applications using Android emulators or Android devices.

    Mobile developers using Flutter or React Native will appreciate how helpful it is to use a virtual or physical device while building their app. This helps to discover all kinds of syntax, runtime, and compatibility errors early on so they can be fixed quickly.

    However, with Python libraries, developers usually have to build the app on their desktop device before packaging the APK version for the mobile device.

    This can lead to a lot of compatibility issues when Android or iOS tools start interacting with your app code.

    So the problem is that an application that was built for a desktop environment is packaged for mobile devices.

    This may seem pretty obvious, but developers should keep this in mind when packaging a Python mobile app because the terminal likely won’t prompt you for it (as was the case for me).

    This can waste valuable development time and might even frustrate the developer to the point of giving up.

  2. Using third-party libraries.

    I feel it necessary to point this out because of my experience; when trying to package a Python program, I created it as a Windows executable.

    To my surprise, although the program worked fine in the Python shell, I received complaints about incompatibility of third-party libraries when packaging the application.

    This can also happen when packaging a Python program as a mobile app.

Best practices to overcome this bug storm in Packaging Python APK

  1. Please expect the terminal not to flag all present errors.

  2. Investigate the compatibility of the versions of the libraries used.

  3. Create a dynamic user interface that can adapt to mobile devices without causing clutter.

  4. Use any additional debugging commands available to get a detailed error report.

  5. Use debugging applications such as logcat reader to get additional terminal reports when your application is running on your mobile device.

Leave a Comment

Your email address will not be published. Required fields are marked *