Using python to develop app combat

You say, our future
Being loaded into a coffin, it can’t be dyed with dust

I have long wanted to develop an app to play with, but I am not familiar with java. I have not developed the experience of the app before, so I have been delayed. I recently thought of trying to develop an app in python. After searching for it, I found that there is a way to find it. Now I have some relatively mature modules, so I started to get started. I found that there are many pits in the process. In the end, I relied on Google to solve it, so I remembered it a little.

Say the words in front

Although the python language is very versatile, it is a bit wrong to use it to develop the app. Therefore, the app developed with python should be used as a coding exercise or self-entertainment. In addition, the current module is not very mature. More, in short, advise the gentleman to sneak into.

Ready to work

Using python to develop an app requires a module of python –kivy, an open source, cross-platform Python development framework for developing and using innovative applications. program. In short, this is a python desktop development framework (similar to wxpython and other modules), the powerful is kivy support linux, mac, windows, android, ios platform, which is why the development of the app needs to use this module.
Although kivy is cross-platform, but you want to use python code on different platforms, you also need to package the python code into the executable program of the corresponding platform. Fortunately, there is a package tool project under the kivy project–buildozer, this is the official recommended packaging tool, because it is relatively simple, highly automated, other projects such as: [python-for-android] (https://github.com/kivy/ Python-for-android can also play a similar role, not introduced here.

Building a kivy development environment

Need to install the kivy development environment on the pc, here demonstrates the installation process under mac and linux.

install kivy for mac

Install some dependencies:

1
brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer

Install cython and kivy:

1
2
pip install cython==0.25
pip install kivy

If you install kivy error, install kivy in the following way:

1
2
git clone https://github.com/kivy/kivy
python setup.py install

Post-installation test:

1
2
3
4
5
6
7
8
9
10
$python
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import despair
[INFO ] [Logger ] Record log in /Users/didi/.kivy/logs/kivy_18-05-08_4.txt
[INFO] [Depressed] v1.10.1.dev0, git-5f6c66e, 20180507
[INFO ] [Python ] v2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]

Note: If the kivy module is imported without error, the installation is successful.

install kivy for centos7

Install dependencies first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum install \
make \
mercurial \
automake \
gcc \
gcc-c++ \
SDL_ttf-devel \
SDL_mixer-devel \
khrplatform-devel \
mesa-libGLES \
mesa-libGLES-devel \
gstreamer-plugins-good \
gstreamer \
gstreamer-python \
mtdev-devel \
python-devel \
python-pip \
java-devel

Install cython and kivy:

1
2
pip install Cython==0.20
pip install kivy

Centos install kivy reference: https://kivy.org/docs/installation/installation-linux.html#using-software-packages

Description: Other installation kivy mode can be moved: https://kivy.org/#download (requires over the wall)

Using kivy to develop the first python app

After installing kivy, you can develop the app. Here, the hello-world program is demonstrated. The more complicated usage of kivy is not the focus of this article.
1) Create a main.py file and write:

1
2
3
4
5
6
7
8
9
#! -*- coding:utf-8 -*-
from kivy.app import App
class HelloApp(App):
pass
if __name__ == '__main__':
HelloApp().run()

2) Create a hello.kv file and write:

1
2
Label:
text: 'Hello, World! I am nMask'

Brief description: main.py is the entry function, defines a HelloApp class, the class inherits kivy.app; the hello.kv file is a kivy program, which is equivalent to defining the interface style, etc. The file naming convention is lowercase the class name and removes the app.

Running the first python app

1
python main.py

operation result:

Installing the buildozer tool

Through the above coding, I created my first python app, which can run directly on mac, linux, windows, so how to make it run on Android or Apple? We know that running on Android, you need to package it into apk installer, so you need to use the buildozer tool mentioned above, (buildozer tool can package kivy program, support android, ios, etc.), buildozer installation process is relatively simple :

1
pip install buildozer

Using the buildozer tool to package the kivy program into apk

Run in the python project directory:

1
buildozer init

Successful operation will create a configuration file buildozer.spec, you can change the name of the app by modifying the configuration file, etc., then run:

1
buildozer android debug deploy run

Running the above command will generate a cross-platform installation package for Android, iOS, etc. If used for Android, use [python-for-android] (https://github.com/kivy/python-for-android )project.

When you run the above command for the first time, you will automatically download the necessary files such as Android sdk in the system, as shown below. (The process needs to be over the wall, and there are a lot of dependencies to download)

Note: This is only a demonstration of packaging into apk files, the iso platform can be studied by itself, reference documentation: https://github.com/kivy/buildozer.

python apk program test

If the above steps are all successful, an apk file should be generated in the bin directory under the project directory, similar to the following:

Then download the apk to the Android phone and install it. The test results are as follows:

Open the app:

buildozer instructions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Usage:
buildozer [--profile <name>] [--verbose] [target] <command>...
buildozer --version
Available targets:
android Android target, based on python-for-android project
ios iOS target, based on kivy-ios project
android_old Android target, based on python-for-android project (old toolchain)
Global commands (without target):
distclean Clean the whole Buildozer environment.
help Show the Buildozer help.
init Create a initial buildozer.spec in the current directory
serve Serve the bin directory via SimpleHTTPServer
setdefault Set the default command to run when no arguments are given
version Show the Buildozer version
Target commands:
clean Clean the target environment
update Update the target dependencies
debug Build the application in debug mode
release Build the application in release mode
deploy Deploy the application on the device
run Run the application on the device
serve Serve the bin directory via SimpleHTTPServer
Target "android_old" commands:
adb Run adb from the Android SDK. Args must come after --, or
use --alias to make an alias
logcat Show the log from the device
Target "ios" commands:
list identities List the available identities to use for signing.
xcode Open the xcode project.
Target "android" commands:
adb Run adb from the Android SDK. Args must come after --, or
use --alias to make an alias
logcat Show the log from the device
p4a Run p4a commands. Args must come after --, or use --alias
to make an alias

###Buildozer pits in the packaging process

If you encounter an error during the packaging process, you can modify the log_level in the buildozer.spec configuration file to 2, and then re-run, you can see the specific error message.

This mistake is the fault I reported when running on centos7. The effect is that the system lacks some 32-bit dependent files.
solution:

1
yum -y install --skip-broken glibc.i686 arts.i686 audiofile.i686 bzip2-libs.i686 cairo.i686 cyrus-sasl-lib.i686 dbus-libs.i686 directfb.i686 esound-libs.i686 fltk.i686 freeglut.i686 gtk2.i686 hal-libs.i686 imlib.i686 lcms-libs.i686 lesstif.i686 libacl.i686 libao.i686 libattr.i686 libcap.i686 libdrm.i686 libexif.i686 libgnomecanvas.i686 libICE.i686 libieee1284.i686 libsigc++20.i686 libSM.i686 libtool-ltdl.i686 libusb.i686 libwmf.i686 libwmf-lite.i686 libX11.i686 libXau.i686 libXaw.i686 libXcomposite.i686 libXdamage.i686 libXdmcp.i686 libXext.i686 libXfixes.i686 libxkbfile.i686 libxml2.i686 libXmu.i686 libXp.i686 libXpm.i686 libXScrnSaver.i686 libxslt.i686 libXt.i686 libXtst.i686 libXv.i686 libXxf86vm.i686 lzo.i686 mesa-libGL.i686 mesa-libGLU.i686 nas-libs.i686 nss_ldap.i686 cdk.i686 openldap.i686 pam.i686 popt.i686 pulseaudio-libs.i686 sane-backends-libs-gphoto2.i686 sane-backends-libs.i686 SDL.i686 svgalib.i686 unixODBC.i686 zlib.i686 compat-expat1.i686 compat-libstdc++-33.i686 openal-soft.i686 alsa-oss-libs.i686 redhat-lsb.i686 alsa-plugins-pulseaudio.i686 alsa-plugins-oss.i686 alsa-lib.i686 nspluginwrapper.i686 libXv.i686 libXScrnSaver.i686 qt.i686 qt-x11.i686 pulseaudio-libs.i686 pulseaudio-libs-glib2.i686 alsa-plugins-pulseaudio.i686 python-matplotli

Reference: https://ask.fedoraproject.org/en/question/9556/how-do-i-install-32bit-libraries-on-a-64-bit-fedora/

Error: Error compiling Cython file

The error is that the cython file is faulty. It may be that the cython module is not installed, or the version is faulty.
solution:

1
pip install cython==0.25

This is the last step in the packaging, copy the apk file to the project bin directory time error, is a bug in buildozer.
solution:
Modify the /usr/local/lib/python2.7/dist-packages/buildozer/tagets/android.py file:
(1) Import at the beginning of the file:

1
from distutils.version import LooseVersion

(2) Replace the code below line 786: XXX found how the apk name is really built from the title:

1
2
3
4
5
__sdk_dir = self.android_sdk_dir
build_tools_versions = os.listdir(join(__sdk_dir, 'build-tools'))
build_tools_versions = sorted(build_tools_versions, key=LooseVersion)
build_tools_version = build_tools_versions[-1]
gradle_files = ["build.gradle", "gradle", "gradlew"]

buildozer virtual machine

Kivy officially launched a buildozer virtual machine image, installed buildozer and some dependent files to provide a platform for buildozer package testing. Since I used the buildozer package on the mac to report an error, I changed it to centos and it still didn’t succeed. So I downloaded this virtual machine and the test results are as follows:

Virtual machine download address: http://txzone.net/files/torrents/kivy-buildozer-vm-2.0.zip

Note: For those who can’t solve the dependency problem, you can use this virtual machine to package the program. The development environment still recommends your own machine.

kivy development example

Because the focus of this article is on how to develop a python app using kivy+buildozer, the kivy development process and app functionality are simplified. To learn how to develop more complex apps, please refer to: https://muxuezi.github.io/posts/kivy-perface.html#

~! ~ Toss python makes me happy, …, think about it or go back and learn java~! ~

本文标题:Using python to develop app combat

文章作者:nmask

发布时间:2018年05月08日 - 12:05

最后更新:2019年07月11日 - 18:07

原始链接:https://thief.one/2018/05/08/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: