自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
二進位模組
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 rust_binary
模組類型產生 Rust 二進位檔,例如:
rust_binary {
name: "hello_rust",
srcs: ["src/hello_rust.rs"],
}
同樣地,您也可以使用 rust_binary_host
提供僅限主機的模組。
值得注意的 rust_binary 屬性
除了重要共用屬性之外,下列屬性也相當重要,或會顯示 rust_binary
模組類型的特殊行為:
- static_executable:將此二進位檔建構為靜態二進位檔,並暗示
prefer_rlib
為 true
。您只能為 Bionic 目標產生完全靜態可執行檔。對於非 Bionic 目標,這只表示 prefer_rlib
是 true
,但仍會動態連結 libc
和 libdl
。
- prefer_rlib:變更
rustlibs
連結,讓系統預設為裝置目標選取 rlib
連結,並將 libstd
連結為裝置目標的 rlib
。請注意,這是主機目標的預設行為,不會對主機目標造成任何影響。
連結
根據預設,指定裝置的 rust_binary
模組一律會動態連結至 libstd
,且預設會偏好 rustlibs
中列出的依附元件的 dylib
程式庫,除非 prefer_rlib
為 true
。如果需要完全靜態的二進位檔,請參閱 static_executable
屬性。
主機模組一律會靜態連結至 libstd
,且預設會針對 rustlibs
中列出的依附元件,優先使用 rlib
程式庫。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Binary modules\n\nRust binaries can be produced using the `rust_binary` module type; for example: \n\n rust_binary {\n name: \"hello_rust\",\n srcs: [\"src/hello_rust.rs\"],\n }\n\nSimilarly, you can use `rust_binary_host` to provide a host-only\nmodule.\n\nNotable rust_binary properties\n------------------------------\n\nIn addition to the [Important common properties](/docs/setup/build/rust/building-rust-modules/android-rust-modules#important-common-properties),\nthe following properties are also either important, or exhibit unique behavior\nspecific to the `rust_binary` module type:\n\n- **static_executable** : Builds this binary as a static binary, and implies `prefer_rlib` is `true`. Fully static executables can only be produced for bionic targets. For non-bionic targets, this implies only that `prefer_rlib` is `true`, but it still links `libc` and `libdl` dynamically.\n- **prefer_rlib** : Changes the `rustlibs` linkage to select the `rlib` linkage by default for device targets, and links `libstd` as an `rlib` on device targets. Note that this is the default behavior for host targets, and has no impact on them.\n\nLinkage\n-------\n\nBy default, `rust_binary` modules that are targeting devices are always linked\ndynamically against `libstd` and by default will prefer `dylib` libraries for\ndependencies listed in `rustlibs` unless `prefer_rlib` is `true`. If fully\nstatic binaries are required, see the `static_executable` property.\n\nHost modules are always linked statically against `libstd` and\nby default prefer `rlib` libraries for dependencies that are listed in\n`rustlibs`."]]