Binder is a system for interprocess communication that lets two processes on an Android-powered device communicate. Binder provides a means to execute function calls in another process that is completely transparent to the caller.
In binder terms, the calling process is considered the client and its endpoint is called the binder proxy or proxy. Conversely, the process being called is the server and its endpoint is called the binder node or node.
Each node can expose and implement its own interface. And, using a proxy, the client can execute methods on a node interface as though invocation was a local function call. The following example shows a method being invoked:
int result = someNodeInterface.foo(a, b); // someNodeInterface is a proxy object
Assume that the client calling foo()
is running in process A and the server
implementing foo()
is running in process B. Figure 1 shows how this call is
executed:
Figure 1. Binder call execution.
To execute a method in another process, as shown in figure 1, the following occurs:
- The client code in process A invokes the proxy code in process A. The proxy
code in process A creates a transaction containing the following items:
- An identifier for the node
- An identifier for the
foo()
method on the node - A buffer containing a copy of the arguments
a
andb
- The transaction is submitted to the binder kernel driver.
- The binder kernel driver determines that process B hosts the node.
- The kernel copies the entire transaction into process B's address space.
- The kernel finds a thread in process B to handle the transaction and passes the transaction to it.
- The thread unpacks the transaction, finds the node, and sends the transaction to the node object.
- The node object obtains the function identifier from the transaction,
unpacks
a
andb
from the transaction buffer, and storesa
andb
in local variables. - The node object calls
foo(a, b)
on the server code in process B. - The result of the call is returned in a reply transaction, which is passed to the kernel driver and then back to the calling proxy in process A.
- The proxy returns that result to the caller in process A.
Binder use cases
Binder can be used in a variety of scenarios where communication between software in different processes must occur. For example:
A camera app uses binder to communicate with the camera server in another process. The camera server then uses binder to communicate with the camera HAL in another process.
An app uses binder to communicate with a system server in another process. The system server uses binder to talk to HALs in other processes.
An app in one process uses binder to communicate with a different app in another process.
The system daemon responsible for installing, updating, and removing apps (
installd
) uses binder to communicate with the Android runtime daemon ('artd') to compile apps.