README.md
1# Python for Android
2
3These instructions are only needed if you're planning to compile Python for
4Android yourself. Most users should *not* need to do this. Instead, use one of
5the tools listed in `Doc/using/android.rst`, which will provide a much easier
6experience.
7
8
9## Prerequisites
10
11First, make sure you have all the usual tools and libraries needed to build
12Python for your development machine.
13
14Second, you'll need an Android SDK. If you already have the SDK installed,
15export the `ANDROID_HOME` environment variable to point at its location.
16Otherwise, here's how to install it:
17
18* Download the "Command line tools" from <https://developer.android.com/studio>.
19* Create a directory `android-sdk/cmdline-tools`, and unzip the command line
20 tools package into it.
21* Rename `android-sdk/cmdline-tools/cmdline-tools` to
22 `android-sdk/cmdline-tools/latest`.
23* `export ANDROID_HOME=/path/to/android-sdk`
24
25The `android.py` script also requires the following commands to be on the `PATH`:
26
27* `curl`
28* `java` (or set the `JAVA_HOME` environment variable)
29* `tar`
30* `unzip`
31
32
33## Building
34
35Python can be built for Android on any POSIX platform supported by the Android
36development tools, which currently means Linux or macOS. This involves doing a
37cross-build where you use a "build" Python (for your development machine) to
38help produce a "host" Python for Android.
39
40The easiest way to do a build is to use the `android.py` script. You can either
41have it perform the entire build process from start to finish in one step, or
42you can do it in discrete steps that mirror running `configure` and `make` for
43each of the two builds of Python you end up producing.
44
45The discrete steps for building via `android.py` are:
46
47```sh
48./android.py configure-build
49./android.py make-build
50./android.py configure-host HOST
51./android.py make-host HOST
52```
53
54`HOST` identifies which architecture to build. To see the possible values, run
55`./android.py configure-host --help`.
56
57To do all steps in a single command, run:
58
59```sh
60./android.py build HOST
61```
62
63In the end you should have a build Python in `cross-build/build`, and an Android
64build in `cross-build/HOST`.
65
66You can use `--` as a separator for any of the `configure`-related commands –
67including `build` itself – to pass arguments to the underlying `configure`
68call. For example, if you want a pydebug build that also caches the results from
69`configure`, you can do:
70
71```sh
72./android.py build HOST -- -C --with-pydebug
73```
74
75
76## Testing
77
78The test suite can be run on Linux, macOS, or Windows:
79
80* On Linux, the emulator needs access to the KVM virtualization interface, and
81 a DISPLAY environment variable pointing at an X server.
82* On Windows, you won't be able to do the build on the same machine, so you'll
83 have to copy the `cross-build/HOST` directory from somewhere else.
84
85The test suite can usually be run on a device with 2 GB of RAM, but this is
86borderline, so you may need to increase it to 4 GB. As of Android
87Studio Koala, 2 GB is the default for all emulators, although the user interface
88may indicate otherwise. Locate the emulator's directory under `~/.android/avd`,
89and find `hw.ramSize` in both config.ini and hardware-qemu.ini. Either set these
90manually to the same value, or use the Android Studio Device Manager, which will
91update both files.
92
93Before running the test suite, follow the instructions in the previous section
94to build the architecture you want to test. Then run the test script in one of
95the following modes:
96
97* In `--connected` mode, it runs on a device or emulator you have already
98 connected to the build machine. List the available devices with
99 `$ANDROID_HOME/platform-tools/adb devices -l`, then pass a device ID to the
100 script like this:
101
102 ```sh
103 ./android.py test --connected emulator-5554
104 ```
105
106* In `--managed` mode, it uses a temporary headless emulator defined in the
107 `managedDevices` section of testbed/app/build.gradle.kts. This mode is slower,
108 but more reproducible.
109
110 We currently define two devices: `minVersion` and `maxVersion`, corresponding
111 to our minimum and maximum supported Android versions. For example:
112
113 ```sh
114 ./android.py test --managed maxVersion
115 ```
116
117By default, the only messages the script will show are Python's own stdout and
118stderr. Add the `-v` option to also show Gradle output, and non-Python logcat
119messages.
120
121Any other arguments on the `android.py test` command line will be passed through
122to `python -m test` – use `--` to separate them from android.py's own options.
123See the [Python Developer's
124Guide](https://devguide.python.org/testing/run-write-tests/) for common options
125– most of them will work on Android, except for those that involve subprocesses,
126such as `-j`.
127
128Every time you run `android.py test`, changes in pure-Python files in the
129repository's `Lib` directory will be picked up immediately. Changes in C files,
130and architecture-specific files such as sysconfigdata, will not take effect
131until you re-run `android.py make-host` or `build`.
132
133
134## Using in your own app
135
136See `Doc/using/android.rst`.
137