ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
โมดูลคลัง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
โมดูลไลบรารี Rust มี 2 ประเภท ได้แก่ โมดูลที่สร้างไลบรารี Rust และโมดูลที่สร้างไลบรารีที่เข้ากันได้กับ C นอกจากนี้ คุณยังพิจารณามาโครเชิงกระบวนการของ Rust (proc-macros
) เป็นไลบรารีประเภทพิเศษสำหรับวัตถุประสงค์ของระบบการสร้างได้
rust_library
โมดูล rust_library
จะสร้างไลบรารี Rust ไว้ให้โมดูล Rust อื่นๆ ใช้งาน
นอกจากตัวแปร _host
แล้ว ไลบรารี Rust ยังมีประเภทโมดูลที่ควบคุมการลิงก์ที่ใช้ได้
ประเภทโมดูลไลบรารี Rust |
คำจำกัดความ |
rust_library |
มีทั้งตัวแปรไลบรารี rlib และ dylib AOSP แนะนำประเภทโมดูลนี้สำหรับไลบรารี Rust เนื่องจากช่วยให้โมดูลทำงานได้อย่างถูกต้องเมื่อระบุเป็นข้อกำหนดในพร็อพเพอร์ตี้ rustlibs |
rust_library_rlib |
มีเฉพาะตัวแปร rlib ของไลบรารี Rust เท่านั้น ไม่สามารถรับประกันได้ว่าโมดูลที่มีเฉพาะตัวแปร rlib จะทำงานร่วมกับพร็อพเพอร์ตี้ rustlibs ได้ |
rust_library_dylib |
มีเฉพาะตัวแปร dylib ของไลบรารี Rust เท่านั้น ไม่สามารถรับประกันได้ว่าโมดูลที่มีเฉพาะตัวแปร dylib จะทำงานร่วมกับพร็อพเพอร์ตี้ rustlibs ได้ |
rust_ffi
โมดูล rust_ffi
จะสร้างไลบรารีที่เข้ากันได้กับ C เพื่อทำงานร่วมกันกับโมดูล CC
นอกจากตัวแปร _host
แล้ว ไลบรารี FFI ของ Rust ยังมีประเภทโมดูลที่ควบคุมการลิงก์ที่ใช้ได้ดังที่แสดงในตารางต่อไปนี้
ประเภทโมดูลไลบรารี FFI ของ Rust |
คำจำกัดความ |
rust_ffi |
มีทั้งตัวแปรของไลบรารี C แบบคงที่และแบบแชร์ |
rust_ffi_shared |
มีเฉพาะตัวแปรไลบรารีที่ใช้ร่วมกัน C เท่านั้น |
rust_ffi_static |
มีเฉพาะตัวแปรไลบรารีแบบคงที่ C เท่านั้น |
ดูตัวอย่างพื้นฐานของการใช้ rust_ffi
สำหรับการเรียกใช้ Rust จาก C ได้ที่หน้ารูปแบบ Rust ของ Android
ดูข้อมูลเกี่ยวกับการใช้งานขั้นสูงได้ที่เอกสารประกอบอย่างเป็นทางการของ 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
Dependencies จะขึ้นอยู่กับค่ากําหนดการลิงก์ของโมดูลรูท (เช่น rust_binary
ที่มี prefer_rlib: true
จะต้องใช้ตัวแปรไลบรารี Rust ที่ลิงก์ rustlibs
เป็น rlibs
)
rlibs
มีตัวแปรการลิงก์ทั้งแบบไดนามิกและแบบคงที่ของ libstd
เพื่อให้สามารถผลิตโมดูลการพึ่งพารูทที่ไม่ใช้ไลบรารี Rust แบบไดนามิก (เช่น ไฟล์ปฏิบัติการแบบคงที่) โดย
Soong จะลิงก์ตัวแปรที่ถูกต้องโดยอัตโนมัติ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],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."]]