自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
程式庫模組
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Rust 程式庫模組有兩種類型,一種會產生 Rust 程式庫,另一種會產生與 C 相容的程式庫。此外,為了建構系統,您可以將 Rust 程序巨集 (proc-macros
) 視為特殊類型的程式庫。
rust_library
rust_library
模組會產生 Rust 程式庫,供其他 Rust 模組使用。
除了 _host
變體之外,Rust 程式庫還有可控制可用連結的模組類型。
Rust 程式庫模組類型 |
定義 |
rust_library |
提供 rlib 和 dylib 這兩種程式庫變化版本。AOSP 建議將此模組類型用於 Rust 程式庫,因為這樣一來,當模組列為 rustlibs 屬性下的依附元件時,便可正常運作 |
rust_library_rlib |
只提供 Rust 程式庫的 rlib 變數;只提供 rlib 變數的模組無法保證可與 rustlibs 屬性搭配運作。 |
rust_library_dylib |
只提供 Rust 程式庫的 dylib 變數;只提供 dylib 變數的模組無法保證可搭配 rustlibs 屬性運作。 |
rust_ffi
rust_ffi
模組會產生與 C 相容的程式庫,以便與 CC 模組互通。
除了 _host
變化版本之外,Rust FFI 程式庫也有控制可用連結的模組類型,如下表所示。
Rust FFI 程式庫模組類型 |
定義 |
rust_ffi |
提供靜態和共用的 C 程式庫變化版本。 |
rust_ffi_shared |
僅提供 C 共用程式庫變化版本。 |
rust_ffi_static |
僅提供 C 靜態資料庫變化版本。 |
如需使用 rust_ffi
從 C 呼叫 Rust 的基本範例,請參閱 Android Rust 模式頁面。
如要進一步瞭解進階用法,請參閱 官方 Rust 說明文件。
rust_proc_macro
Rust 程序巨集 (proc-macros) 可用於擴充編譯器,以便執行原始碼轉換或提供新的屬性。如需更多資訊,請前往官方 Rust 說明文件的「程序式巨集」頁面。
就建構系統而言,rust_proc_macro
模組的運作方式與 rust_library
模組類似。對於依附 rust_proc_macros
的模組,請將模組名稱新增至 proc_macros
屬性。
由於 proc_macros
是編譯器外掛程式,因此必須指定主機,且不會產生任何可在裝置上執行的程式碼。
值得注意的 Rust 程式庫屬性
除了適用於所有模組的重要通用屬性,下表中定義的屬性也是如此。這些方法對 Rust 程式庫模組特別重要,或會顯示 rust_library
模組類型的特殊行為。
Rust 程式庫屬性 |
定義 |
stem / name |
stem 屬性會控制輸出程式庫檔案名稱,否則預設為 name 。
Rust 編譯器會對程式庫檔案名稱設下特定規定,因此建構系統會強制執行這些規定,以避免編譯問題。輸出檔案名稱必須符合 lib<crate_name><suffix> 格式。(此處有依附於 crate_name 屬性的依附元件)。 |
crate_name |
這是程式庫產生模組的必要屬性,且與輸出檔案名稱有關係。(請參閱 stem 定義)。 |
export_include_dirs |
對於 rust_ffi 模組,這項屬性會定義代表相對包含路徑的字串清單:包含依附 cc 模組可使用的標頭的路徑。 |
rust_library 或 rust_ffi 的連結
根據預設,針對裝置的 Rust 程式庫模組一律會與 libstd
動態連結。不過,主機模組一律會靜態連結至 libstd
。
rustlibs
依附元件使用的連結,取決於根模組的連結偏好設定。(例如,具有 prefer_rlib: true
的 rust_binary
會使用將 rustlibs
連結為 rlibs
的 Rust 程式庫變化版本)。
為了讓您能夠產生不依賴任何動態 Rust 程式庫 (例如靜態可執行檔) 的根依附元件模組,rlibs
提供動態和靜態 libstd
連結變化版本。Song 會自動連結正確的變化版本。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# Library modules\n\nThere are two types of Rust library modules, one that produces Rust libraries and\none that produces C-compatible libraries. Additionally, for build system\npurposes you can consider Rust procedural macros (`proc-macros`) as a special\ntype of library.\n\n### rust_library\n\nThe `rust_library` module produces Rust libraries for use by other Rust modules.\n\nIn addition to the `_host` variants, Rust libraries have module types that control\nthe available linkage.\n\n| Rust Library Module Type | Definition |\n|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **rust_library** | Provides both library variants, `rlib` and `dylib`. AOSP recommends this module type for Rust libraries, as it allows modules to work correctly when listed as a dependency under the `rustlibs` property |\n| **rust_library_rlib** | Provides only the `rlib` variant of a Rust library; modules providing only `rlib` variants can't be guaranteed to work with the `rustlibs` property. |\n| **rust_library_dylib** | Provides only the `dylib` variant of a Rust library; modules providing only `dylib` variants can't be guaranteed to work with the `rustlibs` property. |\n\n### rust_ffi\n\nThe `rust_ffi` module produces C-compatible libraries to interop with CC modules.\n\nIn addition to the `_host` variants, Rust FFI libraries have module types that\ncontrol the available linkage, shown in the following table.\n\n| Rust FFI Library Module Type | Definition |\n|------------------------------|------------------------------------------------------|\n| **rust_ffi** | Provides both C library variants: static and shared. |\n| **rust_ffi_shared** | Provides only the C shared library variant. |\n| **rust_ffi_static** | Provides only the C static library variant. |\n\nFor a basic example of using `rust_ffi` for calling Rust from C, see the\n[Android Rust Patterns](/docs/setup/build/rust/building-rust-modules/android-rust-patterns) page.\n\nFor information on more advanced usage, visit the [official Rust documentation](https://doc.rust-lang.org/nomicon/ffi.html#calling-rust-code-from-c).\n\n### rust_proc_macro\n\nRust procedural macros (proc-macros) can be useful for extending the compiler to\nperform source code transformations or providing new attributes. More information\ncan be found on these in the [Procedural Macros](https://doc.rust-lang.org/reference/procedural-macros.html)\npage of the official Rust documentation.\n\nFor the purposes of the build system, the `rust_proc_macro` module works similarly\nto the `rust_library` modules. For modules that depend on `rust_proc_macros`,\nadd the module name to the `proc_macros` property.\n\nAs `proc_macros` are compiler plugins, they necessarily target the host and don't\nproduce any code that would run on a device.\n\nNotable Rust library properties\n-------------------------------\n\nThe properties defined in the table below are in addition to the [Important common properties](/docs/setup/build/rust/building-rust-modules/android-rust-modules#important-common-properties)\nthat apply to all modules. These are either particularly important to Rust\nlibrary modules, or exhibit unique behavior specific to the `rust_library` module type.\n\n| Rust Library Properties | Definition |\n|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **stem / name** | The `stem` property controls the output library filename, which otherwise defaults to `name`. The Rust compiler imposes certain requirements on library filenames, and as a result the build system enforces these requirements to avoid compilation issues. The output filename must conform to the format `lib\u003ccrate_name\u003e\u003csuffix\u003e`. (There's a dependency here on the [`crate_name`](/docs/setup/build/rust/building-rust-modules/android-rust-modules#crate-name) property). |\n| **crate_name** | This is a required property for library-producing modules; it additionally has a relationship to the output filename. (See the [`stem`](/docs/setup/build/rust/building-rust-modules/android-rust-modules#stem) definition.) |\n| **export_include_dirs** | For `rust_ffi` modules, this property defines a list of strings representing relative include paths: paths which contain headers that dependent `cc` modules can use. |\n\nLinkage of rust_library or rust_ffi\n-----------------------------------\n\nBy default, Rust library modules targeting devices are always linked dynamically\nagainst `libstd`. Host modules, however, are always linked statically\nagainst `libstd`.\n\nThe linkage used for `rustlibs` dependencies depends on the root module's\nlinkage preferences. (For example, a `rust_binary` with `prefer_rlib: true` will\nuse Rust library variants that link `rustlibs` as `rlibs`.)\n\nTo allow production of root dependency modules that don't rely on any dynamic\nrust libraries (such as static executables), `rlibs` provides both dynamic and\nstatic `libstd` linkage variants. The correct variant is automatically linked by\nSoong."]]