Skip to content

Update environment variables for Android cross-compilation support#2825

Merged
messense merged 4 commits intoPyO3:mainfrom
ririv:android-support
Nov 19, 2025
Merged

Update environment variables for Android cross-compilation support#2825
messense merged 4 commits intoPyO3:mainfrom
ririv:android-support

Conversation

@ririv
Copy link
Copy Markdown
Contributor

@ririv ririv commented Nov 5, 2025

Fix #2810
Enhancement #2803

In fact, to successfully compile Python extension modules written with PYO3 on Android,

we must not only set PYO3_CROSS_LIB_DIR (refer to PEP738),

but also configure environment variables for the Android NDK and related utilities like rustup and cargo.

  • rustup target add aarch64-linux-android
  • For Cargo, configure the linker variable to point to the Android NDK's clang.
  • Additional environment variables like cc may also need configuration.

However, this is not an inherent issue with Maturin itself. These environment variables can be configured externally, and Cargo within Maturin will read them.

This fix ensures that after properly configuring the relevant environment variables, we can successfully cross-compile to an Android-targeted wheel.

Comment thread src/compile.rs Outdated
Comment thread src/python_interpreter/config.rs Outdated
ririv and others added 3 commits November 14, 2025 16:04
…_dir is already specified in the generated pyo3 config;

Co-authored-by: messense <messense@icloud.com>
Co-authored-by: messense <messense@icloud.com>
@messense messense merged commit f156350 into PyO3:main Nov 19, 2025
45 checks passed
@ririv ririv deleted the android-support branch November 29, 2025 03:24
messense pushed a commit that referenced this pull request Dec 30, 2025
## Motivation
Enhancement #2803

#2825 fixed a bug so that Maturin
can now compile a wheel for Android.

However, the generated wheel's tag name is incorrect. This is because
Maturin lacks support for Android tags and treats Android as Linux,
generating a Linux tag.

Python's Android wheel tags are strictly defined. See
[PEP738](https://peps.python.org/pep-0738/#packaging)


This is Log.
https://github.com/ririv/android-wheels/actions/runs/20456436296/job/58779454736

At the end of the wheel building process, we can see:
`Successfully built XXXXX-linux_aarch64.whl`

It must be renamed manually or by setting the `_PYTHON_HOST_PLATFORM
`environment variable to obtain the correct tag name.


## Changes
- **Android Platform Tag Support**: Implemented the
`android_{api}_{arch}` platform tag generation logic within the Linux
match arm of `get_platform_tag` in `src/build_context.rs`.
- **Automatic API Level Detection**:
    - Implemented a prioritized search for the Android API level:
        1.  `ANDROID_API_LEVEL` environment variable.
2. `linker` path resolution via `cargo-config2` (handling
`.cargo/config.toml` and `CARGO_TARGET_<TRIPLE>_LINKER`).
        3.  `CC_<TARGET>` and `CC` environment variables.
- Utilizes regex `android(\d+)` to accurately extract the version number
from toolchain paths.
    - Explicit error reporting if no API level can be determined.
- **Strict Architecture Mapping**: 
- Maps Rust target architectures to standard Android ABIs:
`armeabi_v7a`, `arm64_v8a`, `x86`, and `x86_64`.
- Return a `bail!` error to prevent incorrect tag generation for
unsupported architectures.
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request Jan 8, 2026
https://build.opensuse.org/request/show/1325799
by user mia + anag_factory
- Update to version 1.11.2 (version bump only)
- Changes in 1.11.1:
  * Fix compiled artifacts being excluded by source path matching
    gh#PyO3/maturin#2910
  * Better error reporting for missing interpreters
    gh#PyO3/maturin#2918
  * Ignore unreadable excluded directories
    gh#PyO3/maturin#2916
- Changes in 1.11.0:
  * Correct tagging for x86_64 iOS simulator wheels.
    gh#PyO3/maturin#2851
  * Bump MSRV to 1.85.0 and use Rust 2024 edition
    gh#PyO3/maturin#2850
  * Upgrade goblin to 0.10
    gh#PyO3/maturin#2853
  * Set entry type when adding to the tar file
    gh#PyO3/maturin#2859
  * Split up module_writer.rs code for code organization
    gh#PyO3/maturin#2857
  * Update environment variables for Android cross-compilation
    support
    gh#PyO3/maturin#2825
  * Upgrade some
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cross compling error for Android

2 participants