Skip to main content

Build native packages for Android.

Requirements

  • Python;
  • Android SDK;

The Android SDK can be installed from Android Studio.

Then set the environment variable ANDROID_SDK_ROOT or ANDROID_HOME.

Finally, Python's distutils package must be patched for cross-compiling. The patch is in the patches directory. Apply it like so:

DESTLIB=$(python -c "import sysconfig; \
    print(sysconfig.get_config_var('DESTLIB'))")
patch -p2 -d $DESTLIB < patches/patch-Python-3.10.0.txt

This patch should apply to any version of Python.

You may need to use sudo.

Install androidenv

Install from the Python Package Index:

pip install androidenv

You can also copy the file androidenv.py to where you need it, it is self contained.

Usage

Build something from source:

cd thing
python -m androidenv setup.py build
python -m androidenv setup.py install

You really want to do this in a virtual environment.

Use the --find-library option to find libraries like libandroid or liblog:

python -m androidenv --find-library android log

Environment variables

This module works entirely with environment variables.

Input environment variables:

  • ANDROID_SDK_ROOT or ANDROID_HOME (required);
  • ABI (optional; armeabi-v7a or arm64-v8a);
  • API (optional);
  • DEBUG (optional; 0 or 1);

What is being built must respect the following evironment variables:

  • AR
  • AS
  • CC
  • CFLAGS
  • CPP
  • CPPFLAGS
  • CXX
  • CXXFLAGS
  • LD
  • LDFLAGS
  • PATH
  • RANLIB
  • READELF

Many packages do, many don't. Python itself (distutils) does not respect RANLIB; CMake does not respect CPPFLAGS; and many more. Fixing that is your homework.

setup.cfg

Some useful configuration options when building for Android:

[build]
build-base = build
build-temp = build/tmp
build-lib = build/lib

Examples

Python

test -d "$ANDROID_SDK_ROOT" || echo "uhoh"
gunzip < Python-3.7.11.tgz | tar -f - -x
cd Python-3.7.11
cp configure.ac configure.ac.orig
sed -e 's/ $host / "$host" /' -e 's/"$host"/"$build"/' \
    < configure.ac.orig > configure.ac
autoreconf -f -i
python -m androidenv /bin/sh
./configure \
    --host=aarch64-apple-darwin \
    --build=aarch64-linux-android \
    --disable-ipv6 \
    ac_cv_file__dev_ptmx=no \
    ac_cv_file__dev_ptc=no
make

NumPy

test -d "$ANDROID_SDK_ROOT" || echo "uhoh"
unzip numpy-1.20.3.zip
cd numpy-1.20.3
export BLAS=None LAPACK=None ATLAS=None
ABI=arm64-v8a API=24 python -m androidenv setup.py build
ABI=arm64-v8a API=24 python -m androidenv setup.py install

Release files for androidenv 0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for androidenv 0.8
File Size Uploaded
androidenv-0.8.tar.gz 5.9 kB Details

Release files / androidenv-0.8.tar.gz

Download URL androidenv-0.8.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
31472fd22ac34e403160c15f0cca602db2954e4f744546a1208e83dc4e29386a
BLAKE2b-256 checksum
How to use checksums
1ba16382e18e81cb86f80f9600f5d6d5781a327ad31a2f01e917f38298109fe0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

Release history Release notifications | RSS feed

1.0

1 release file

0.9

1 release file

This release

0.8 This release

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page