Note: This section uses sample .hal files to illustrate how HIDL language constructs map to C++.

With few exceptions, HIDL interface packages are located in hardware/interfaces or the vendor/ directory. The hardware/interfaces top-level maps directly to the android.hardware package namespace; the version is a subdirectory under the package (not interface) namespace.

The hidl-gen compiler compiles the .hal files into a set of a .h and .cpp files. From these autogenerated files a shared library that client/server implementations link against is built. The Android.bp file that builds this shared library is autogenerated by the hardware/interfaces/update-makefiles.sh script. Every time you add a new package to hardware/interfaces, or add/remove .hal files to/from an existing package, you must rerun the script to ensure the generated shared library is up-to-date.

For example, the IFoo.hal sample file should be located in hardware/interfaces/samples/1.0. The sample IFoo.hal file creates an IFoo interface in the samples package:

package android.hardware.samples@1.0;
interface IFoo {
    struct Foo {
       int64_t someValue;
       handle  myHandle;
    };

    someMethod() generates (vec<uint32_t>);
    anotherMethod(Foo foo) generates (int32_t ret);
};

Generated files

Autogenerated files in a HIDL package are linked into a single shared library with the same name as the package (for example, android.hardware.samples@1.0). The shared library also exports a single header, IFoo.h, which can be included by clients and servers. Using the hidl-gen compiler with the IFoo.hal interface file as an input, binderized mode has the following autogenerated files:

Figure 1. Files generated by compiler.

The files are structured similarly to the files generated by aidl-cpp (for details, see "Passthrough mode" in the HIDL Overview). The only autogenerated file that is independent of the RPC mechanism used by HIDL is IFoo.h; all other files are tied to the HwBinder RPC mechanism used by HIDL. Therefore, client and server implementations should never directly refer to anything other than IFoo. To achieve this, include only IFoo.h and link against the generated shared library.

Note: HwBinder is only one possible transport; new transports may be added in the future.

A client or server that uses any interface in a package must include the shared library of that package in one (1) of the following locations:

For specific libraries:

libhidlbase Includes standard HIDL data-types. Unless your interface consists only of primitives that map directly to C++ primitives, you must also link this library:
LOCAL_SHARED_LIBRARIES += libhidlbase
libhidltransport Handles the transport of HIDL calls over different RPC/IPC mechanisms. You must always link this library:
LOCAL_SHARED_LIBRARIES += libhidltransport
libhwbinder You must also link to this library:
LOCAL_SHARED_LIBRARIES += libhwbinder
libfmq To use Fast Message Queue IPC, you must also link to this library.
LOCAL_SHARED_LIBRARIES += libfmq

Namespaces

HIDL functions and types such as Return<T> and Void() are declared in namespace ::android::hardware. The C++ namespace of a package is determined by the package name and version. For example, a package mypackage with version 1.2 under hardware/interfaces has the following qualities: