Skip to content

Commit f5ff59f

Browse files
committed
Add ability to build on Android (with opt-in)
1 parent 79a77fe commit f5ff59f

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This version of `cargo-ndk` is much closer to a thin passthrough to `cargo` than
1111
- **Breaking change**: Removed Cargo.toml-based configuration property handling
1212
- **Breaking change**: Bump minimum supported Rust version (MSRV) to 1.86 and use Rust edition 2024.
1313
- **Breaking change**: `--bindgen` has been removed, and relevant environment variables are set by default
14+
- Enhancement: Can now be built for an Android host (i.e. Termux) if built with `CARGO_NDK_ON_ANDROID` environment variable set
1415
- Enhancement: CLI flags can now be used in any order (e.g., `cargo ndk -t x86 build` and `cargo ndk build --target x86` are equivalent).
1516
- Enhancement: Added environment variable support for configuration flags with `CARGO_NDK_` prefix (e.g., `CARGO_NDK_TARGET`, `CARGO_NDK_PLATFORM`, `CARGO_NDK_OUTPUT_DIR`)
1617
- Enhancement: Target flag now supports comma-delimited lists for specifying multiple targets

build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
println!("cargo:rerun-if-env-changed=CARGO_NDK_ON_ANDROID");
3+
4+
if std::env::var("CARGO_NDK_ON_ANDROID").is_ok() {
5+
println!("cargo:rustc-cfg=cargo_ndk_on_android");
6+
}
7+
8+
println!("cargo:rustc-check-cfg=cfg(cargo_ndk_on_android)");
9+
}

src/cargo.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ use crate::shell::Shell;
1414

1515
#[cfg(target_os = "macos")]
1616
const ARCH: &str = "darwin-x86_64";
17-
#[cfg(target_os = "linux")]
17+
#[cfg(any(target_os = "linux", target_os = "android"))]
1818
const ARCH: &str = "linux-x86_64";
1919
#[cfg(target_os = "windows")]
2020
const ARCH: &str = "windows-x86_64";
2121

22-
#[cfg(target_os = "android")]
22+
#[cfg(all(target_os = "android", not(cargo_ndk_on_android)))]
2323
compile_error!(
24-
"You cannot build cargo-ndk _for_ Android. Build it for your host OS and run it with cargo."
24+
r#"
25+
Building cargo-ndk on Android is not supported. This binary is intended to be run on your host OS.
26+
27+
Set CARGO_NDK_ON_ANDROID to override this check (for example, building for Termux)."
28+
"#
2529
);
2630

2731
#[cfg(not(any(
@@ -31,8 +35,6 @@ compile_error!(
3135
target_os = "windows"
3236
)))]
3337
compile_error!("Unsupported target OS");
34-
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
35-
const ARCH: &str = "unknown";
3638

3739
pub(crate) fn clang_target(rust_target: &str, api_level: u8) -> String {
3840
let target = match rust_target {

0 commit comments

Comments
 (0)