This is an experimental set of build scripts that will cross-compile Python 3.9.0 for an Android device.
Building requires:
- Linux. This project might work on other systems supported by NDK but no guarantee.
- Android NDK r21 installed and environment variable
$ANDROID_NDKpoints to its root directory. Older NDK may not work and NDK <= r18 is known to be incompatible. python3.9binary from Python 3.9.0 on the building host. It's recommended to use exactly that Python version, which can be installed via pyenv. Don't forget to check thatpython3.9is available in $PATH.ticbinary from ncurses 6.2 on the building host. Slightly newer or older version may also work but no guarantee.- A case-sensitive filesystem. The default filesystem on Windows and macOS is case-insensitive, and building may fail.
Running requires:
- Android 5.0 (Lollipop, API 21) or above
- arm, arm64, x86 or x86-64
- Run
./clean.shfor good measure. - For every API Level/architecture combination you wish to build for:
ARCH=arm ANDROID_API=21 ./build.shto build everything!
Download the latest NDK for Linux from https://developer.android.com/ndk/downloads and extract it.
docker run --rm -it -v $(pwd):/python3-android -v /path/to/android-ndk:/android-ndk:ro --env ARCH=arm --env ANDROID_API=21 python:3.9.0-slim /python3-android/docker-build.sh
Here /path/to/android-ndk should be replaced with the actual for NDK (e.g., /opt/android-ndk).
Podman is also supported. Simply replace docker with podman in the command above.
- Make sure
adb shellworks fine - Copy all files in
buildto a folder on the device (e.g.,/data/local/tmp/python3). Note that on most devices/sdcardis not on a POSIX-compliant filesystem, so the python binary will not run from there. - In adb shell:
cd /data/local/tmp/build . ./env.sh python3
And have fun!
SSL certificates have old and new naming schemes. Android uses the old scheme yet the latest OpenSSL uses the new one. If you got CERTIFICATE_VERIFY_FAILED when using SSL/TLS in Python, you need to collect system certificates: (thanks @GRRedWings for the idea)
cd /data/local/tmp/build
mkdir -p etc/ssl
cat /system/etc/security/cacerts/* > etc/ssl/cert.pem
Path for certificates may vary with device vendor and/or Android version. Note that this approach only collects system certificates. If you need to collect user-installed certificates, most likely root access on your Android device is needed.
Check SSL/TLS functionality with:
import urllib.request
print(urllib.request.urlopen('https://httpbin.org/ip').read().decode('ascii'))
No big issues! yay