Progress Β· 0/7 phases
π π Phase 4 β HAL, Binder IPC & System Services
4 min read Β· Notion
Duration: 4β6 weeks | Level: Advanced
Binder is the backbone of Android. Everything talks through it. HAL is how the framework talks to hardware. Master both.
π Binder IPC β The Core of Android IPC
What is Binder?
Binder is Android's primary inter-process communication (IPC) mechanism. It's a Linux kernel driver (drivers/android/binder.c) that enables:
- Fast, safe, synchronous RPC between processes
- Identity-based security (callerUID, callerPID tracking)
- Object references across process boundaries
Why not UNIX sockets or pipes?
- Binder is ~10x faster than D-Bus (used on Linux desktop)
- Supports passing file descriptors and complex objects
- Enforces permissions via
uid/pidof the calling process - One copy instead of two (copy-on-write via kernel memory mapping)
π± Binder Architecture
App Process (Client) System Server (Service)
ββββββββββββββββββββββ ββββββββββββββββββββββ
β Proxy (IActivityManager)β β Stub (ActivityManager)β
β calls transact() β β onTransact() handlesβ
ββββββββββββββββββββββ ββββββββββββββββββββββ
β Binder kernel driver β
/dev/binder (mmap shared buffer)Binder Transaction Flow
- Client calls method on Proxy object
- Proxy serializes arguments into a
Parcel - Calls
IBinder.transact()β syscall into kernel - Kernel driver copies data to service process's memory
- Service's Stub
onTransact()deserializes and calls real method - Result parceled back the same way
π AIDL β Android Interface Definition Language
AIDL auto-generates Proxy + Stub boilerplate from an interface definition:
// IMyService.aidl
interface IMyService {
String getMessage(int id);
void setData(in ParcelableData data);
}Build generates:
IMyService.javaβ interfaceIMyService.Stubβ extend this in your serviceIMyService.Stub.Proxyβ used by clients automatically
AIDL in AOSP context
- Framework services (AMS, WMS) define AIDL in
frameworks/base/core/java/android/ - New AIDL-based HAL interfaces live in
hardware/interfaces/
π ServiceManager β The IPC Registry
ServiceManager is the Binder name registry (like DNS for services):
// Registering (done in SystemServer)
ServiceManager.addService("activity", mActivityManagerService);
// Getting a service (done in client)
IBinder binder = ServiceManager.getService("activity");
IActivityManager am = IActivityManager.Stub.asInterface(binder);Key files:
frameworks/native/cmds/servicemanager/servicemanager.cppframeworks/base/core/java/android/os/ServiceManager.java
π Hardware Abstraction Layer (HAL)
Why HAL exists
- Different phones have different hardware (cameras, sensors, audio chips).
- HAL provides a standard interface between Android Framework and hardware drivers.
- OEMs implement HAL for their specific hardware in
vendor/without modifying framework.
HAL Evolution
| Generation | Interface | Location |
|---|---|---|
| Legacy HAL | C struct (hw_module_t) | hardware/libhardware/ |
| HIDL HAL (8.0+) | HIDL (HAL Interface Definition Language) | hardware/interfaces/ |
| AIDL HAL (11.0+) | AIDL (same as framework AIDL) | hardware/interfaces/ |
HIDL HAL example
hardware/interfaces/sensors/2.0/
βββ ISensors.hal # Interface definition
βββ types.hal # Data types
βββ default/
βββ Sensors.h # Implementation header
βββ Sensors.cpp # OEM-provided implementationWriting a simple Legacy HAL module
// my_hal.c
#include <hardware/hardware.h>
static struct hw_module_methods_t my_module_methods = {
.open = my_device_open,
};
struct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.id = "com.example.my_hal",
.name = "My HAL Module",
.methods = &my_module_methods,
};π οΈ Hands-On Tasks
- Trace a Binder call: Use
adb shell amto start an Activity and trace it throughActivityManagerService. - Write a custom AIDL service:
- Define
IHelloService.aidl - Implement
HelloService.javaextendingStub - Register in
SystemServer.java - Access it from an app
- Define
- Read a real system service AIDL: Browse
frameworks/base/core/java/android/app/IActivityManager.aidl - Implement a stub Legacy HAL: Write a
.cfile withhw_module_t, addAndroid.bp, build and push to/vendor/lib/hw/. - Use
adb shell service listto see all registered Binder services.
β Phase 4 Checklist
- Can explain Binder IPC flow (client proxy β kernel β service stub)
- Know why Android uses Binder instead of UNIX sockets
- Understand
Parceland how data is serialized - Can write a basic AIDL interface + service
- Read
IActivityManager.aidland traced a real call - Understand
ServiceManageras a name registry - Know the difference between Legacy HAL, HIDL, and AIDL HAL
- Read a HIDL
.halinterface file - Implemented a stub Legacy HAL module
- Registered a custom service in
SystemServer.java - Used
service listandservice callfrom adb shell