Exporting Constants

In cases where an interface is not Java-compatible (because it uses unions for example) it may 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 will be grouped under that interface's Java class declaration.