2024-03-21 23:52:29 +00:00
|
|
|
|
# Python for Android
|
|
|
|
|
|
|
2025-04-01 01:46:29 +01:00
|
|
|
|
If you obtained this README as part of a release package, then the only
|
|
|
|
|
|
applicable sections are "Prerequisites", "Testing", and "Using in your own app".
|
2024-03-21 23:52:29 +00:00
|
|
|
|
|
2025-04-01 01:46:29 +01:00
|
|
|
|
If you obtained this README as part of the CPython source tree, then you can
|
|
|
|
|
|
also follow the other sections to compile Python for Android yourself.
|
|
|
|
|
|
|
|
|
|
|
|
However, most app developers should not need to do any of these things manually.
|
|
|
|
|
|
Instead, use one of the tools listed
|
|
|
|
|
|
[here](https://docs.python.org/3/using/android.html), which will provide a much
|
|
|
|
|
|
easier experience.
|
2024-03-21 23:52:29 +00:00
|
|
|
|
|
2025-04-01 01:46:29 +01:00
|
|
|
|
## Prerequisites
|
2024-09-13 05:23:54 +01:00
|
|
|
|
|
2025-04-01 01:46:29 +01:00
|
|
|
|
If you already have an Android SDK installed, export the `ANDROID_HOME`
|
|
|
|
|
|
environment variable to point at its location. Otherwise, here's how to install
|
|
|
|
|
|
it:
|
2024-03-21 23:52:29 +00:00
|
|
|
|
|
|
|
|
|
|
* Download the "Command line tools" from <https://developer.android.com/studio>.
|
|
|
|
|
|
* Create a directory `android-sdk/cmdline-tools`, and unzip the command line
|
|
|
|
|
|
tools package into it.
|
|
|
|
|
|
* Rename `android-sdk/cmdline-tools/cmdline-tools` to
|
|
|
|
|
|
`android-sdk/cmdline-tools/latest`.
|
|
|
|
|
|
* `export ANDROID_HOME=/path/to/android-sdk`
|
|
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
The `Platforms/Android` script will automatically use the SDK's `sdkmanager` to install
|
2025-05-01 05:17:41 +01:00
|
|
|
|
any packages it needs.
|
|
|
|
|
|
|
|
|
|
|
|
The script also requires the following commands to be on the `PATH`:
|
2024-05-01 07:36:45 +01:00
|
|
|
|
|
|
|
|
|
|
* `curl`
|
2024-08-16 06:00:29 +01:00
|
|
|
|
* `java` (or set the `JAVA_HOME` environment variable)
|
2024-05-01 07:36:45 +01:00
|
|
|
|
|
2024-03-21 23:52:29 +00:00
|
|
|
|
## Building
|
|
|
|
|
|
|
2024-05-01 07:36:45 +01:00
|
|
|
|
Python can be built for Android on any POSIX platform supported by the Android
|
2025-04-01 01:46:29 +01:00
|
|
|
|
development tools, which currently means Linux or macOS.
|
|
|
|
|
|
|
|
|
|
|
|
First we'll make a "build" Python (for your development machine), then use it to
|
|
|
|
|
|
help produce a "host" Python for Android. So make sure you have all the usual
|
|
|
|
|
|
tools and libraries needed to build Python for your development machine.
|
2024-05-01 07:36:45 +01:00
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
The easiest way to do a build is to use the `Platforms/Android` script. You can either
|
2024-03-21 23:52:29 +00:00
|
|
|
|
have it perform the entire build process from start to finish in one step, or
|
|
|
|
|
|
you can do it in discrete steps that mirror running `configure` and `make` for
|
|
|
|
|
|
each of the two builds of Python you end up producing.
|
|
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
The discrete steps for building via `Platforms/Android` are:
|
2024-03-21 23:52:29 +00:00
|
|
|
|
|
|
|
|
|
|
```sh
|
2026-04-14 05:41:16 +08:00
|
|
|
|
python3 Platforms/Android configure-build
|
|
|
|
|
|
python3 Platforms/Android make-build
|
|
|
|
|
|
python3 Platforms/Android configure-host HOST
|
|
|
|
|
|
python3 Platforms/Android make-host HOST
|
2024-03-21 23:52:29 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
2024-05-01 07:36:45 +01:00
|
|
|
|
`HOST` identifies which architecture to build. To see the possible values, run
|
2026-04-14 05:41:16 +08:00
|
|
|
|
`python3 Platforms/Android configure-host --help`.
|
2024-03-21 23:52:29 +00:00
|
|
|
|
|
2024-05-01 07:36:45 +01:00
|
|
|
|
To do all steps in a single command, run:
|
2024-03-21 23:52:29 +00:00
|
|
|
|
|
|
|
|
|
|
```sh
|
2026-04-14 05:41:16 +08:00
|
|
|
|
python3 Platforms/Android build HOST
|
2024-03-21 23:52:29 +00:00
|
|
|
|
```
|
2025-04-01 01:46:29 +01:00
|
|
|
|
In the end you should have a build Python in `cross-build/build`, and a host
|
|
|
|
|
|
Python in `cross-build/HOST`.
|
2024-03-21 23:52:29 +00:00
|
|
|
|
|
|
|
|
|
|
You can use `--` as a separator for any of the `configure`-related commands –
|
|
|
|
|
|
including `build` itself – to pass arguments to the underlying `configure`
|
|
|
|
|
|
call. For example, if you want a pydebug build that also caches the results from
|
|
|
|
|
|
`configure`, you can do:
|
|
|
|
|
|
|
|
|
|
|
|
```sh
|
2026-04-14 05:41:16 +08:00
|
|
|
|
python3 Platforms/Android build HOST -- -C --with-pydebug
|
2024-03-21 23:52:29 +00:00
|
|
|
|
```
|
2024-05-01 07:36:45 +01:00
|
|
|
|
|
2025-04-01 01:46:29 +01:00
|
|
|
|
## Packaging
|
|
|
|
|
|
|
|
|
|
|
|
After building an architecture as described in the section above, you can
|
|
|
|
|
|
package it for release with this command:
|
|
|
|
|
|
|
|
|
|
|
|
```sh
|
2026-04-14 05:41:16 +08:00
|
|
|
|
python3 Platforms/Android package HOST
|
2025-04-01 01:46:29 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
`HOST` is defined in the section above.
|
|
|
|
|
|
|
|
|
|
|
|
This will generate a tarball in `cross-build/HOST/dist`, whose structure is
|
|
|
|
|
|
similar to the `Android` directory of the CPython source tree.
|
|
|
|
|
|
|
2024-05-01 07:36:45 +01:00
|
|
|
|
## Testing
|
|
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
Tests can be run on Linux, macOS, or Windows, using either an Android emulator
|
|
|
|
|
|
or a physical device.
|
2024-09-13 05:23:54 +01:00
|
|
|
|
|
2025-08-12 18:16:04 +01:00
|
|
|
|
On Linux, the emulator needs access to the KVM virtualization interface. This may
|
|
|
|
|
|
require adding your user to a group, or changing your udev rules. On GitHub
|
|
|
|
|
|
Actions, the test script will do this automatically using the commands shown
|
|
|
|
|
|
[here](https://github.blog/changelog/2024-04-02-github-actions-hardware-accelerated-android-virtualization-now-available/).
|
2024-08-16 06:00:29 +01:00
|
|
|
|
|
2025-04-01 01:46:29 +01:00
|
|
|
|
The test script supports the following modes:
|
2024-08-16 06:00:29 +01:00
|
|
|
|
|
|
|
|
|
|
* In `--connected` mode, it runs on a device or emulator you have already
|
|
|
|
|
|
connected to the build machine. List the available devices with
|
|
|
|
|
|
`$ANDROID_HOME/platform-tools/adb devices -l`, then pass a device ID to the
|
|
|
|
|
|
script like this:
|
|
|
|
|
|
|
|
|
|
|
|
```sh
|
2026-04-14 05:41:16 +08:00
|
|
|
|
python3 Platforms/Android test --connected emulator-5554
|
2024-08-16 06:00:29 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
* In `--managed` mode, it uses a temporary headless emulator defined in the
|
|
|
|
|
|
`managedDevices` section of testbed/app/build.gradle.kts. This mode is slower,
|
|
|
|
|
|
but more reproducible.
|
|
|
|
|
|
|
|
|
|
|
|
We currently define two devices: `minVersion` and `maxVersion`, corresponding
|
|
|
|
|
|
to our minimum and maximum supported Android versions. For example:
|
|
|
|
|
|
|
|
|
|
|
|
```sh
|
2026-04-14 05:41:16 +08:00
|
|
|
|
python3 Platforms/Android test --managed maxVersion
|
2024-08-16 06:00:29 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
By default, the only messages the script will show are Python's own stdout and
|
|
|
|
|
|
stderr. Add the `-v` option to also show Gradle output, and non-Python logcat
|
|
|
|
|
|
messages.
|
|
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
### Testing Python
|
|
|
|
|
|
|
|
|
|
|
|
You can run the test suite by doing a build as described above, and then running
|
|
|
|
|
|
`python3 Platforms/Android test`. On Windows, you won't be able to do the build
|
|
|
|
|
|
on the same machine, so you'll have to copy the `cross-build/HOST/prefix` directory
|
|
|
|
|
|
from somewhere else.
|
|
|
|
|
|
|
|
|
|
|
|
Extra arguments on the `Platforms/Android test` command line will be passed through
|
|
|
|
|
|
to `python -m test` – use `--` to separate them from `Platforms/Android`'s own options.
|
2024-08-16 06:00:29 +01:00
|
|
|
|
See the [Python Developer's
|
|
|
|
|
|
Guide](https://devguide.python.org/testing/run-write-tests/) for common options
|
2025-02-16 10:33:14 +09:00
|
|
|
|
– most of them will work on Android, except for those that involve subprocesses,
|
2024-08-16 06:00:29 +01:00
|
|
|
|
such as `-j`.
|
|
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
Every time you run `python3 Platforms/Android test`, changes in pure-Python files in the
|
2024-08-16 06:00:29 +01:00
|
|
|
|
repository's `Lib` directory will be picked up immediately. Changes in C files,
|
|
|
|
|
|
and architecture-specific files such as sysconfigdata, will not take effect
|
2026-04-14 05:41:16 +08:00
|
|
|
|
until you re-run `python3 Platforms/Android make-host` or `build`.
|
|
|
|
|
|
|
|
|
|
|
|
### Testing a third-party package
|
|
|
|
|
|
|
|
|
|
|
|
The `Platforms/Android` script is also included as `android.py` in the root of a
|
|
|
|
|
|
release package (i.e., the one built using `Platforms/Android package`).
|
|
|
|
|
|
|
|
|
|
|
|
You can use this script to test third-party packages by taking a release
|
|
|
|
|
|
package, extracting it wherever you want, and using the `android.py` script to
|
|
|
|
|
|
run the test suite for your third-party package.
|
|
|
|
|
|
|
|
|
|
|
|
Any argument that can be passed to `python3 Platforms/Android test` can also be
|
|
|
|
|
|
passed to `android.py`. The following options will be of particular use when
|
|
|
|
|
|
configuring the execution of a third-party test suite:
|
|
|
|
|
|
|
|
|
|
|
|
* `--cwd`: the directory of content to copy into the testbed app as the working
|
|
|
|
|
|
directory.
|
|
|
|
|
|
* `--site-packages`: the directory to copy into the testbed app to use as site
|
|
|
|
|
|
packages.
|
2024-09-24 00:09:53 +01:00
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
Extra arguments on the `android.py test` command line will be passed through to
|
|
|
|
|
|
Python – use `--` to separate them from `android.py`'s own options. You must include
|
|
|
|
|
|
either a `-c` or `-m` argument to specify how the test suite should be started.
|
2025-06-05 06:46:16 +01:00
|
|
|
|
|
2026-04-14 05:41:16 +08:00
|
|
|
|
For more details, run `android.py test --help`.
|
2024-09-24 00:09:53 +01:00
|
|
|
|
|
|
|
|
|
|
## Using in your own app
|
|
|
|
|
|
|
2025-04-01 01:46:29 +01:00
|
|
|
|
See https://docs.python.org/3/using/android.html.
|