Skip to content

Commit bf452f7

Browse files
gh-146445: Migrate Android build tools to the Platforms folder. (#148282)
Migrates Android build tooling to the shared Platforms folder. Co-authored-by: Malcolm Smith <smith@chaquo.com>
1 parent a5b76d5 commit bf452f7

File tree

21 files changed

+61
-41
lines changed

21 files changed

+61
-41
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ jobs:
354354
with:
355355
persist-credentials: false
356356
- name: Build and test
357-
run: ./Android/android.py ci --fast-ci ${{ matrix.arch }}-linux-android
357+
run: python3 Platforms/Android ci --fast-ci ${{ matrix.arch }}-linux-android
358358

359359
build-ios:
360360
name: iOS
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The Android build tools have been moved to the Platforms folder.
Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Instead, use one of the tools listed
1111
[here](https://docs.python.org/3/using/android.html), which will provide a much
1212
easier experience.
1313

14-
1514
## Prerequisites
1615

1716
If you already have an Android SDK installed, export the `ANDROID_HOME`
@@ -25,15 +24,14 @@ it:
2524
`android-sdk/cmdline-tools/latest`.
2625
* `export ANDROID_HOME=/path/to/android-sdk`
2726

28-
The `android.py` script will automatically use the SDK's `sdkmanager` to install
27+
The `Platforms/Android` script will automatically use the SDK's `sdkmanager` to install
2928
any packages it needs.
3029

3130
The script also requires the following commands to be on the `PATH`:
3231

3332
* `curl`
3433
* `java` (or set the `JAVA_HOME` environment variable)
3534

36-
3735
## Building
3836

3937
Python can be built for Android on any POSIX platform supported by the Android
@@ -43,29 +41,28 @@ First we'll make a "build" Python (for your development machine), then use it to
4341
help produce a "host" Python for Android. So make sure you have all the usual
4442
tools and libraries needed to build Python for your development machine.
4543

46-
The easiest way to do a build is to use the `android.py` script. You can either
44+
The easiest way to do a build is to use the `Platforms/Android` script. You can either
4745
have it perform the entire build process from start to finish in one step, or
4846
you can do it in discrete steps that mirror running `configure` and `make` for
4947
each of the two builds of Python you end up producing.
5048

51-
The discrete steps for building via `android.py` are:
49+
The discrete steps for building via `Platforms/Android` are:
5250

5351
```sh
54-
./android.py configure-build
55-
./android.py make-build
56-
./android.py configure-host HOST
57-
./android.py make-host HOST
52+
python3 Platforms/Android configure-build
53+
python3 Platforms/Android make-build
54+
python3 Platforms/Android configure-host HOST
55+
python3 Platforms/Android make-host HOST
5856
```
5957

6058
`HOST` identifies which architecture to build. To see the possible values, run
61-
`./android.py configure-host --help`.
59+
`python3 Platforms/Android configure-host --help`.
6260

6361
To do all steps in a single command, run:
6462

6563
```sh
66-
./android.py build HOST
64+
python3 Platforms/Android build HOST
6765
```
68-
6966
In the end you should have a build Python in `cross-build/build`, and a host
7067
Python in `cross-build/HOST`.
7168

@@ -75,43 +72,33 @@ call. For example, if you want a pydebug build that also caches the results from
7572
`configure`, you can do:
7673

7774
```sh
78-
./android.py build HOST -- -C --with-pydebug
75+
python3 Platforms/Android build HOST -- -C --with-pydebug
7976
```
8077

81-
8278
## Packaging
8379

8480
After building an architecture as described in the section above, you can
8581
package it for release with this command:
8682

8783
```sh
88-
./android.py package HOST
84+
python3 Platforms/Android package HOST
8985
```
9086

9187
`HOST` is defined in the section above.
9288

9389
This will generate a tarball in `cross-build/HOST/dist`, whose structure is
9490
similar to the `Android` directory of the CPython source tree.
9591

96-
9792
## Testing
9893

99-
The Python test suite can be run on Linux, macOS, or Windows.
94+
Tests can be run on Linux, macOS, or Windows, using either an Android emulator
95+
or a physical device.
10096

10197
On Linux, the emulator needs access to the KVM virtualization interface. This may
10298
require adding your user to a group, or changing your udev rules. On GitHub
10399
Actions, the test script will do this automatically using the commands shown
104100
[here](https://github.blog/changelog/2024-04-02-github-actions-hardware-accelerated-android-virtualization-now-available/).
105101

106-
You can run the test suite either:
107-
108-
* Within the CPython repository, after doing a build as described above. On
109-
Windows, you won't be able to do the build on the same machine, so you'll have
110-
to copy the `cross-build/HOST/prefix` directory from somewhere else.
111-
112-
* Or by taking a release package built using the `package` command, extracting
113-
it wherever you want, and using its own copy of `android.py`.
114-
115102
The test script supports the following modes:
116103

117104
* In `--connected` mode, it runs on a device or emulator you have already
@@ -120,7 +107,7 @@ The test script supports the following modes:
120107
script like this:
121108

122109
```sh
123-
./android.py test --connected emulator-5554
110+
python3 Platforms/Android test --connected emulator-5554
124111
```
125112

126113
* In `--managed` mode, it uses a temporary headless emulator defined in the
@@ -131,29 +118,55 @@ The test script supports the following modes:
131118
to our minimum and maximum supported Android versions. For example:
132119

133120
```sh
134-
./android.py test --managed maxVersion
121+
python3 Platforms/Android test --managed maxVersion
135122
```
136123

137124
By default, the only messages the script will show are Python's own stdout and
138125
stderr. Add the `-v` option to also show Gradle output, and non-Python logcat
139126
messages.
140127

141-
Any other arguments on the `android.py test` command line will be passed through
142-
to `python -m test` – use `--` to separate them from android.py's own options.
128+
### Testing Python
129+
130+
You can run the test suite by doing a build as described above, and then running
131+
`python3 Platforms/Android test`. On Windows, you won't be able to do the build
132+
on the same machine, so you'll have to copy the `cross-build/HOST/prefix` directory
133+
from somewhere else.
134+
135+
Extra arguments on the `Platforms/Android test` command line will be passed through
136+
to `python -m test` – use `--` to separate them from `Platforms/Android`'s own options.
143137
See the [Python Developer's
144138
Guide](https://devguide.python.org/testing/run-write-tests/) for common options
145139
– most of them will work on Android, except for those that involve subprocesses,
146140
such as `-j`.
147141

148-
Every time you run `android.py test`, changes in pure-Python files in the
142+
Every time you run `python3 Platforms/Android test`, changes in pure-Python files in the
149143
repository's `Lib` directory will be picked up immediately. Changes in C files,
150144
and architecture-specific files such as sysconfigdata, will not take effect
151-
until you re-run `android.py make-host` or `build`.
145+
until you re-run `python3 Platforms/Android make-host` or `build`.
146+
147+
### Testing a third-party package
148+
149+
The `Platforms/Android` script is also included as `android.py` in the root of a
150+
release package (i.e., the one built using `Platforms/Android package`).
151+
152+
You can use this script to test third-party packages by taking a release
153+
package, extracting it wherever you want, and using the `android.py` script to
154+
run the test suite for your third-party package.
155+
156+
Any argument that can be passed to `python3 Platforms/Android test` can also be
157+
passed to `android.py`. The following options will be of particular use when
158+
configuring the execution of a third-party test suite:
159+
160+
* `--cwd`: the directory of content to copy into the testbed app as the working
161+
directory.
162+
* `--site-packages`: the directory to copy into the testbed app to use as site
163+
packages.
152164

153-
The testbed app can also be used to test third-party packages. For more details,
154-
run `android.py test --help`, paying attention to the options `--site-packages`,
155-
`--cwd`, `-c` and `-m`.
165+
Extra arguments on the `android.py test` command line will be passed through to
166+
Python – use `--` to separate them from `android.py`'s own options. You must include
167+
either a `-c` or `-m` argument to specify how the test suite should be started.
156168

169+
For more details, run `android.py test --help`.
157170

158171
## Using in your own app
159172

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424

2525

2626
SCRIPT_NAME = Path(__file__).name
27+
if SCRIPT_NAME.startswith("__"):
28+
SCRIPT_NAME = "Platforms/Android"
29+
2730
ANDROID_DIR = Path(__file__).resolve().parent
28-
PYTHON_DIR = ANDROID_DIR.parent
31+
PYTHON_DIR = ANDROID_DIR.parent.parent
2932
in_source_tree = (
3033
ANDROID_DIR.name == "Android" and (PYTHON_DIR / "pyconfig.h.in").exists()
3134
)
@@ -756,7 +759,7 @@ def package(context):
756759
prefix_dir = subdir(context.host, "prefix")
757760
version = package_version(prefix_dir)
758761

759-
with TemporaryDirectory(prefix=SCRIPT_NAME) as temp_dir:
762+
with TemporaryDirectory(prefix=SCRIPT_NAME.replace("/", "-")) as temp_dir:
760763
temp_dir = Path(temp_dir)
761764

762765
# Include all tracked files from the Android directory.
@@ -765,7 +768,10 @@ def package(context):
765768
cwd=ANDROID_DIR, capture_output=True, text=True, log=False,
766769
).stdout.splitlines():
767770
src = ANDROID_DIR / line
768-
dst = temp_dir / line
771+
# "__main__.py" is renamed "android.py" for distribution purpose
772+
dst = temp_dir / {
773+
"__main__.py": "android.py"
774+
}.get(line, line)
769775
dst.parent.mkdir(parents=True, exist_ok=True)
770776
shutil.copy2(src, dst, follow_symlinks=False)
771777

@@ -831,7 +837,7 @@ def ci(context):
831837
"emulator on this platform."
832838
)
833839
else:
834-
with TemporaryDirectory(prefix=SCRIPT_NAME) as temp_dir:
840+
with TemporaryDirectory(prefix=SCRIPT_NAME.replace("/", "-")) as temp_dir:
835841
print("::group::Tests")
836842

837843
# Prove the package is self-contained by using it to run the tests.

Android/testbed/.idea/inspectionProfiles/Project_Default.xml renamed to Platforms/Android/testbed/.idea/inspectionProfiles/Project_Default.xml

File renamed without changes.

Android/testbed/app/build.gradle.kts renamed to Platforms/Android/testbed/app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
val ANDROID_DIR = file("../..")
10-
val PYTHON_DIR = ANDROID_DIR.parentFile!!
10+
val PYTHON_DIR = ANDROID_DIR.parentFile.parentFile!!
1111
val PYTHON_CROSS_DIR = file(System.getenv("CROSS_BUILD_DIR") ?: "$PYTHON_DIR/cross-build")
1212
val inSourceTree = (
1313
ANDROID_DIR.name == "Android" && file("$PYTHON_DIR/pyconfig.h.in").exists()

Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt renamed to Platforms/Android/testbed/app/src/androidTest/java/org/python/testbed/PythonSuite.kt

File renamed without changes.

0 commit comments

Comments
 (0)