虚拟环境
- 系统自带的Python版本号太高,要开
python=3.10虚拟环境才行。
1
2
3
4
5
6
7
8
9
10
|
(base) ubuntu@ubuntu:~$ python
Python 3.13.5 | packaged by Anaconda, Inc. | (main, Jun 12 2025, 16:09:02) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
(base) ubuntu@ubuntu:~$ conda deactivate
ubuntu@ubuntu:~$ python3
Python 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
|
1
2
|
conda create -n kivy python=3.10 -y
conda activate kivy
|
安装依赖
1
2
3
4
|
pip install --upgrade pip # 可跳过
pip install "cython<3.1"
pip install buildozer==1.5.0
pip install kivy
|
删除上次报错
1
|
rm -rf .buildozer # 删除之前报错,若首次,则跳过此步。
|
创键APP
1
2
3
4
5
6
7
8
9
|
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text="Hello from Python to APK!", font_size=48)
if __name__ == "__main__":
MyApp().run()
|
初始化
1
|
buildozer init # 生成buildozer.spec
|
1
2
3
4
5
6
7
8
9
10
11
12
|
title = MyKivyApp # 手机上显示的名字
package.name = mykivyapp # 包名的一部分
package.domain = org.test # 包名的一部分 -> org.test.mykivyapp
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
version = 0.1
requirements = python3,kivy
# 如果你有其它依赖,比如 numpy, requests:
# requirements = python3,kivy,numpy,requests
|
打包
- 第一次打包(会下载各种SDK/NDK,时间比较久)。
1
2
|
# 生成 debug 版 APK
buildozer -v android debug
|
运行buildozer -v android debug,
1
2
3
|
# Check that aidl can be executed
# Search for Aidl
# Aidl not found, please install it.
|
删除Buildozer缓存目录即可,
1
2
|
rm -rf ~/.buildozer
rm -rf ./.buildozer
|
减少打包体积,可以修改buildozer.spec中android.archs = arm64-v8a。
上架版APK必须使用正式签名(Release签名)。
1
|
bin/your_app_name-0.1-debug.apk
|