Kivy – Packaging


Kivy – Packaging



”;


The term “packaging” refers to creating a single package of the source code of the application along with all the dependencies which includes the libraries, data files configuration files, etc.

When you develop a Kivy app, it needs various resources. For example, the common requirement is sdl2 package, or glew package. When you install Kivy, these dependencies are also installed.


kivy-deps.glew
kivy-deps.gstreamer
kivy-deps.sdl2

So far, you have been running the Kivy app from your machine which has Python runtime already installed. Hoewever, when it comes to porting this application to another machine that doesn”t have Python installed, you need to build a package which consists of the program along with the Python runtime as well as the dependencies.

PyInstaller package helps you to build a redistributable package of your app. The user need not install Python, Kivy or any other library to run the app.

To build such a distributable package, you should first install PyInstaller in your current Kivy environment with the PIP command.


pip3 install -U pyinstaller

The next step is to collect one or more Python source files (with .py extension), along with other resources such as image files etc. in a separate folder.

For this exercise, we shall be building a package for ImageButton app. The files for this app are stored in imgbtn folder.


Directory of C:kivyenvimgbtn
forward.png main.py pause.png play.png
previous.png

Create another folder ImangBtnApp that will eventually store the distributable package. From within the folder, execute the following command −


(kivyenv) C:kivyenvImageBtnApp>pyinstaller -n ImageBtnApp
c:kivyenvimgbtnmain.py

A Kivy app has a lot of dependencies. Hence, it might take a while to collect all of them. Eventually, the ImageButtonApp folder will populate with −


Directory of C:kivyenvImageBtnApp

27-07-2023 21:25    <DIR>           .
27-07-2023 21:07    <DIR>           ..
27-07-2023 21:25    <DIR>           build
27-07-2023 21:28    <DIR>           dist
27-07-2023 21:25                    970 ImageBtnApp.spec

The dist folder is the distributable folder in which you will find the EXE file “ImageBtnApp.exe”, along with the DLL libraries required such as sdl2 etc.

There is a spec file of the name of the app. we need to edit the spec file to add the dependencies hooks to correctly build the exe.

Open the spec file with your favorite editor and add these lines at the beginning of the spec −


from kivy_deps import sdl2, glew

Scroll down the spec file to locate COLLECT section and add a Tree object for every path of the dependencies. E.g. *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)].


coll = COLLECT(
   exe, Tree(''c:\kivyenv\imgbtn\''),
   a.binaries,
   a.zipfiles,
   a.datas,
   *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
   strip=False,
   upx=True,
   upx_exclude=[],
   name=''ImageBtnApp'',
)

Now we build the spec file in ImageBtnApp with −


python -m PyInstaller ImageBtnApp.spec

The compiled package will be in the ImageBtnAppdistImageBtnApp directory.

You can also put the runtime, application code and the dependencies in a single file (instead of the distributable package) with -onetime switch of the Pyinstaller commandline syntax −


pyinstaller --onefile -n ImageBtnApp

To build a distributable package targeted for Android, you need to use Buildozer tool. It downloads and sets up all the prerequisites for pythonfor-android, including the android SDK and NDK, then builds an apk that can be automatically pushed to the device.

Navigate to your project directory and run −


buildozer init

This creates a buildozer.spec file controlling your build configuration. Edit the file with your app name etc. You can set variables to control most or all of the parameters passed to python-for-android. Finally, plug in your android device and run −


buildozer android debug deploy run

to build, push and automatically run the APK on your device.

Advertisements

”;

Leave a Reply

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