自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
匯出常數
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如果介面不相容於 Java (例如使用了聯集),仍建議將常數 (列舉值) 匯出至 Java 世界。hidl-gen -Ljava-constants
…
支援此情境,可從套件中的介面檔案擷取已註解的列舉宣告,並產生名為 [PACKAGE-NAME]-V[PACKAGE-VERSION]-java-constants
的 Java 程式庫。請為要匯出的每個列舉宣告加上註解,如下所示:
@export
enum Foo : int32_t {
SOME_VALUE,
SOME_OTHER_VALUE,
};
如有需要,您可以新增註解參數 name
,讓此類型在匯出至 Java 世界時使用的名稱與介面宣告中選擇的名稱不同:
@export(name="JavaFoo")
enum Foo : int32_t {
SOME_VALUE,
SOME_OTHER_VALUE,
};
如果 Java 慣例或個人偏好設定要求將常見前置字元新增至列舉型別的值,請使用註解參數 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;
};
};
最後,針對 types.hal
中宣告的列舉類型,Java 類型宣告會在指定套件中的 Constants
類別中分組。宣告為介面子項的列舉類型會在該介面的 Java 類別宣告下方分組。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# 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."]]