Skip to content

feat: add automatic Android API Level detection via getprop#3153

Open
MartheusX wants to merge 1 commit intoPyO3:mainfrom
MartheusX:fix-android-api-detection
Open

feat: add automatic Android API Level detection via getprop#3153
MartheusX wants to merge 1 commit intoPyO3:mainfrom
MartheusX:fix-android-api-detection

Conversation

@MartheusX
Copy link
Copy Markdown

Description

Currently, maturin requires the ANDROID_API_LEVEL environment variable to be explicitly set when building on-device in environments like Termux. If missing, the build fails even though the host system is a live Android instance where this information is readily available.

This PR introduces a fallback mechanism:

  1. It checks if the current platform is Android.
  2. If ANDROID_API_LEVEL and other clues (linker/CC) are missing, it attempts to query getprop ro.build.version.sdk.
  3. This significantly improves the developer experience for users building Rust-based Python packages directly on Android devices.

Motivation

I encountered this issue while trying to install tools in Termux on my Redmi Note 8 Pro. Automating this step removes a manual barrier for many mobile developers.

Testing

  • Verified on Android 11 (API 30) within Termux.
  • Confirmed that manual environment variables still take priority over this fallback.

@messense
Copy link
Copy Markdown
Member

I don't believe this is strictly required, ANDROID_API_LEVEL is not required since 1.13.0 release as long as you have clang installed which is the case when you try to develop on Termux I think.

Though it does not hurt to include this, but please verify the claim.

@MartheusX
Copy link
Copy Markdown
Author

​"I have verified the claim on my another device (Xiaomi Poco F3, Android 13, API 33).
​As shown in the attached screenshot, clang -
Screenshot_2026-04-24-06-39-43-252_com termux
dumpmachine returns aarch64-unknown-linux-android24, but the actual system API level retrieved via getprop is 33.
​Relying solely on clang clues is inaccurate for on-device builds in Termux, as clang defaults to a much lower API level (24) than the host system actually supports. My proposed getprop fallback ensures that maturin correctly identifies the host's actual capabilities (API 33) when building natively.
​Without this fallback or a manual ANDROID_API_LEVEL export, the developer is forced to manually investigate and set the correct version, which goes against the goal of making maturin 'just work'."

@messense
Copy link
Copy Markdown
Member

So you are saying the lower API level 24 does not work on Termux, any errors? Could you also check the output of python3 -m pip debug --verbose on Termux, it should show the supported platform tags. Thanks!

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.

2 participants