📜 ⬆️ ⬇️

Caution - Bulldozer (build apk packages in Kivy)


Whew! Just got out from under the tracks of this monster. And some people are less fortunate. As promised in the previous article , today we will explain how apk packages for programs written using the Kivy framework using the Buildozer utility are built.


What is needed for this? In addition to the already well-known tools - coffee-cigarettes, half a kilo of nerves - we will need the keys to the brand new Bulldozer, which stands in the hangar on the github and related dependencies, without which, unfortunately, it will not start, and if it does, it will not be possible to crush anyone and apk Android package does not build.


By the way, Microsoft users don’t go with us on their Windows machines for the Bulldozer, because to build the apk package you need a Linux system, or its image on the virtual machine. Well. And we go to github, drag the bulldozer and unpack the archive in some directory. Pancake. Yes, it's just a monster!


Calmly Do not start. For levers do not grab. First, we need to download the pip package — a tool for installing and managing Python packages.


sudo apt-get install python-pip 

We go in the hangar, in the sense of a folder with a bulldozer ...



... and execute the command -


 sudo python setup.py install 

We will be notified about the successful installation by a message Successfully installed buildozer. The bulldozer works on both Python 2.7 and Python> = 3.3. I am using Python 2.7. Just because I have not tried to build apk packages for programs written using Python 3, although according to Kivy developers, the third Python is fully supported. Well, now, actually, let's download python-for-android . This branch works for me, therefore I do not offer others. Unpack python-for-android.zip and forget about it for now.


Dependencies


There are quite a few of them and it will depend on their proper installation whether you can put together a cherished apk-neck or have to drink validol and start all over again. It is believed that all of the above packages are missing from you and it is assumed that the Kivy framework is installed and successfully used by you.


 sudo pip install --upgrade cython sudo pip install virtualenv sudo pip install Jinja2 

To build a package for Android, we need the Android SDK. Some of his binaries are still in 32 bit libraries, so you need to make them available:


 dpkg --add-architecture i386 

After that we can install the following packages:


 sudo apt-get update sudo apt-get install -y build-essential ccache git zlib1g-dev python2.7 python2.7-dev libncurses5:i386 libstdc++6:i386 zlib1g:i386 openjdk-7-jdk unzip 

Well, now, finally, we are ready to sit in a comfortable chair of the Bulldozer and grab hold of the controls. Go to the project folder and execute the command in the terminal -


 buildozer init 

... which will create the buildozer.spec specification file in the project, where we will specify the Bulldozer with what parameters to build our application.



Open this file and see its contents:


buildozer.spec
 [app] # (str) Title of your application title = DemoCleanMaster # (str) Package name package.name = democleanmaster # (str) Package domain (needed for android/ios packaging) package.domain = org.heattheatr # (str) Source code where the main.py live source.dir = . # (list) Source files to include (let empty to include all the files) source.include_exts = py,png,kv,jpg # (list) Source files to exclude (let empty to not exclude anything) #source.exclude_exts = [] # (list) List of directory to exclude (let empty to not exclude anything) #source.exclude_dirs = [] # (list) List of exclusions using pattern matching #source.exclude_patterns = license,images/*/*.jpg # (str) Application versioning (method 1) version.regex = __version__ = ['"](.*)['"] version.filename = %(source.dir)s/main.py # (str) Application versioning (method 2) # version = 1.2.0 # (list) Application requirements # comma seperated eg requirements = sqlite3,kivy requirements = kivy # (str) Custom source folders for requirements # Sets custom source for any requirements with recipes # requirements.source.kivy = ../../kivy # (list) Garden requirements #garden_requirements = # (str) Presplash of the application presplash.filename = %(source.dir)s/Data/Images/presplash.jpg # (str) Icon of the application icon.filename = %(source.dir)s/Data/Images/logo.png # (str) Supported orientation (one of landscape, portrait or all) orientation = portrait # (bool) Indicate if the application should be fullscreen or not fullscreen = 1 # # Android specific # # (list) Permissions android.permissions = INTERNET # (int) Android API to use android.api = 18 # (int) Minimum API required (8 = Android 2.2 devices) android.minapi = 8 # (int) Android SDK version to use android.sdk = 21 # (str) Android NDK version to use android.ndk = 9 # (bool) Use --private data storage (True) or --dir public storage (False) android.private_storage = False # (str) Android NDK directory (if empty, it will be automatically downloaded.) android.ndk_path = /home/zavulon/Opt/android-ndk-r9 # (str) Android SDK directory (if empty, it will be automatically downloaded.) android.sdk_path = /home/zavulon/Opt/android-sdk # (str) python-for-android git clone directory (if empty, it will be automatically cloned from github) android.p4a_dir = /home/zavulon/Opt/Python/python-for-android # (list) python-for-android whitelist #android.p4a_whitelist = # (str) Android entry point, default is ok for Kivy-based app #android.entrypoint = org.renpy.android.PythonActivity # (list) List of Java .jar files to add to the libs so that pyjnius can access # their classes. Don't add jars that you do not need, since extra jars can slow # down the build process. Allows wildcards matching, for example: # OUYA-ODK/libs/*.jar #android.add_jars = foo.jar,bar.jar,path/to/more/*.jar # (list) List of Java files to add to the android project (can be java or a # directory containing the files) #android.add_src = # (str) python-for-android branch to use, if not master, useful to try # not yet merged features. #android.branch = master # (str) OUYA Console category. Should be one of GAME or APP # If you leave this blank, OUYA support will not be enabled #android.ouya.category = GAME # (str) Filename of OUYA Console icon. It must be a 732x412 png image. #android.ouya.icon.filename = %(source.dir)s/data/ouya_icon.png # (str) XML file to include as an intent filters in <activity> tag #android.manifest.intent_filters = # (list) Android additionnal libraries to copy into libs/armeabi #android.add_libs_armeabi = libs/android/*.so #android.add_libs_armeabi_v7a = libs/android-v7/*.so #android.add_libs_x86 = libs/android-x86/*.so #android.add_libs_mips = libs/android-mips/*.so # (bool) Indicate whether the screen should stay on # Don't forget to add the WAKE_LOCK permission if you set this to True #android.wakelock = False # (list) Android application meta-data to set (key=value format) #android.meta_data = # (list) Android library project to add (will be added in the # project.properties automatically.) #android.library_references = # # iOS specific # # (str) Name of the certificate to use for signing the debug version # Get a list of available identities: buildozer ios list_identities #ios.codesign.debug = "iPhone Developer: <lastname> <firstname> (<hexstring>)" # (str) Name of the certificate to use for signing the release version #ios.codesign.release = %(ios.codesign.debug)s [buildozer] # (int) Log level (0 = error only, 1 = info, 2 = debug (with command output)) log_level = 2 # (int) Display warning if buildozer is run as root (0 = False, 1 = True) warn_on_root = 0 # ----------------------------------------------------------------------------- # List as sections # # You can define all the "list" as [section:key]. # Each line will be considered as a option to the list. # Let's take [app] / source.exclude_patterns. # Instead of doing: # #[app] #source.exclude_patterns = license,data/audio/*.wav,data/images/original/* # # This can be translated into: # #[app:source.exclude_patterns] #license #data/audio/*.wav #data/images/original/* # # ----------------------------------------------------------------------------- # Profiles # # You can extend section / key with a profile # For example, you want to deploy a demo version of your application without # HD content. You could first change the title to add "(demo)" in the name # and extend the excluded directories to remove the HD content. # #[app@demo] #title = My Application (demo) # #[app:source.exclude_patterns@demo] #images/hd/* # # Then, invoke the command line with the "demo" profile: # #buildozer --profile demo android debug 

Now let's take a closer look ...


 title = DemoCleanMaster 

This is the name of the application that will be displayed on the Android device and which will carry the installation apk package.


 package.name = democleanmaster package.domain = org.heattheatr 

Unique application domain.



 source.dir = . 

The directory where the application entry point is located is the main.py file. By default, this is the root directory of the project.



 source.include_exts = py,kv,jpg,png 

The extension of the files that will be included in the build apk package from your project. The directory tree structure from which the specified files will be taken is preserved.


 version.regex = __version__ = ['"](.*)['"] version.filename = %(source.dir)s/main.py 

Version of the program and in which file its (version) to search. This method says that in the main.py file of the project root directory there should be a variable with the value of the version of your application.


 # main.py __version__ = '0.0.1' # ...  

 requirements = kivy 

Third-party libraries to be included in the build. Comma-separated, you can specify the library packages your application works with, for example: kivy, opencv, pil, sqlite3.


You can see the full list of available libraries in the specification editor in the tool for building UI - Kivy Designer . Highly recommend!



 presplash.filename = %(source.dir)s/Data/Images/presplash.jpg 

The image that will be shown on the device screen while your application is running, where% (source.dir) s is the path to the root directory of the project.



 icon.filename = %(source.dir)s/Data/Images/logo.png 

The icon of the application that will be displayed on the device.


 orientation = portrait fullscreen = 1 

Orientation of your application on the device. In addition to 'portrait', it can be set to 'landscape'. 'fullscreen' tells you to unwrap the activity in full screen. Also takes the value '0'.


 android.permissions = INTERNET 

Application rights.


 android.api = 18 

The version of the Android API used by the application (must be in the Android SDK). There may be problems if, for example, you specify the version of Android API 10, which is not in the Android NDK.



 android.minapi = 8 

The minimum version of the Android API supported by the application.


 android.sdk = 21 

Android SDK version.


 android.ndk = 9 

Android NDK version.


 android.private_storage = False 

Specifies to create a folder with project files on the device on the SD card. Recommend. Because in this case you can replace the bytecompiled representation of the * .pyo files of the project with the source texts, and if there is a need to make a small change to the project, you can do it right on the device by editing the sources or reloading them from the computer.


 android.ndk_path = /home/zavulon/Opt/android-ndk-r9 android.sdk_path = /home/zavulon/Opt/android-sdk android.p4a_dir = /home/zavulon/Opt/Python/python-for-android 

Everything is clear here. You can do as follows:


 android.ndk_path = android.sdk_path = android.p4a_dir = 

In this case, the Bulldozer will download both the Android SDK with the ANDROID API versions specified in the specification, and the Android NDK, and python-for-android, Apache-ant will be downloaded if the Bulldozer does not find it. The following libraries and sources of Python itself and modules will also be downloaded:



Yes. All this is somewhat troublesome, but it is done once. Then, to build a project, simply edit the specification file buildozer.spec and throw it into the project that we want to build. I do not consider the rest of the specification parameters, since I have not yet used them myself. And, yes, the parameters for assembly under iOS also remain behind the tracks, because I do not have a device with this platform.


Actually, hey, you are there - close the doors of the Bulldozer from the other side and away from the runway - we go to the project folder and execute the command to start the compilation in the terminal:


 buildozer android debug 

It will take oh-oh-a lot of time (useful cigarettes and coffee), before you see the cherished message:



Please note that apk-shechka is obtained debazhny. On Habré there is an article that describes the process of creating a signed package. In addition, the build size is quite impressive - 7.6 MB. This is because the Bulldozer builds the entire Python with all the tests and unnecessary libraries. Also, Kivy itself is included with all APIs even if your application does not use them.


After installing the assembled apk on the device and starting it, the application will run for quite a while. Kivy will take time to extract and deploy all the libraries. Fortunately, all subsequent program downloads last no more than five seconds.


In the next article I will tell you what levers to pull the Bulldozer to see the assembled project, but already much smaller ...



Successful builds! Look under the tracks!


')

Source: https://habr.com/ru/post/301776/


All Articles