May 30, 2021 Article blog
Author: nmask
Source: WinTogether Security Team
I wanted to develop an app to play with a long time ago, I wasn't familiar with java, and I didn't have any experience developing apps before, so I've been delayed. Recently thought of trying to develop an app with python, google search, found that there is indeed a way to find, there are now some relatively mature modules, so began to do hands-on, the process found that there are many pits, good in the end rely on google to solve, so small note.
Although the python language is omnidiractical, but it is still a bit wrong to use it to develop apps, so apps developed with python should be used as coding exercises, or for self-entertainment, plus the current module in this area is not particularly mature, more bugs, in short, to persuade Junmo to enter.
Using
python
to develop an app requires one of python's
kivy
an open source, cross-platform Python development framework for developing applications that use innovation.
kivy
I
n short, this is a python desktop program development framework (like modules like
wxpython
that is powerful for
kivy
support linux, mac, windows, android, ios platform, which is why it is necessary to develop apps.
Although
kivy
is cross-platform, to use python code on different platforms, you also need to package python code into executables for the corresponding platform, so that there is a packaging tool project under the kivy project -
buildozer
which is the official recommended packaging tool, because it is relatively simple, high degree of automation, other projects such as:
python-for-android
can also play a similar role, not expanded here.
The
kivy
development environment needs to be installed on the pc, and the installation process under mac and
linux
is demonstrated here.
Install some dependent 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 the installation
kivy
reports an error, install
kivy
in the following way:
git clone https://github.com/kivy/kivy
python setup.py install
Post-installation testing:
$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)]
Description:
Importing
kivy
module without errors indicates a successful installation.
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:
Install Kiivy pip on Linux
Description:
Other installation
kivy
methods can be moved:
kivy official download
(wall flipping required)
After installing kivy, you can develop the app program, here is the
hello-world
program, about
kivy
more complex usage is not the focus of this article, and then read about it later.
1) Create a
main.py
file that writes:
#! -*- coding:utf-8 -*-
from kivy.app import App
class HelloApp(App):
pass
if __name__ == '__main__':
HelloApp().run()
2) Create a
hello.kv
file that writes:
Label:
text: 'Hello, World! I am nMask'
Simple description:
main.py
is an entry function that defines a
HelloApp
class that inherits
kivy.app
hello.kv
file is a
kivy
program, equivalent to defining interface style, etc., and the file name rule is lowercase and removes the app.
python main.py
Results:
With the coding above, I created my first python app, which runs directly on the mac, linux, and windows platforms, so how do I get it to run on Android or Apple?
We know that running on Android requires packaging it as an apk installer, so we need the buildozer tool mentioned earlier (buildozer tools can package kivy programs that support android, ios,
buildozer
installation process is simpler:
pip install buildozer
Run in the python project directory:
buildozer init
A successful run will create a profile
buildozer.spec
which can change the name of the app by modifying the profile, and so on, and then run:
buildozer android debug deploy run
Running the above commands will generate a cross-platform installation package for Android, ios, and so on, and if used for Android,
python-for-android
projects.
The first time you run the above command, you will automatically download necessary files such as Android sdk from the system, as shown below. ( The process needs to go over the wall, and there's a lot of dependency to download)
Description: Only the demonstration is packaged as an apk file, self-study of the iso platform, Buildozer reference documentation.
If all of the above steps work successfully, an apk file should be generated under the
bin
directory under the project directory, similar to this:
Then download apk to your Android phone and install it, and the test results are as follows: Open the app:
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
If you encounter an error during packaging, you can modify
log_level
in the
buildozer.spec
profile to
2
and then run again to see the specific error message.
The mistake was that I ran the Times on
centos7
to the extent that the system was missing some
32
dependency 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/
The error is intended to be an error in the
cython
file, either the
cython
module is not installed or there is a problem with the version.
solution:
pip install cython==0.25
This is the last step in packaging, and the mistake of copying the apk file to the project bin directory is a bug on buildozer.
Solution:
Modify
/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 line
786
lines:
XXX found how the apk name is really built from the title
__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'
Kivy officially launched a buildozer virtual machine image, which has been installed with buildozer and some dependent files to provide a platform for buildozer packaging testing. Since I had previously reported errors with buildozer packaging on mac, and then switched to centos was still unsuccessful, I downloaded the virtual machine with the following test results:
Virtual machine download address:
http://txzone.net/files/torrents/kivy-buildozer-vm-2.0.zip
Description: For friends who can't resolve dependency issues, you can use this virtual machine for program packaging, or the development environment recommends using your own native.
Because this article focuses on how to develop a python app with
kivy
and
buildozer
the development process for
kivy
as well as app features, is most simplified.
To learn how to develop more complex apps, see:
https://muxuezi.github.io/posts/kivy-perface.html#