#build-script #android #cargo-build #build #java #robius #java-class

build android-build

A build-time dependency to compile Java source files for Android as part of a Rust build. Use this from your build.rs Cargo build script

1 unstable release

0.1.0 May 22, 2024

#423 in Build Utils

Download history 11/week @ 2024-08-23 27/week @ 2024-08-30 9/week @ 2024-09-06 11/week @ 2024-09-13 15/week @ 2024-09-20 5/week @ 2024-09-27 11/week @ 2024-10-04 27/week @ 2024-10-11 11/week @ 2024-10-18 4/week @ 2024-10-25 21/week @ 2024-11-01 46/week @ 2024-11-08 60/week @ 2024-11-15 48/week @ 2024-11-22 45/week @ 2024-11-29 30/week @ 2024-12-06

216 downloads per month
Used in 8 crates (5 directly)

MIT license

36KB
516 lines

android-build

Latest Version Docs Project Robius Matrix Chat

Use this crate from your Cargo build.rs build script to compile Java source files and to run Java/Android commands as part of a Rust build, specifically designed for Android-targeted builds and Android tools.

This crate aims to behave similarly to cc-rs, but for Java (primarily on Android) instead of C/C++.

This crate is part of Project Robius and is primarily used by those crates.

Usage

Add this crate as a build dependency to your Cargo.toml:

[build-dependencies]
android-build = "0.1.0"

Then add this to your build.rs build script:

fn main() {
    let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
    if target_os == "android" {
        let output_dir = std::env::var("OUT_DIR").unwrap();
        let android_jar_path = android_build::android_jar(None)
            .expect("Failed to find android.jar");

        android_build::JavaBuild::new()
            .class_path(android_jar_path)
            .classes_out_dir(std::path::PathBuf::from(output_dir))
            .file("YourJavaFile.java")
            .compile()
            .expect("java build failed!");

        // `YourJavaFile.class` will be the Cargo-specified OUT_DIR.
    }
}

The above code will automatically run when you build your crate using cargo build.

Configuration via environment variables

The crate-level documentation provides a detailed list of environment variables that can be set to configure this crate.

Examples

Check out the robius-authentication build script to see how we use this crate for more complicated build procedures:

  • Discovering specific Android jarfiles and SDK directories (platforms, build tools, etc).
  • Compiling Java classes against the main android.jar jarfile.
  • Invoking Android's d8 DEXer tool.

Dependencies

~0–8MB
~61K SLoC