使用Buildozer打包的Kivy应用在手机黑屏,电脑及Kivy Launcher可正常运行
Hey there! I’ve dealt with this exact black screen issue a few times when building Kivy apps for Android, so let’s walk through the most likely fixes to get your button app working properly:
1. Validate Your Buildozer Spec Configuration
First, double-check your buildozer.spec file—small mismatches here are a common culprit:
- Match Kivy versions: Make sure the
requirementsline uses the exact same Kivy version you used for desktop development. For example, if you built with Kivy 2.2.1 locally, set:
Version inconsistencies can break rendering or component initialization on Android.requirements = python3,kivy==2.2.1 - Adjust Android API level: Set
android.apito a level compatible with your phone. If your device runs Android 12/13, useandroid.api = 33; for older devices, try 29 or 30. Avoid using the absolute latest API unless you’ve tested compatibility. - Add basic permissions: Even if your app doesn’t need internet, adding
INTERNETpermission can resolve hidden loading issues. Update this line:android.permissions = INTERNET
2. Dig Into Logcat for Error Details
The best way to pinpoint the issue is to check your app’s runtime logs on Android:
- Run this command in your project directory while your phone is connected via USB (with debugging enabled):
buildozer android logcat - Launch the app on your phone, then look for error messages in the log output. Common issues include:
- Typos or syntax errors in your
.kvfile that didn’t break desktop/Kivy Launcher but crash on Android. - Unhandled exceptions in your button’s callback methods (even a missing
passor incorrect variable reference can cause a silent crash). - Missing resources (like images or fonts) that you assumed were bundled correctly.
- Typos or syntax errors in your
3. Simplify Your Code to Isolate the Problem
Strip your app down to the smallest working version with a button, then rebuild:
from kivy.app import App from kivy.uix.button import Button class TestApp(App): def build(self): btn = Button(text="Click Me") btn.bind(on_press=self.on_click) return btn def on_click(self, instance): print("Button clicked!") if __name__ == "__main__": TestApp().run()
If this simplified app works, gradually reintroduce parts of your original code until the black screen returns. This will help you identify exactly which component or line is causing the issue.
4. Fix Rendering or Initialization Quirks
Sometimes Android-specific rendering settings are to blame:
- Add these lines at the very top of your main.py file to disable multisampling, which can resolve black screen issues on some devices:
from kivy.config import Config Config.set('graphics', 'multisamples', '0') - Double-check that your
build()method actually returns a root widget. It’s easy to accidentally omit thereturnstatement, which leaves the app with no UI to display.
Hopefully one of these steps gets your app running smoothly on Android!
内容的提问来源于stack exchange,提问作者S. X. Apo

