AOSP Foundations
3 min read

The hardware/ Directory

Explore the Hardware Abstraction Layer (HAL) interfaces that standardize communication between Android and proprietary silicon.

The Android operating system is designed to run on a massive variety of hardware configurations, from low-end smartwatches to high-end flagship smartphones, tablets, and even cars.

Because Google does not manufacture all of these hardware components natively, they needed a standardized way for the Android software framework to talk to the physical chips. The solution is the Hardware Abstraction Layer (HAL), and its definitions live in the hardware/ directory.

The Purpose of HALs

Imagine a camera app trying to take a photo. A Qualcomm camera sensor requires completely different electrical signals and initialization sequences than a Sony camera sensor.

If the Android framework had to know about every single camera sensor in the world, the codebase would be unmaintainable. Instead, Google defines a generic "Camera HAL Interface."

  1. The Android framework says, "Take a photo."
  2. The HAL receives this generic command via IPC.
  3. The vendor-specific implementation of the HAL translates the generic command into the proprietary code required by that specific physical sensor.

Core Subdirectories

hardware/interfaces

Since Android 8.0 (Project Treble), HALs are strictly separated from the main Android OS. They run in their own isolated user-space processes and communicate with the framework via Inter-Process Communication (IPC, specifically hwservicemanager).

This directory contains the strict interface definitions for every type of hardware Android supports (Bluetooth, Audio, Camera, NFC, Sensors).

  • HIDL (.hal files): The Hardware Interface Definition Language was the original language used to define these interfaces.
  • AIDL (.aidl files): The Android Interface Definition Language is the modern standard, replacing HIDL to unify how Java apps and native C++ HALs communicate across process boundaries.
// Example of an AIDL definition in hardware/interfaces
package android.hardware.light;

@VintfStability
interface ILight {
    void setLightState(in int id, in HwLightState state);
    HwLight[] getLights();
}

When you compile AOSP, the build system parses these definition files and auto-generates the massive C++ and Java boilerplate code required to securely pass data back and forth between the framework and the hardware driver.

hardware/libhardware

This directory contains the legacy (pre-Treble) HAL definitions. In the early days of Android, HALs were simply shared .so libraries loaded directly into the memory space of the framework process. While modern Android devices use the IPC-based interfaces mentioned above, the legacy C headers in this directory are still maintained for backward compatibility and for extremely simple hardware implementations.

Vendor Implementations

It is critically important to understand that the hardware/ directory primarily contains the interfaces (the strict rules of communication).

The actual, proprietary C++ code that implements these rules and physically talks to the silicon is usually closed-source and is provided by the hardware manufacturer. Those implementation binaries live entirely outside of the hardware/ directory, typically inside the vendor/ directory.

# Debugging HAL interfaces by listing all registered hardware services running on the device
adb shell lshal