ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
โมดูลไบนารี
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณสร้างไบนารี Rust ได้โดยใช้ประเภทโมดูล rust_binary
เช่น
rust_binary {
name: "hello_rust",
srcs: ["src/hello_rust.rs"],
}
ในทํานองเดียวกัน คุณสามารถใช้ rust_binary_host
เพื่อระบุข้อบังคับสําหรับโฮสต์เท่านั้นได้
พร็อพเพอร์ตี้ rust_binary ที่ควรทราบ
นอกจากพร็อพเพอร์ตี้ทั่วไปที่สําคัญแล้ว พร็อพเพอร์ตี้ต่อไปนี้ก็สำคัญเช่นกัน หรือแสดงลักษณะการทํางานที่ไม่ซ้ำกันเฉพาะสําหรับประเภทโมดูล rust_binary
- static_executable: สร้างไบนารีนี้เป็นไบนารีแบบคงที่ และบอกเป็นนัยว่า
prefer_rlib
คือ true
ไฟล์ปฏิบัติการแบบคงที่ทั้งหมดจะสร้างขึ้นสําหรับเป้าหมาย Bionic เท่านั้น สําหรับเป้าหมายที่ไม่ใช่ไบโอนิก เงื่อนไขนี้หมายความว่า prefer_rlib
เป็นเพียง true
เท่านั้น แต่ยังคงลิงก์ libc
กับ libdl
แบบไดนามิก
- prefer_rlib: เปลี่ยนการลิงก์
rustlibs
เพื่อเลือกการลิงก์ rlib
โดยค่าเริ่มต้นสําหรับเป้าหมายอุปกรณ์ และลิงก์ libstd
เป็น rlib
ในเป้าหมายอุปกรณ์ โปรดทราบว่าลักษณะการทำงานนี้เป็นลักษณะการทำงานเริ่มต้นของเป้าหมายโฮสต์และไม่มีผลกระทบต่อเป้าหมายโฮสต์
การลิงก์
โดยค่าเริ่มต้น โมดูล rust_binary
ที่กําหนดเป้าหมายเป็นอุปกรณ์จะลิงก์กับ libstd
แบบไดนามิกเสมอ และโดยค่าเริ่มต้นจะเลือกไลบรารี dylib
สําหรับข้อกําหนดซึ่งระบุไว้ใน rustlibs
เว้นแต่ prefer_rlib
จะเท่ากับ true
หากต้องใช้ไบนารีแบบคงที่ทั้งหมด โปรดดูพร็อพเพอร์ตี้ static_executable
โมดูลโฮสต์จะลิงก์กับ libstd
แบบคงที่เสมอ และโดยค่าเริ่มต้นจะเลือกไลบรารี rlib
สำหรับ Dependency ที่แสดงใน rustlibs
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[null,null,["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],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`."]]