You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Buildozer编译Kivy应用启动崩溃,请求排查原因

Kivy应用Buildozer编译后启动崩溃排查方案

问题背景

用Buildozer编译首个Kivy应用,编译完成后启动即崩溃。项目结构包含:1个.py文件、多个.kv文件,以及存放所有.png资源的assets子文件夹。

相关代码与配置

main.py 开头代码

from kivy.uix.screenmanager import ScreenManager
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
import kivy
from kivy.animation import Animation
from firebase import firebase
import json
import os
import random
import smtplib
import pyrebase

buildozer.spec 部分配置

[app]

# (str) Title of your application
title = app
# (str) Package name
package.name = app

# (str) Package domain (needed for android/ios packaging)
package.domain = org.test

# (str) Source code where the main.py live
source.dir = .

# (list) Source files to include (let empty to include all the files)
source.include_exts = py,png,jpg,kv,atlas

# (list) List of inclusions using pattern matching
source.include_patterns = assets/*,images/*.png

# (list) Source files to exclude (let empty to not exclude anything)
#source.exclude_exts = spec

# (list) List of directory to exclude (let empty to not exclude anything)
#source.exclude_dirs = tests, bin, venv

# (list) List of exclusions using pattern matching
# Do not prefix with './'
#source.exclude_patterns = license,images/*/*.jpg

# (str) Application versioning (method 1)
version = 0.1

# (str) Application versioning (method 2)
# version.regex = __version__ = ['"](.*)['"]
# version.filename = %(source.dir)s/main.py

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3,kivy==master,https://github.com/kivymd/KivyMD/archive/master.zip,firebase,certifi,charset-normalizer,Cython,docutils,firebase,gcloud,googleapis-common-protos,httplib2,idna,jws,Kivy-Garden,oauth2client,osxrelocator,Pillow,protobuf,pyasn1,pyasn1-modules,pycryptodome,Pygments,pyparsing,Pyrebase,python-jwt,requests,requests-toolbelt,rsa,six,urllib3,wheel

排查步骤

  1. 查看崩溃日志
    用adb连接设备,执行adb logcat | grep python,可直接获取Python层面的错误信息,比如资源缺失、依赖冲突、代码语法问题等,这是定位崩溃原因的核心方式。

  2. 清理依赖冗余与冲突

    • requirements中同时加入了firebase和Pyrebase,两个库功能重叠易引发冲突,只保留项目实际使用的即可。
    • 避免使用kivy==master和KivyMD的master分支,这类开发版兼容性不稳定,建议改用稳定版本,比如kivy==2.2.1搭配kivymd==1.2.0。
    • smtplib是Python标准库,无需手动添加到requirements中,删除这类冗余依赖。
  3. 检查资源打包与路径

    • spec里的source.include_patterns包含images/*.png,但项目中只有assets文件夹存放图片,删掉该部分确保资源被正确打包。
    • 代码中引用图片必须用相对路径(如assets/xxx.png),绝对路径会导致设备上路径不匹配。
  4. 补充必要权限
    若应用用到网络(比如Firebase操作),需在buildozer.spec中添加网络权限:

    android.permissions = INTERNET
    

    缺少权限会直接导致启动崩溃。

  5. 最小化测试定位
    先编译一个极简KivyMD应用(比如仅显示一个按钮),用相同配置测试是否能正常运行。若可行,再逐步添加原项目的代码和资源,找到导致崩溃的具体模块。

内容的提问来源于stack exchange,提问作者Davide

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 19:25:14