Preface
The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.
PS: If you need Python learning materials, you can click on the link below to get it yourself
Python free learning materials and group communication answers Click to join
I wanted to develop an app for fun a long time ago, but I was not familiar with Java enough, and I had no experience in app development before, so I was delayed. Recently, I thought of trying to develop an app with python. After searching on Google, I found that there is indeed a way to find. There are some relatively mature modules, so I started hands-on combat, and found that there are many pits in the process. In the end, I relied on Google to solve it, so I have to remember.
Ready to work
The use of python to develop apps requires a module of python-kivy. Kivy is an open source, cross-platform Python development framework for the development and use of innovative applications. In short, this is a python desktop program development framework (similar to modules such as wxpython). The powerful thing is that kivy supports linux, mac, windows, android, ios platforms, which is why this module is needed to develop apps.
Although kivy is cross-platform, if you want to use python code on different platforms, you also need to package the python code into an executable program for the corresponding platform. Fortunately, there is a packaging tool project under the kivy project-buildozer, which is officially recommended The packaging tool is relatively simple and highly automated. Other projects such as python-for-android can also play a similar role. I will not introduce it here.
Set up kivy development environment
The kivy development environment needs to be installed on the pc, here is the demonstration of the installation process under mac and linux.
install kivy for mac
installs some dependency packages:
brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer
Install cython and kivy:
pip install cython==0.25
pip install kivy
If an error is reported when installing kivy, use the following method to install kivy:
git clone https://github.com/kivy/kivy
python setup.py install
Test after installation:
$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 kivy
[INFO ] [Logger ] Record log in /Users/didi/.kivy/logs/kivy_18-05-08_4.txt
[INFO ] [Kivy ] 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 no error is reported when importing the kivy module, the installation is successful.
install kivy for centos7
Install dependencies first:
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:
pip install Cython==0.20
pip install kivy
Centos installation kivy reference: https://kivy.org/docs/installation/installation-linux.html#using-software-packages
Note: Other ways to install kivy can be moved: https://kivy.org/#download (requires over the wall)
Develop the first python app with kivy
After installing kivy, you can develop app programs. Here is a demonstration of the hello-world program. The more complex usage of kivy is not the focus of this article, and I will introduce it later.
- Create a main.py file and write:
#! -*- 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:
Label:
text: 'Hello, World! I am nMask'
Brief description: main.py is the entry function, which defines a HelloApp class, which inherits kivy.app; the hello.kv file is a kivy program, which is equivalent to defining the interface style, etc. The file naming rule is lowercase class name and remove app.
Run the first python app
python main.py
operation result:
Install buildozer tool
Through the above coding, I created my first python app program, which can run directly on mac, linux, windows platform, then how to make it run on Android or Apple mobile phone? We know that to run on Android, it needs to be packaged into an apk installer, so we need to use the buildozer tool mentioned earlier (buildozer tool can package kivy programs, support android, ios, etc.), the installation process of buildozer is relatively simple :
pip install buildozer
Use the buildozer tool to package the kivy program into an apk
Run in the python project directory:
buildozer init
A successful operation will create a configuration file buildozer.spec, you can modify the configuration file to change the name of the app, and then run:
buildozer android debug deploy run
Running the above command will generate a cross-platform installation package, applicable to Android, ios, etc. If it is used for Android, use the python-for-android project.
When running the above command for the first time, necessary files such as Android SDK will be automatically downloaded in the system, as shown in the figure below. (The process needs to go over the wall, and there are many dependencies to download)
Note: This is only the demonstration packaged into an apk file, and you can study on the iso platform by yourself. Refer to the document: https://github.com/kivy/buildozer .
python apk program test
If the above steps are all run successfully, 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
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
Holes in the buildozer 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.
报错:You might have missed to install 32bits libs
This error is reported when I run it on centos7, to the effect that the system lacks some 32-bit dependent files.
solution:
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 to the effect that the cython file is wrong, it may be that the cython module is not installed or the version is wrong.
solution:
pip install cython==0.25
报错:IOError: [Errno 2] No such file or directory…..
This is the last step of packaging. An error was reported when copying the apk file to the bin directory of the project, which was 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:
from distutils.version import LooseVersion
(2) Replace the following code in line 786: XXX found how the apk name is really built from the title with:
__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"]
is_gradle_build = any((exists(join(dist_dir, x)) for x in gradle_files)) and build_tools_version >= ’25.0'
buildozer virtual machine
Kivy officially launched a buildozer virtual machine image. Buildozer and some dependent files have been installed to provide a platform for buildozer packaging and testing. Since I used buildozer to package on the mac, I kept reporting errors, and later I changed to centos and still failed, so I downloaded this virtual machine. The test results are as follows:
Virtual machine download address: http://txzone.net/files/torrents/kivy-buildozer-vm-2.0.zip
Note: For friends who cannot solve the dependency problem, you can use this virtual machine for program packaging. The development environment is still recommended to use your own machine.
kivy development example
Because this article focuses on how to use kivy+buildozer to develop a python app, the kivy development process and app functions are simplified. To learn how to develop more complex apps, please refer to: https://muxuezi.github.io/posts/kivy-perface.html#