Buildozer Debian 10 使用记录

Buildozer is a tool for creating application packages easily.

没错,Buildozer 可以很简单地帮你把 Python 程序打包成 Android、iOS 程序。其核心是同项目下的另一个程序,python-for-android。但 Buildozer 可以将各种设置、需要的依赖自动设置完成,等于是一个傻瓜式打包器。

可是呢可是呢,即便如此,在整个打包过程中仍然遇到很多坑,就此记录一下。

系统环境如下:

  • Debian 10 buster(以下内容主要基于此)
  • Python 3.7
  • Buildozer 1.0
  • 非大陆网络(推荐)

相关文档:

安装

先记录下安装过程。

依赖

以下命令用于 python-for-android 必要依赖。

  • 如果显示未找到 openjdk-8-jdk 包,可能是系统版本问题,可以查看 错误-Java 版本
$ sudo dpkg --add-architecture i386
$ sudo apt update
$ sudo apt install -y build-essential ccache git zlib1g-dev python2.7 python2.7-dev libncurses5:i386 libstdc++6:i386 zlib1g:i386 openjdk-8-jdk unzip ant ccache autoconf libtool
Android

若要编译为安卓应用,执行以下命令。

$ sudo apt install -y git zip unzip openjdk-8-jdk python3-pip autoconf libtool pkg-config zlib1g-dev libncurses5-dev libncursesw5-dev libtinfo5 cmake libffi-dev
$ pip3 install --user --upgrade cython virtualenv  # the --user should be removed if you do this in a venv

# add the following line at the end of your ~/.bashrc file
$ export PATH=$PATH:~/.local/bin/
iOS

在安装了 brew 后,执行以下命令。

$ brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer autoconf automake
$ python -m pip install --user --upgrade pip virtualenv kivy-ios

最后,安装 Buildozer。

$ pip3 install --user --upgrade buildozer

经过一些试错,我发现按照以上方法安装依赖比较稳妥,能覆盖得比较全面。

使用

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

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 "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 init
# edit the buildozer.spec, then
$ buildozer android debug

如果没问题的话,就可以直接把 Python 打包成 Android 程序了。需要注意,务必修改以下 buildozer.spec 部分参数。

我是用服务器编译的,所以并没有启用 IDE 调试。为什么不在本地呢?这就迎来了第一个也是最大的坑。

错误

网络问题

为什么一开始推荐使用非大陆网络呢?因为国内的网络出于各种原因,要么一些服务连接不上,要么就非常非常慢、不稳定。

导致的结果就是,Builozer 在编译期间会自动下载各种额外依赖,比如 Android SDK 等,国内网络根本下载不了... 那使用终端代理如何呢?尝试后发现直接 export 部分可行,但部分行不通(如果可以请告知!),那只能一个个手动设置要走的代理。比较麻烦,后续会有什么问题我也没有尝试...

总之,推荐在网络通畅的主机上编译。会少很多麻烦。

网络相关的错误可以查看这篇博文。

Java 版本

一开始安装了 OpenJDK 11,后续发现编译时并不支持。

如果你出现类似下述报错,那可能是 Java 版本问题。

Command failed: /root/.buildozer/android/platform/android-sdk/tools/bin/sdkmanager tools platform-tools
解决方法

这就很简单了,只要安装 OpenJDK 8 即可。如果已安装,请切换版本。

Step 1: 安装 OpenJDK 8

下面是 Debian 10 的安装方法,如果是其他版本系统请自行搜索。

# 安装必要依赖
$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates wget dirmngr gnupg software-properties-common

# 导入存储库密钥并添加
$ wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
$ sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/

# 更新 apt 并安装 openjdk 8
$ sudo apt update
$ sudo apt install adoptopenjdk-8-hotspot

# 测试一下
$ java -version
Step 2: 设置默认版本(如果有其他版本)
# 设置默认 java 版本
$ sudo update-alternatives --config java
# 设置默认 javac 版本
$ sudo update-alternatives --config javac

Virtualenv 相关

如果有类似以下报错,那可能是这个问题。

'env': env must be a dict. Got environ...
解决方法

buildozer.spec 中的相应设置改为 p4a.branch = develop 即可。记得将注释删去。

master 线改为 develop 就行,所以我猜测是目前版本程序 BUG 问题?

最后

目前为止暂时遇到这些问题,如果后续继续使用的话可能会跟进。

其实一开始我是在本地编译的,结果问题一个接着一个,最主要还是网络导致的。真的心累... 所以就用国外主机了,整个过程比较顺利。

tag(s): 笔记
show comments · back · home
Edit with Markdown