android: add run-time patch to make pycryptodomex work

to work around https://github.com/kivy/python-for-android/issues/1866 :

> PyCryptodome >=3.6.0 crashes at runtime (since commit https://github.com/Legrandin/pycryptodome/commit/f5aa2c1618e97b6d773172fdd07794a0a6f05905). So actually the currently pinned version in the recipe does not work:
> https://github.com/kivy/python-for-android/blob/80e4f059c1ee0da48a7c85167087dfe5928ac395/pythonforandroid/recipes/pycryptodome/__init__.py#L5
>
> The issue at runtime is with ctypes.
>
>
> Say I have a main script that just does the following (https://github.com/Legrandin/pycryptodome/blob/95ccce7ae82d3a36f1a8652dd2c645222d0128dd/lib/Crypto/Util/_raw_api.py#L200):
> ```
>  import ctypes
>  ctypes.pythonapi.PyObject_GetBuffer
>  ```
>
> This works with cpython on my laptop, but with the p4a-compiled python on Android it fails:
>
> ```
> 06-14 19:06:27.053 15246 15274 I python  : Android kivy bootstrap done. __name__ is __main__
> 06-14 19:06:27.053 15246 15274 I python  : AND: Ran string
> 06-14 19:06:27.053 15246 15274 I python  : Run user program, change dir and execute entrypoint
> 06-14 19:06:27.092 15246 15274 I python  : Traceback (most recent call last):
> 06-14 19:06:27.092 15246 15274 I python  :   File "/home/user/wspace/electrum/.buildozer/android/app/main.py", line 84, in <module>
> 06-14 19:06:27.093 15246 15274 I python  :   File "/home/user/wspace/electrum/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/ctypes/__init__.py", line 369, in __getattr__
> 06-14 19:06:27.093 15246 15274 I python  :   File "/home/user/wspace/electrum/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/ctypes/__init__.py", line 374, in __getitem__
> 06-14 19:06:27.094 15246 15274 I python  : AttributeError: undefined symbol: PyObject_GetBuffer
> ```
>
> I have also tried to access some other attributes of `ctypes.pythonapi`, such as `Py_IncRef`, which raises the same exception.

---

Unclear if the issue still affects newer p4a: upstream seems to think it's fixed, but there multiple comments in the thread saying otherwise. Anyway, rebasing p4a has other blockers atm.
This commit is contained in:
SomberNight
2025-11-30 05:57:03 +00:00
parent cfa052bc86
commit 5b67f21bc4
+7
View File
@@ -100,6 +100,13 @@ if not is_android:
check_imports()
if is_android:
# hack to make pycryptodomex work on Android
# from https://github.com/kivy/python-for-android/issues/1866#issuecomment-927157780
import ctypes
ctypes.pythonapi = ctypes.PyDLL("libpython%d.%d.so" % sys.version_info[:2]) # replaces ctypes.PyDLL(None)
sys._ELECTRUM_RUNNING_VIA_RUNELECTRUM = True # used by logging.py
from electrum.logging import get_logger, configure_logging # import logging submodule first