|
| 1 | +# Python for Android |
| 2 | + |
| 3 | +These instructions are only needed if you're planning to compile Python for |
| 4 | +Android yourself. Most users should *not* need to do this. Instead, use one of |
| 5 | +the tools listed in `Doc/using/android.rst`, which will provide a much easier |
| 6 | +experience. |
| 7 | + |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +First, make sure you have all the usual tools and libraries needed to build |
| 12 | +Python for your development machine. |
| 13 | + |
| 14 | +Second, you'll need an Android SDK. If you already have the SDK installed, |
| 15 | +export the `ANDROID_HOME` environment variable to point at its location. |
| 16 | +Otherwise, 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 | + |
| 25 | +The `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 | + |
| 35 | +Python can be built for Android on any POSIX platform supported by the Android |
| 36 | +development tools, which currently means Linux or macOS. This involves doing a |
| 37 | +cross-build where you use a "build" Python (for your development machine) to |
| 38 | +help produce a "host" Python for Android. |
| 39 | + |
| 40 | +The easiest way to do a build is to use the `android.py` script. You can either |
| 41 | +have it perform the entire build process from start to finish in one step, or |
| 42 | +you can do it in discrete steps that mirror running `configure` and `make` for |
| 43 | +each of the two builds of Python you end up producing. |
| 44 | + |
| 45 | +The 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 | + |
| 57 | +To do all steps in a single command, run: |
| 58 | + |
| 59 | +```sh |
| 60 | +./android.py build HOST |
| 61 | +``` |
| 62 | + |
| 63 | +In the end you should have a build Python in `cross-build/build`, and an Android |
| 64 | +build in `cross-build/HOST`. |
| 65 | + |
| 66 | +You can use `--` as a separator for any of the `configure`-related commands – |
| 67 | +including `build` itself – to pass arguments to the underlying `configure` |
| 68 | +call. 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 | + |
| 78 | +The 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 | + |
| 85 | +The test suite can usually be run on a device with 2 GB of RAM, but this is |
| 86 | +borderline, so you may need to increase it to 4 GB. As of Android |
| 87 | +Studio Koala, 2 GB is the default for all emulators, although the user interface |
| 88 | +may indicate otherwise. Locate the emulator's directory under `~/.android/avd`, |
| 89 | +and find `hw.ramSize` in both config.ini and hardware-qemu.ini. Either set these |
| 90 | +manually to the same value, or use the Android Studio Device Manager, which will |
| 91 | +update both files. |
| 92 | + |
| 93 | +Before running the test suite, follow the instructions in the previous section |
| 94 | +to build the architecture you want to test. Then run the test script in one of |
| 95 | +the 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 | + |
| 117 | +By default, the only messages the script will show are Python's own stdout and |
| 118 | +stderr. Add the `-v` option to also show Gradle output, and non-Python logcat |
| 119 | +messages. |
| 120 | + |
| 121 | +Any other arguments on the `android.py test` command line will be passed through |
| 122 | +to `python -m test` – use `--` to separate them from android.py's own options. |
| 123 | +See the [Python Developer's |
| 124 | +Guide](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, |
| 126 | +such as `-j`. |
| 127 | + |
| 128 | +Every time you run `android.py test`, changes in pure-Python files in the |
| 129 | +repository's `Lib` directory will be picked up immediately. Changes in C files, |
| 130 | +and architecture-specific files such as sysconfigdata, will not take effect |
| 131 | +until you re-run `android.py make-host` or `build`. |
| 132 | + |
| 133 | + |
| 134 | +## Using in your own app |
| 135 | + |
| 136 | +See `Doc/using/android.rst`. |
0 commit comments