Rust 二进制文件

让我们从一个简单的应用程序开始。在 AOSP 签出的根目录下,创建以下文件:

hello_rust/Android.bp:

rust_binary {
    name: "hello_rust",
    crate_name: "hello_rust",
    srcs: ["src/main.rs"],
}

hello_rust/src/main.rs:

//! Rust demo.

/// Prints a greeting to standard output.
fn main() {
    println!("Hello from Rust!");
}

你现在可以构建、推送和运行二进制文件:

m hello_rust
adb push "$ANDROID_PRODUCT_OUT/system/bin/hello_rust" /data/local/tmp
adb shell /data/local/tmp/hello_rust
Hello from Rust!
  • Go through the build steps and demonstrate them running in your emulator.

  • Notice the extensive documentation comments? The Android build rules enforce that all modules have documentation. Try removing it and see what error you get.

  • Stress that the Rust build rules look like the other Soong rules. This is on purpose to make it as easy to use Rust as C++ or Java.