android: add run-time patch to make pycryptodomex work
What changed, and why it matters
This commit adds a small Android-only workaround so that a cryptography library (pycryptodomex) can load correctly inside the Electrum mobile app. It replaces the way Python finds its own C API with an explicit path to the Python shared library. There is no direct security vulnerability here; it is a compatibility patch for a packaging/toolchain issue. However, it is a 'hack' that touches low-level Python internals, so it could in theory affect app stability or future updates.
Treat as a maintenance/compatibility patch rather than a security fix. Reviewers should verify that `libpython%d.%d.so` exists and is loadable on all supported Android ABIs and Python versions, and that the workaround does not interfere with other ctypes users. Consider removing the hack once python-for-android upstream definitively resolves issue #1866 and Electrum rebases to that p4a version.
Security signals we found
Low-level ctypes/pythonapi manipulation
Workaround for missing/undefined CPython symbols in Android build
Dependency on external shared library path derived from sys.version_info
No input validation or sanititation involved (not applicable to this patch)
Evidence from the diff
On Android builds created with python-for-android, ctypes.pythonapi defaults to ctypes.PyDLL(None), which fails to resolve symbols such as PyObject_GetBuffer because the Python shared library is not loaded with global symbol visibility. The patch explicitly sets ctypes.pythonapi = ctypes.PyDLL('libpythonX.Y.so') before pycryptodomex is imported, allowing PyCryptodome’s ctypes-based raw API to find the needed CPython buffer protocol symbols. The change is gated behind if is_android and is sourced from an upstream p4a issue comment.
Changed components
run_electrum startup scriptAndroid build of Electrumpycryptodomex / PyCryptodome initialization pathctypes/pythonapi on AndroidInspect captured patch +7 / −0
diff --git a/run_electrum b/run_electrum
index e9b3ab8..3d75478 100755
--- a/run_electrum
+++ b/run_electrum
@@ -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
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.