Buildozer 无法编译 .apk,因为未定义“xrange”

Posted

技术标签:

【中文标题】Buildozer 无法编译 .apk,因为未定义“xrange”【英文标题】:Buildozer fail to compile .apk because 'xrange' is not defined 【发布时间】:2021-04-13 11:22:29 【问题描述】:

我在 buildozer 编译过程中遇到了 python3 pip 或 hmac 的问题。

错误提示“未定义 xrange”,但仅当我在以前的 buildozer 文件夹 (.buildozer) 上编译时才会发生这种情况。

如果我做了一个干净的构建删除 .buildozer 文件夹,它会按预期工作,而且 .apk 也能正常工作。

我已经在 main.py 中添加了“xrange = range”

任何帮助将不胜感激。提前谢谢。

这是完整的构建器错误:

[INFO]:    *** PYTHON PACKAGE / PROJECT INSTALL STAGE ***
[INFO]:    The requirements (kivy-garden.qrcode, kivy-garden.xcamera, kivy-garden.zbarcam, unix-time) don't have recipes, attempting to install them with pip
[INFO]:    If this fails, it may mean that the module has compiled components and needs a recipe.
[INFO]:    -> directory context /home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build
[INFO]:    -> running python3 -m venv venv
[INFO]:    Upgrade pip to latest version
[INFO]:    -> running bash -c source venv/bin/activate && pip install -U pip
Exception in thread background thread for pid 27840:                                                                                                                                                     
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ciko/.local/lib/python3.7/site-packages/sh.py", line 1633, in wrap
    fn(*rgs, **kwargs)
  File "/home/ciko/.local/lib/python3.7/site-packages/sh.py", line 2557, in background_thread
    handle_exit_code(exit_code)
  File "/home/ciko/.local/lib/python3.7/site-packages/sh.py", line 2261, in fn
    return self.command.handle_command_exit_code(exit_code)
  File "/home/ciko/.local/lib/python3.7/site-packages/sh.py", line 861, in handle_command_exit_code
    raise exc
sh.ErrorReturnCode_1: 

  RAN: /bin/bash -c 'source venv/bin/activate && pip install -U pip'

  STDOUT:
Traceback (most recent call last):
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/bin/pip", line 10, in <module>
    sys.exit(main())
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/commands/__init__.py", line 105, in create_command
    module = importlib.import_module(module_path)
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/Lib/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 17, in <module>
    from pip._internal.cli.req_command import RequirementCommand, with_cleanup
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 18, in <module>
    from pip._internal.index.collector import LinkCollector
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/index/collector.py", line 14, in <module>
    from pip._vendor import html5lib, requests
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/requests/__init__.py", line 43, in <module>
    from pip._vendor import urllib3
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/__init__.py", line 13, in <module>
    from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/connectionpool.py", line 11, in <module>
    from .connection import (
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/connection.py", line 15, in <module>
    from .util.proxy import create_proxy_ssl_context
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/util/__init__.py", line 8, in <module>
    from .ssl_ import (
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/util/ssl_.py", line 3, in <module>
    import hmac
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/bitcodepro/hmac.py", line 8, in <module>
    trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
NameError: name 'xrange' is not defined


  STDERR:


Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/toolchain.py", line 1231, in <module>
    main()
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/entrypoints.py", line 18, in main
    ToolchainCL()
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/toolchain.py", line 688, in __init__
    getattr(self, command)(args)
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/toolchain.py", line 154, in wrapper_func
    build_dist_from_args(ctx, dist, args)
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/toolchain.py", line 208, in build_dist_from_args
    args, "ignore_setup_py", False
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/build.py", line 598, in build_recipes
    ignore_setup_py=ignore_project_setup_py
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/build.py", line 772, in run_pymodules_install
    ), _env=copy.copy(base_env))
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/python-for-android/pythonforandroid/logger.py", line 167, in shprint
    for line in output:
  File "/home/ciko/.local/lib/python3.7/site-packages/sh.py", line 907, in next
    self.wait()
  File "/home/ciko/.local/lib/python3.7/site-packages/sh.py", line 837, in wait
    self.handle_command_exit_code(exit_code)
  File "/home/ciko/.local/lib/python3.7/site-packages/sh.py", line 861, in handle_command_exit_code
    raise exc
sh.ErrorReturnCode_1: 

  RAN: /bin/bash -c 'source venv/bin/activate && pip install -U pip'

  STDOUT:
Traceback (most recent call last):
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/bin/pip", line 10, in <module>
    sys.exit(main())
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/cli/main.py", line 73, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/commands/__init__.py", line 105, in create_command
    module = importlib.import_module(module_path)
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/other_builds/hostpython3/desktop/hostpython3/Lib/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 17, in <module>
    from pip._internal.cli.req_command import RequirementCommand, with_cleanup
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 18, in <module>
    from pip._internal.index.collector import LinkCollector
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_internal/index/collector.py", line 14, in <module>
    from pip._vendor import html5lib, requests
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/requests/__init__.py", line 43, in <module>
    from pip._vendor import urllib3
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/__init__.py", line 13, in <module>
    from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/connectionpool.py", line 11, in <module>
    from .connection import (
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/connection.py", line 15, in <module>
    from .util.proxy import create_proxy_ssl_context
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/util/__init__.py", line 8, in <module>
    from .ssl_ import (
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/venv/lib/python3.7/site-packages/pip/_vendor/urllib3/util/ssl_.py", line 3, in <module>
    import hmac
  File "/home/ciko/Scrivania/bitCodePro-App/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/bitcodepro/hmac.py", line 8, in <module>
    trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
NameError: name 'xrange' is not defined

编辑:

问题在于 hmac,因为它是为 python2 设计的。 问题出在 hmac.py 第 8 行和第 9 行:

trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
                                             ^
trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
                                             ^

在 python3 中,'xrange' 已被删除并替换为 'range'。所以我试图在我的 main.py 中添加'xrange = range',但它只适用于 buildozer clean 构建过程,而不是如果 .buildozer 文件夹已经存在,即使我构建了一个新的 .apk,它也适用于 clean构建但在第二次构建期间失败。

上网我找到了另一种解决问题的方法,方法是通过 pip 下载“未来”包并将以下行添加到 main.py

from past.builtins import xrange

但正如我所说,它适用于干净的构建并在接下来的构建中失败。

我的目标是不要每次都进行干净的构建,因为过程太长了。有什么想法吗?

【问题讨论】:

【参考方案1】:

我有办法!

    从 buildozer.spec 要求中删除“hmac”; 从here下载官方python3.7 hmac.py 将 hmac.py 放入您的 buildozer 文件夹中,可能位于名为“my_pkgs”的专用子文件夹中,并以这种方式直接从您的 main.py 导入:
    from my_pkgs import hmac

它在第一次干净构建期间完美运行,也适用于使用相同 .buildozer 文件夹发布的所有其他构建。 正是我正在寻找的。​​p>

[已解决]由我!

【讨论】:

以上是关于Buildozer 无法编译 .apk,因为未定义“xrange”的主要内容,如果未能解决你的问题,请参考以下文章

在 Ubuntu 12.04 上使用 Kivy 和 Buildozer 制作 apk

Buildozer 编译 apk,但它在 android 上崩溃

我无法通过 buildozer 生成 APK

使用 buildozer 创建 kivy apk 错误:“Aidl 无法执行”

buildozer(kivy,python) 无法将生成的 apk 复制到 docker 虚拟机上的主目录

无法在 Google Colab 中使用 buildozer 构建我的应用 apk?