MacOS下Buildozer编译Android应用报错:C编译器无法生成可执行文件
问题描述
在MacOS系统的Python 3.11虚拟环境中,使用Kivy 2.3.1、Buildozer 1.5.0、python-for-android 2024.01.21执行buildozer android debug命令时出现报错:
[DEBUG]: configure: error: in `/Users/macbook/Documents/code/py/vmonfront/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/python3/armeabi-v7a__ndk_target_21/python3/android-build': [DEBUG]: configure: error: C compiler cannot create executables [DEBUG]: See `config.log' for more details Exception in thread background thread for pid 85320: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.11/3.11.12_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1045, in _bootstrap_inner self.run() File "/opt/homebrew/Cellar/python@3.11/3.11.12_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 982, in run self._target(*self._args, **self._kwargs) File "/Users/macbook/Documents/code/py/vmonfront/venv311/lib/python3.11/site-packages/sh.py", line 1642, in wrap fn(*rgs, **kwargs) File "/Users/macbook/Documents/code/py/vmonfront/venv311/lib/python3.11/site-packages/sh.py", line 2647, in background_thread handle_exit_code(exit_code) File "/Users/macbook/Documents/code/py/vmonfront/venv311/lib/python3.11/site-packages/sh.py", line 2338, in fn return self.command.handle_command_exit_code(exit_code) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/macbook/Documents/code/py/vmonfront/venv311/lib/python3.11/site-packages/sh.py", line 823, in handle_command_exit_code raise exc sh.ErrorReturnCode_77: RAN: /Users/macbook/Documents/code/py/vmonfront/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/python3/armeabi-v7a__ndk_target_21/python3/configure --host=arm-linux-androideabi --build=aarch64-apple-darwin24.5.0 --enable-shared --enable-ipv6 ac_cv_file__dev_ptmx=yes ac_cv_file__dev_ptc=no --without-ensurepip ac_cv_little_endian_double=yes ac_cv_header_sys_eventfd_h=no --prefix=/usr/local --exec-prefix=/usr/local --enable-loadable-sqlite-extensions --with-build-python=/Users/macbook/Documents/code/py/vmonfront/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/native-build/python3 --with-openssl=/Users/macbook/Documents/code/py/vmonfront/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/openssl/armeabi-v7a__ndk_target_21/openssl3.0.16
直接调用C编译器时输出如下,看起来编译器运行正常:
/Users/macbook/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi21-clang ; exit; (base) macbook@JunCs-MacBook-Pro ~ % /Users/macbook/Library/Android/sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi21-clang ; exit; clang-14: error: no input files Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. [Process completed]
附buildozer.spec文件内容:
[app] # (str) Title of your application title = Video Monitor Front # (str) Package name package.name = vmonfront # (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 # (str) Application versioning (method 1) version = 0.1 # (list) Application requirements # comma separated e.g. requirements = sqlite3,kivy requirements = python3==3.11.12,kivy,pyjnius, openssl==3.0.16 #,openssl==3.0.16,cryptography==42.0.8 # android.skip_download = True android.accept_sdk_license = True android.ndk_version = 25b android.sdk_version = 34 android.api = 34 android.minapi = 21 android.ndk_path = /Users/macbook/Library/Android/sdk/ndk/25.2.9519653 android.sdk_path = /Users/macbook/Library/Android/sdk android.sdkmanager_path = /Users/macbook/Library/Android/sdk/cmdline-tools/latest/bin/sdkmanager p4a.branch = develop #android.archs = arm64-v8a #android.extra_patches = patches/pyjnius_long_fix.patch:pyjnius # (list) Supported orientations # Valid options are: landscape, portrait, portrait-reverse or landscape-reverse orientation = portrait # # OSX Specific # # # author = © Copyright Info # change the major version of python used by the app osx.python_version = 3 # Kivy version to use osx.kivy_version = 1.9.1 # # Android specific # # (bool) Indicate if the application should be fullscreen or not fullscreen = android.python = 3.11.12 # Explicitly set Python version # (bool) enables Android auto backup feature (Android API >=23) android.allow_backup = True # # Python for android (p4a) specific # # # iOS specific # # (str) Path to a custom kivy-ios folder #ios.kivy_ios_dir = ../kivy-ios # Alternately, specify the URL and branch of a git checkout: ios.kivy_ios_url = https://github.com/kivy/kivy-ios ios.kivy_ios_branch = master # Another platform dependency: ios-deploy # Uncomment to use a custom checkout #ios.ios_deploy_dir = ../ios_deploy # Or specify URL and branch ios.ios_deploy_url = https://github.com/phonegap/ios-deploy ios.ios_deploy_branch = 1.10.0 # (bool) Whether or not to sign the code ios.codesign.allowed = false [buildozer] # (int) Log level (0 = error only, 1 = info, 2 = debug (with command output)) log_level = 2 # (int) Display warning if buildozer is run as root (0 = False, 1 = True) warn_on_root = 1
请问问题出在哪里?是配置问题吗?
解决方案
从报错和配置来看,问题大概率出在NDK版本兼容性或交叉编译环境配置上,以下是具体排查和修复步骤:
1. 对齐python-for-android与NDK版本
你使用的python-for-android是2024.01.21版本,搭配的NDK是25b。虽然NDK25理论兼容,但develop分支的p4a可能对版本有更严格要求:
- 把
android.ndk_version改成25c或26b,同步更新android.ndk_path到对应NDK目录 - 或者把
p4a.branch从develop改成2024.01.21(和你当前p4a版本一致),避免开发分支的兼容性坑
2. 清理buildozer.spec中的冲突配置
requirements里显式指定openssl==3.0.16可能和NDK交叉编译工具链冲突,先删掉这个指定,让p4a自动选择兼容版本- 注释掉
android.skip_download = True,让buildozer自动校验并下载依赖,避免手动路径配置导致的不匹配
3. 重置构建缓存
直接删除项目根目录下的.buildozer文件夹,清理之前的缓存构建文件,然后重新执行buildozer android debug,排除缓存残留问题
4. 查看config.log找具体原因
报错里提到的config.log是关键,找到对应路径下的这个文件,里面会记录configure时测试编译失败的具体细节(比如缺库、链接错误),能直接定位问题根源
内容的提问来源于stack exchange,提问作者user938363
相关产品推荐
相关产品推荐

