สวัสดีตัวอย่างสนิม

ต่อไปนี้เป็นตัวอย่างของการสร้างไบนารีของ Rust ที่ขึ้นอยู่กับไลบรารี่ของ Rust

ปัจจุบันโมดูล Rust ถูกจำกัดอยู่ในไดเร็กทอรีเฉพาะที่กำหนดไว้ใน build/soong/rust/config/allowed_list.go โมดูล helloWorld ต่อไปนี้ใช้ไดเร็กทอรี external/rust เพื่อหลีกเลี่ยงการแก้ไขรายการนี้

จากรูทต้นทาง AOSP

mkdir -p external/rust/hello_rust/src/
mkdir -p external/rust/libsimple_printer/src/

สร้าง external/rust/hello_rust/src/hello_rust.rs ด้วยเนื้อหาต่อไปนี้:

use simple_printer;
fn main() {
 simple_printer::print_hello_rust();
}

สร้างไฟล์ external/rust/libsimple_printer/src/lib.rs โดยมีเนื้อหาดังต่อไปนี้:

pub fn print_hello_rust() {
  println!("Hello Rust!");
}

สร้างไฟล์ external/rust/hello_rust/Android.bp โดยมีเนื้อหาดังต่อไปนี้:

rust_binary {
    name: "hello_rust",

    // srcs must contain a single source file, the entry point source.
    srcs: ["src/hello_rust.rs"],

    // rustlibs are Rust library dependencies. The type, rlib (static) or dylib
    // (dynamic), are chosen automatically based on module type and target
    // to avoid linkage issues.
    rustlibs: ["libsimple_printer"],

    // define any additional compilation flags
    flags: [
        "-C debug-assertions=yes",
    ],

    // cc libraries can be linked in to rust binaries and libraries through the
    // shared_libs and static_libs properties. These are not needed for this
    // simple example, so they are not included.
}

สร้างไฟล์ external/rust/libsimple_printer/Android.bp โดยมีเนื้อหาดังต่อไปนี้:

// rust_library provides dylib and rlib variants.
rust_library {
    //name or stem properties must be of the form lib<crate_name><any_suffix>
    name: "libsimple_printer",

    //crate_name must match the name used in source (e.g. extern crate <name>)
    crate_name: "simple_printer",

    //srcs must contain a single source file, the entry point source
    srcs: ["src/lib.rs"],
}

สุดท้าย สร้างโมดูล hello_rust ของคุณ:

source build/envsetup.sh
lunch aosp_arm64-eng
m hello_rust