Starting March 27, 2025, we recommend using android-latest-release
instead of aosp-main
to build and contribute to AOSP. For more information, see Changes to AOSP.
Export constants
Stay organized with collections
Save and categorize content based on your preferences.
In cases where an interface isn't Java-compatible (because it uses unions
for example) it might still be desirable to export the constants (enum values) to
the Java world. This scenario is supported by hidl-gen -Ljava-constants
…
which extracts annotated enum declarations from the interface
file(s) in a package and produces a java library named
[PACKAGE-NAME]-V[PACKAGE-VERSION]-java-constants
. Annotate each
enum declaration to be exported as follows:
@export
enum Foo : int32_t {
SOME_VALUE,
SOME_OTHER_VALUE,
};
If necessary, the name under which this type is exported to the Java world
can be different from that chosen in the interface declaration by adding the
annotation-parameter name
:
@export(name="JavaFoo")
enum Foo : int32_t {
SOME_VALUE,
SOME_OTHER_VALUE,
};
If Java conventions or personal preference ask for a common prefix to be
added to the enum type's values, use the annotation-parameter
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,
};
The resulting Java class appears as follows:
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;
};
};
Finally, Java type declaration for enum types declared in
types.hal
are grouped inside a class Constants
in the
given package. Enum types declared as children of an interface are grouped
under that interface's Java class declaration.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-08-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-08-26 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."]]