自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
Hello Rust 範例
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
以下是建構依附 Rust 程式庫的 Rust 二進位檔的範例。
Rust 模組目前僅限於 build/soong/rust/config/allowed_list.go 中定義的特定目錄。下列 helloWorld
模組會使用 external/rust
目錄,避免修改這份清單。
從 Android 開放原始碼計畫來源根目錄
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
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Hello Rust example\n\nThe following is an example of constructing a Rust binary that depends on\na Rust library.\n\nRust modules are currently confined to specific directories defined in\n[build/soong/rust/config/allowed_list.go](https://android.googlesource.com/platform/build/soong/+/41461f36604948ef72855d56093d8b3e45f1d19c/rust/config/allowed_list.go).\nThe following `helloWorld` module uses the `external/rust` directory to avoid\nmodifying this list.\n\nFrom the AOSP source root \n\n mkdir -p external/rust/hello_rust/src/\n mkdir -p external/rust/libsimple_printer/src/\n\nCreate `external/rust/hello_rust/src/hello_rust.rs` with the following contents: \n\n use simple_printer;\n fn main() {\n simple_printer::print_hello_rust();\n }\n\nCreate `external/rust/libsimple_printer/src/lib.rs` file with the following contents: \n\n pub fn print_hello_rust() {\n println!(\"Hello Rust!\");\n }\n\nCreate an `external/rust/hello_rust/Android.bp` file with the following contents: \n\n rust_binary {\n name: \"hello_rust\",\n\n // srcs must contain a single source file, the entry point source.\n srcs: [\"src/hello_rust.rs\"],\n\n // rustlibs are Rust library dependencies. The type, rlib (static) or dylib\n // (dynamic), are chosen automatically based on module type and target\n // to avoid linkage issues.\n rustlibs: [\"libsimple_printer\"],\n\n // define any additional compilation flags\n flags: [\n \"-C debug-assertions=yes\",\n ],\n\n // cc libraries can be linked in to rust binaries and libraries through the\n // shared_libs and static_libs properties. These are not needed for this\n // simple example, so they are not included.\n }\n\nCreate an `external/rust/libsimple_printer/Android.bp` file with the following contents: \n\n // rust_library provides dylib and rlib variants.\n rust_library {\n //name or stem properties must be of the form lib\u003ccrate_name\u003e\u003cany_suffix\u003e\n name: \"libsimple_printer\",\n\n //crate_name must match the name used in source (e.g. extern crate \u003cname\u003e)\n crate_name: \"simple_printer\",\n\n //srcs must contain a single source file, the entry point source\n srcs: [\"src/lib.rs\"],\n }\n\nFinally, build your `hello_rust` module: \n\n source build/envsetup.sh\n lunch aosp_arm64-eng\n m hello_rust"]]