How to make (and compile) a mobile application with python

Have you ever thought of making a program for a particular activity you have wanted for a while to be automated? It can be anything at all from wanting to produce tables of student results, modifying a word document the way you want to making a program that enables you get information directly from the web without using a browser and other nice hacks. Well I have and it’s been a real debacle for a while. Well, you might say I am lazy but someone once said that laziness is the key to modernization. If you do feel like that then I advise you to learn Python.
With technological advancement and the current Covid-19 crisis, the need for end-user-oriented apps to automate and streamline activities like voting, e-learning, even editing webpages and many others has boomed. And you can make your own that will suit exactly what you want of it by learning Python.
Python isn’t omnipotent and If you’ve used Python like I have, you may have hit the wall where it’s difficult to actually compile your script into an android installer that any android user can use without the prerequisites you painstakingly went through to make the program. Well in my writeup today I would like to show all my python family members how I made my scripts into .exe, .apk, and .app extension softwares.

Today, I’ll talk of how I to make .exe files (files for windows) and .apk files (files for androids) on a windows OS.

To make .exe and . apk applications.
Well I am going to guide you based on the fact that you are already a python programmer for even if a few days and that you are using pip (pip install package) routine to get external modules into your OS or at least have it installed.
So first there are two approaches I know of to do this. One requires the use of the beeware package and the other involves the use of the pyinstaller package.
Well in my personal opinion beeware is the real deal in that it enables you to make iOS, android, macOS and windows applications without having to use any of these OS to actually compile them into installers. Like for instance while you might need windows to make a .exe executable file, if you have a macOS you can still make a .exe file. Yeah, while still in a macOS environment, real cool right.
On the other hand, pyinstaller still uses the traditional installing process. It compiles the application based on the OS you are using. So, it will make .app for macOS only, .exe for windows only and same can be said of the android environments .apk extension.
So now that I have given my opinions choose whichever package you want and I will start my actual guide.

Step 1. First create a virtual environment.
If you don’t know what this is then I’ll say it’s like a part of your system that while being on your pc maintains minimal interaction with your actual system so you can do stuff that won’t actually affect your real system and it enables one easily distribute your work between other users. Yh like the name it’s there on your pc but only virtually. You can install a new OS in a virtual environment or make programs and web files for other systems in a virtual environment.
To get a virtual environment you need to get virtualenv module. This can be obtained by using the command line command below:
python -m pip install virtualenv

So, now go to any location on your pc where you want to start the project. There create a folder and name it how you like, then open this folder and yh this is where your virtual environment will be.
Now go to your command line and cd (navigate) to the location folder. Know enter the command
python -m venv [name_of_the_environment]

for convention sake add -venv to the names ending. So, if you want the name of the environment folder to had been say tutorial then make it tutorial-venv. Again, this all just convention.
If no error message comes then you’ve succeeded. Otherwise check to see in you have really installed virtualenv using the pip command. If there is an error then check the end of this page.

Step 2. Activate your virtual environment and install beeware unto your new or first virtual environment.
To activate the virtual environment, use the command [name_of_virtual_environment]\Scripts\activate

Now you should see that the next line in your command line begins with your virtual environment name in parenthesis. If not check if your command is the same as the above spelling wise and all.
To close the venv (short for virtual environment) just close the command line or use this command
deactivate
they will save whatever you were doing in both approaches before closing.

Now in the venv (short for virtual environment) get your beeware module by using the command
pip install beeware

as you can see you don’t have to add other flags for user or add python -m to it. Just leave it plain after all you’re in a virtual environment after all.

Step 3. Start your project.
Do this by using this command in command line
briefcase new
this will create your first or a new project
you should now see text messages asking you about your app’s info and such. If you don’t have an answer to any, feel free to use the defaults they give you, they are the ones in the square brackets.
Under the license section, you can choose any of them but usually option 1 is a given and under the GUI you want to use choose the one you. A doc on the toga module is provided below if interested. If not interested in any of the given GUI tools then feel free to choose none and build with a different GUI tool like PyQt5.
When done you will see that the project name you chose is the name of a new folder that is now present in your venv folder. Okay that’s where we actually want to go. So, cd (navigate) to that folder in the command line.
Now that you have made your first template, you need to create the app templates, this will be downloaded and cached for future use.
To do this use the command
briefcase create

Now let’s get started
There are few commands you will need
briefcase dev – this opens your python script in developer’s mode
briefcase build – this builds the app into a package that you can distribute to others
briefcase package – this is more or less a command that turns your app or package into an installer.
briefcase run – this will run the program on pc as if it was in the actual OS environment, if an android app it will load an android simulator.
briefcase update – to make changes to your code
note: these commands are for getting packages in windows or MacOS if android or iOS apps are what you want to build then specify them at the end of command. For instance, the briefcase run will build an app in .exe for window files but if you want to make an android app while still on windows. You just add the android or for iOS add iOS.
Example:
briefcase build android
briefcase build iOS

Note that some of the above commands when used for the first time will download their dependencies from the internet and this may take a lot of time ranging from 10min to an hour. This is usually annoying but the end result is worth it. If a configuration step comes and it seems like it’s taking forever just take heart and endure with patience.

steps in getting your app started:
Use the command briefcase dev.
An empty app will load. So, this is your template and this is what you are going to modify and make your app.

Go to the folder in which you created the venv and move to the folder with the name you chose for your project/ name_of_environment. Then select the src folder and again select the name of your project again. You should see a python file called app.py. Open it with your editor and then modify it as you want.
For documentation on how to use the toga module go to https://wixtoolset.org/releases/

If done modifying as you wish you can see how it turned out with the briefcase dev command again

If satisfied with the outcome use the briefcase build command to make the setup file for .exe file and .apk file for android. These files can be distributed to people on their own.
They will be located in the same location as the src folder as windows and android respectively.
For the windows app open the windows folder and you will easily see the file ending with .exe file by navigating to
windows[name_you_chose]
For the android app open the Android folder and navigate through
android[name_you_chose]\app\build\outputs\apk\debug
to get the .apk installer.

To make an installer for your .exe file for windows use the command briefcase package. This will be found in the windows folder ending with .msi (Microsoft installer).

To test the app, use the briefcase run command to see how it looks like in its supposed OS. For android, briefcase will download an android simulator for you and then you can select the device you want to use.

If you have already built the .exe file or .apk file and still want to make updates to your code, go ahead and do so and once done use the briefcase update command in command line to make an update to your project and then go over step 4 to step 6 again.

For more info go to
https://pybee.org
and you can check out Russell Keith-Magee’s explanatory video at
https://www.youtube.com/watch?v=qaPzlIJ57dk

Note: you can’t make iOS apps on windows you will need MacOS for that, this may be due to the Xcode dependency.

OK, next week, we will learn how to use pyinstaller.
Please feel free to ask any questions you want to ask in the comment section.

~ ThePythonNewbie

Click here to read my previous post: Previous post by Haleem Karmil

9 thoughts on “How to make (and compile) a mobile application with python

Leave a Reply

Your email address will not be published.