The RPC mechanism provided by Binder is not a good fit for certain use cases. As we know, Android framework allocates about 1 MB of virtual memory for each process in ProcessState. So you can only transfer at most several hundreds of KB within one transaction. Besides, every bits of data needs to be copied to target process's memory space so transferring bulk data with Binder transaction is less efficient. Another use case where Binder RPC mechanism doesn't fit well is streaming data transfer. Using Binder transactions to implement streaming data transfer will be highly inefficient.‌

But this doesn’t mean…


Most of the world’s authentication and authorization (auth) techniques in computer science or even in real life are either based on “who you are” (identity based) or “what you own” (possession based). The purpose of auth is to grant or deny someone access to certain resources. For the purpose of easier description, let’s call the one who requests access to certain resources “subject”, and call the one who performs the auth “authenticator”. In a “who-you-are” based auth, the authenticator performs the auth by obtaining the identity of the subject under an identification system that the subject have no control with…


Death notification

Death notification is a Binder mechanism to allow a BpBinder to get notified when the BBinder it references to dies. I briefly touched on this topic in the "Binder architecture and core components" article. In this article we are going to take a deeper look into its implementation. When you say a BBinder dies, it means the process that created this BBinder terminates due to unexpected crashing or intentional exiting. …


The native IBinder and BpRefBase classes inherit from a base class RefBase. In the article "Binder architecture and core components" I skipped it to focus on the key topics of that article, but now it is time to look into it. If you are creating a class that needs to be managed by Android smart pointer framework, it should inherit from RefBase or LightRefBase. Smart pointers is not only used by Binder. In contrast, it is used ubiquitously by Android native frameworks. …


Service Manager is a first class component in Binder that is responsible for managing all system services. A system service is a special kind of BBinder that's created in a system process to provide certain kinds of services for other processes. In essence, these system services don't have much difference with the RemoteService class in our HamKing project [1]. It's just that they run in privileged processes and can access protected resources so they can provide services that apps cannot do on their own. Service Manager is such a component where system processes can register the system services they create…


The ultimate reason of using Binder is to transfer data between processes. The action of sending data to a remote Binder for processing and getting back a reply is called a Binder transaction. There are typically four main steps involved in a Binder transaction:

(1) The source process prepares the data to be transferred and make a BINDER_WRITE_READ with BC_TRANSACTION command code. Binder driver will create a binder_transaction data structure for this transaction, finds a Binder thread in target process and enqueue this binder_transaction structure to the todo list of target thread then wake the thread up. …


Binder threads

In the HamKingServer class of the HamKing sample project [1], I annotated all the service methods with @BinderThread:

When an application is started by Android system, a Binder thread pool is allocated for the application to handle incoming Binder transactions. Note that a Binder thread is only used to serve incoming transactions on the server side of a Binder service. When an app initiates the transaction on the proxy side, the app usually fires off and wait for the transaction to finish on the UI thread. In the above example, the four annotated methods run when the client app…


Simply speaking, the only thing you use Binder for is transferring data between processes. During a Binder transaction, the source process will pack all data to be transferred in a serializable form and hand it over to Binder driver. When the driver handles the Binder transaction, the serialized data will be copied to target process’s memory space. The target process will then deserialize the data into various objects. So in a typical Binder transaction the data is copied three times.

Parcel

The serialization and deserialization of data happen in native user space. Under Binder’s terminology, the serialization process is called parceling…


Android interface definition language (AIDL) is used to define the contract that a Binder service object provides. With the help of AIDL developers only need to focus on implementing the business logic of the Binder service interface instead of all kinds of boilerplate code. The tool for auto-generating Java source code is aidl. It is included in Android SDK under build-tools directory. Not too long ago AIDL is only used for auto-generating Java code. But now Android includes a aidl-cpp tool to auto-generate C++ code as well. It is included in the Android Open Source Project (AOSP). If you don't…


Binder components

Native and proxy Binders

Binder framework takes an object-oriented approach in its design principle and every entity that is capable of serving inter-process calls is a user-space object. Let’s use our HamKing project [1] as an example: The server side app creates a RemoteService object which provides the actual implementation of the IHamKingInterface Binder service interface. The client side app creates a handle mService referring to this remote object via the auto-generated method IHamKingInterface.Stub.asInterface. The client side app can then use this handle to make a remote Binder call to the server side app.

We refer to a Binder object as “local”…

Baiqin Wang

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store