ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
ส่งออกค่าคงที่
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ในกรณีที่อินเทอร์เฟซไม่เข้ากันได้กับ Java (เนื่องจากใช้ยูเนียน เป็นต้น) คุณอาจยังต้องการส่งออกค่าคงที่ (ค่า Enum) ไปยังโลก Java hidl-gen -Ljava-constants
…
รองรับสถานการณ์นี้ ซึ่งจะดึงข้อมูลประกาศ enum ที่มีคำอธิบายประกอบจากไฟล์อินเทอร์เฟซในแพ็กเกจ และสร้างไลบรารี Java ที่มีชื่อว่า [PACKAGE-NAME]-V[PACKAGE-VERSION]-java-constants
กำกับเนื้อหาการประกาศ enum แต่ละรายการที่จะส่งออกดังนี้
@export
enum Foo : int32_t {
SOME_VALUE,
SOME_OTHER_VALUE,
};
หากจำเป็น ชื่อที่ใช้ส่งออกประเภทนี้ไปยังโลก Java อาจแตกต่างจากชื่อที่เลือกในการประกาศอินเทอร์เฟซโดยเพิ่มพารามิเตอร์การกำกับเนื้อหา name
ดังนี้
@export(name="JavaFoo")
enum Foo : int32_t {
SOME_VALUE,
SOME_OTHER_VALUE,
};
หากแบบแผนของ Java หรือค่ากําหนดส่วนตัวกําหนดให้เพิ่มคำนำหน้าทั่วไปลงในค่าของประเภท enum ให้ใช้พารามิเตอร์การกำกับvalue_prefix
ดังนี้
// File "types.hal".
package android.hardware.bar@1.0;
@export(name="JavaFoo", value_prefix="JAVA_")
enum Foo : int32_t {
SOME_VALUE,
SOME_OTHER_VALUE,
};
คลาส Java ที่สร้างขึ้นจะปรากฏดังนี้
package android.hardware.bar.V1_0;
public class Constants {
public final class JavaFoo {
public static final int JAVA_SOME_VALUE = 0;
public static final int JAVA_SOME_OTHER_VALUE = 1;
};
};
สุดท้าย การประกาศประเภท Java สำหรับประเภท enum ที่ประกาศใน types.hal
จะจัดกลุ่มภายในคลาส Constants
ในแพ็กเกจที่ระบุ ระบบจะจัดกลุ่มประเภท Enum ที่ประกาศเป็นรายการย่อยของอินเทอร์เฟซไว้ภายใต้การประกาศคลาส Java ของอินเทอร์เฟซนั้น
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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,["# Export constants\n\nIn cases where an interface isn't Java-compatible (because it uses unions\nfor example) it might still be desirable to export the constants (enum values) to\nthe Java world. This scenario is supported by `hidl-gen -Ljava-constants\n...` which extracts annotated enum declarations from the interface\nfile(s) in a package and produces a java library named\n`[PACKAGE-NAME]-V[PACKAGE-VERSION]-java-constants`. Annotate each\nenum declaration to be exported as follows: \n\n```java\n@export\nenum Foo : int32_t {\n SOME_VALUE,\n SOME_OTHER_VALUE,\n};\n```\n\nIf necessary, the name under which this type is exported to the Java world\ncan be different from that chosen in the interface declaration by adding the\nannotation-parameter `name`: \n\n```java\n@export(name=\"JavaFoo\")\nenum Foo : int32_t {\n SOME_VALUE,\n SOME_OTHER_VALUE,\n};\n```\n\nIf Java conventions or personal preference ask for a common prefix to be\nadded to the enum type's values, use the annotation-parameter\n`value_prefix`: \n\n```java\n// File \"types.hal\".\n\npackage android.hardware.bar@1.0;\n\n@export(name=\"JavaFoo\", value_prefix=\"JAVA_\")\nenum Foo : int32_t {\n SOME_VALUE,\n SOME_OTHER_VALUE,\n};\n```\n\nThe resulting Java class appears as follows: \n\n```java\npackage android.hardware.bar.V1_0;\n\npublic class Constants {\n public final class JavaFoo {\n public static final int JAVA_SOME_VALUE = 0;\n public static final int JAVA_SOME_OTHER_VALUE = 1;\n };\n};\n```\n\nFinally, Java type declaration for enum types declared in\n`types.hal` are grouped inside a class `Constants` in the\ngiven package. Enum types declared as children of an interface are grouped\nunder that interface's Java class declaration."]]