This is an experimental set of build scripts that will cross-compile Python 3.12.4 for an Android device.
$ANDROID_NDK
points to its root directory. Older NDK may not work and NDK <= r18 is known to be incompatible.
export ANDROID_NDK="/home/myuser/android-ndk-r26d-linux" >> $HOME/.bashrc
sudo ./clean.sh
for good measure, and after each build.sudo docker run --rm -it -v $(pwd):/python3-android -v /mnt/c/SPE/android-ndk-r26d --env ARCH=arm64 --env ANDROID_API=30 python:3.12.4-slim /python3-android/docker-build.sh --enable-shared --without-ensurepip --disable-ipv6 --with-build-python
sudo docker run --rm -it -v $(pwd):/python3-android -v /mnt/c/SPE/android-ndk-r26d --env ARCH=arm --env ANDROID_API=30 python:3.12.4-slim /python3-android/docker-build.sh --enable-shared --without-ensurepip --disable-ipv6 --with-build-python
sudo docker run --rm -it -v $(pwd):/python3-android -v /mnt/c/SPE/android-ndk-r26d --env ARCH=x86_64 --env ANDROID_API=30 python:3.12.4-slim /python3-android/docker-build.sh --enable-shared --without-ensurepip --disable-ipv6 --with-build-python
adb shell
works finebuild
to a folder on the device (e.g., /data/local/tmp/python3
). Note that on most devices /sdcard
is not on a POSIX-compliant filesystem, so the python binary will not run from there.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:
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