IPC
Android runs apps in separate Linux processes.
This is done for security, stability, and sandboxing.
Which means if Process A wants to talk to Process B, it must use Inter-Process Communication (IPC).
Binder
It is core IPC mechanism of Android System.
Main Components
1. Client Process
The process requesting a service.
For example: App → wants to call LocationManager
2. Proxy
Client-side object.
It:
looks like a normal Java object
secretly sends IPC requests.
3. Binder Driver
It handles:
process communication
thread management
message passing
4. Stub
Server-side Binder object.
It:
receives requests
unmarshals data
calls actual service method
5. Server Process
The process that implements the service.
For example: system_server → runs LocationManagerService
Binder thread pool
Each Binder service has a thread pool.
Multiple apps may call the same service simultaneously.
Binder driver dispatches them to threads like:
This prevents blocking.
Zero Copy Optimization
Binder uses shared memory instead of copying data repeatedly.
Typical IPC:
Binder:
Much faster.
AIDL
AIDL (Android Interface Definition Language) is used when two processes need to talk to each other using Binder IPC.
AIDL defines what methods can be called remotely.
Think of it like a contract between client and service.
Example contract: ICalculator.aidl
This file is not used directly. During build, AIDL compiler generates Binder code.
Android build system runs:
This generates a Java interface like:
Structure:
Service process implements the Stub.
Stub receives IPC calls. Stub is the server-side Binder implementation.
Client binds to the service. Proxy is the client-side implementation.
Full flow:
in, out, inout
They define data direction between client and service.
in: (Default) Client → Service
out: Service → Client
inout: Both directions.
oneway
Normal AIDL calls are synchronous.
Meaning:
Example:
But if you write:
Then:
This becomes asynchronous.
Used for notifications / events.
oneway methods cannot return values, because the client doesn't wait.
What happens if a process dies?
Binder detects process death.
Client can register:
Why is AIDL faster than sockets?
Because Binder:
uses shared memory
avoids heavy copying
uses kernel driver optimized for IPC
Is AIDL required for Binder?
No.
You can implement Binder manually using:
AIDL just generates the boilerplate.
Messenger IPC
Messenger is a simpler wrapper around Binder.
Instead of defining interfaces, it sends Messages using Handlers.
Good for simple request-response communication.
Architecture:
Service Handler:
Messenger:
Client sends:
Limitations
sequential processing
slower than AIDL
Content Providers
Used for structured data sharing across apps.
Example:
Architecture:
Broadcast IPC
Used for system-wide event communication.
Sockets
Used mainly by native services.
Ashmem
Binder has size limits (~1MB).
For large buffers Android uses: Ashmem (Android Shared Memory)
Architecture: