使用Web3.py+Kivy+Buildozer在Colab编译APP遇cytoolz错误求助
基于Kivy+Web3.py的Android应用编译与运行问题
项目代码与配置
main.py
from kivymd.app import MDApp from kivymd.uix.label import MDLabel from web3 import Web3 class MainApp(MDApp): def build(self): bsc = 'https://bsc-dataseed.binance.org/' web3 = Web3(Web3.HTTPProvider(bsc)) if (web3.isConnected()): print("Connected!") return MDLabel(text="OK connected ! ", halign="center") else: print("KO") return MDLabel(text="KO ", halign="center") MainApp().run()
buildozer.spec 依赖配置
# (list) Application requirements # comma separated e.g. requirements = sqlite3,kivy requirements = python3, cython, kivy, kivymd, pillow, android, attrdict, certifi, cffi, chardet, cytoolz, eth-abi, eth-account, eth-accounts, eth-hash, eth-keyfile, eth-keys, eth-rlp, eth-utils, eth-typing, hexbytes, idna, lru-dict, openssl, pycryptodome, pyzbar, raven, requests, rlp, setuptools, toolz, web3
首次运行错误(架构不兼容)
编译成功但运行APP时,logcat输出错误:
08-22 08:36:31.065 16472 16505 I python : Traceback (most recent call last): 08-22 08:36:31.066 16472 16505 I python : File "/home/HelloWeb3/.buildozer/android/app/main.py", line 3, in <module> 08-22 08:36:31.066 16472 16505 I python : File "/home/HelloWeb3/.buildozer/android/platform/build-arm64-v8a/build/python-installs/helloweb3/arm64-v8a/web3/__init__.py", line 6, in <module> 08-22 08:36:31.066 16472 16505 I python : File "/home/HelloWeb3/.buildozer/android/platform/build-arm64-v8a/build/python-installs/helloweb3/arm64-v8a/eth_account/__init__.py", line 1, in <module> 08-22 08:36:31.066 16472 16505 I python : File "/home/HelloWeb3/.buildozer/android/platform/build-arm64-v8a/build/python-installs/helloweb3/arm64-v8a/eth_account/account.py", line 15, in <module> 08-22 08:36:31.066 16472 16505 I python : File "/home/HelloWeb3/.buildozer/android/platform/build-arm64-v8a/build/python-installs/helloweb3/arm64-v8a/cytoolz/__init__.py", line 1, in <module> 08-22 08:36:31.067 16472 16505 I python : ImportError: dlopen failed: "/data/user/0/org.test.helloweb3/files/app/_python_bundle/site-packages/cytoolz/itertoolz.so" is for EM_X86_64 (62) instead of EM_AARCH64 (183) 08-22 08:36:31.067 16472 16505 I python : Python for android ended.
自定义Recipe尝试及新错误
为解决架构不兼容问题,自定义了toolz和cytoolz的编译Recipe:
toolz Recipe
from pythonforandroid.recipe import CythonRecipe class ToolzRecipe(CythonRecipe): """Build toolz""" version = "0.12.0" url = "https://github.com/pytoolz/toolz/archive/refs/tags/{version}.zip" name = "toolz" depends = ["setuptools"] recipe = ToolzRecipe()
cytoolz Recipe
from pythonforandroid.recipe import CythonRecipe class CytoolzRecipe(CythonRecipe): """Build cytoolz""" version = "0.12.0" url = "https://github.com/pytoolz/cytoolz/archive/refs/tags/{version}.zip" name = "cytoolz" depends = ["setuptools"] recipe = CytoolzRecipe()
修改后编译出现新错误:
[DEBUG]: error: [Errno 2] No such file or directory: 'build/lib.linux-x86_64-3.9/cytoolz/_version.py'
解决建议
- 调整cytoolz Recipe的依赖与构建逻辑:cytoolz编译依赖toolz和cython,需在Recipe的
depends中添加这两项;同时,编译时需要先生成_version.py,可通过重写build_arch方法添加构建参数:from pythonforandroid.recipe import CythonRecipe class CytoolzRecipe(CythonRecipe): """Build cytoolz""" version = "0.12.0" url = "https://github.com/pytoolz/cytoolz/archive/refs/tags/{version}.zip" name = "cytoolz" depends = ["setuptools", "toolz", "cython"] def build_arch(self, arch): self.setup_extra_args = ["--force"] super().build_arch(arch) recipe = CytoolzRecipe() - 明确指定Android架构:在
buildozer.spec中添加android.arch = arm64-v8a,确保编译工具链生成对应架构的产物。 - 改用纯Python版toolz:若cytoolz编译问题难以解决,可将
requirements中的cytoolz替换为toolz(纯Python实现),web3.py可兼容该版本,跳过C扩展编译步骤。 - 清理缓存后重新编译:删除项目根目录下的
.buildozer文件夹,执行buildozer android debug clean,彻底清除旧编译产物。 - 匹配版本兼容性:确保toolz和cytoolz的版本完全一致,避免版本不匹配导致的构建问题。
内容的提问来源于stack exchange,提问作者tony
相关产品推荐
相关产品推荐

