An Internal study to Binder - A bliss for Android
Prerequisite:- Basic Android programming awareness.
As Android developers, we have encountered almost all the basic components in different applications, whether they are Intent, broadcast receiver, service, or content provider.
However, process-to-process communication is a must when designing these components, which means implementing IPC (interprocess communication).
IPC is a complicated procedure in terms of design. In terms of Linux internals, it can be achieved through Signals (the oldest IPC way), Pipes, Sockets, Message queues, Semaphores, or shared memory.
However, Android is designed primarily for memory devices with highly secure application-to-application communications. It needs an Object-oriented method, low memory calls, thread migration, and a synchronous and asynchronous execution model.
For this, the Android team modified the kernel and introduced the Binder framework to achieve this via the RPC (Remote procedure call) mechanism between the client and server processes.
Intents, Content Providers, Messenger, and all system services like Telephone, Vibrator, Wi-Fi, Battery, Notification, etc. utilise the IPC infrastructure provider by Binding. ActivityManagerServer invokes even the lifecycle callbacks in your Activity, like onStart(), onResume(), and onDestroy(), via binding.
The Binder was originally developed under the name OpenBinder by Be Inc and later Palm Inc under the leadership of Dianne Hackborn.
Or more concretely, the Binder has the facility to provide bindings to functions and data from one execution environment to another. The OpenBinder implementation runs under Linux and extends the existing IPC mechanisms. The documentation of OpenBinder states that ”... the code has run on a diverse variety of platforms, including BeOS, Windows, and PalmOS Cobalt.”
Binder in Android is a customised implementation of OpenBinder. In An—droid’s M3 release, the original kernel driver part from OpenBinder was used, but the user space part had to be rewritten because OpenBinder's license was not compatible with Android’s license. In Android’s M5 release, the driver was rewritten, too. The core concepts remained the same, but many details have changed.
The complete flow of Binder in Android can be understood via the diagram below.
The above diagram explains that whenever an application needs IPC communication, it just needs to create a binder object by implementing a Binder interface that defines the necessary tasks and the business logic of tasks in AIDL (Android interface definition language ).
Now internally, AIDL, after compiling, creates a proxy for the client and a stub for the server to communicate with each other. Client Parcel the message (Marshalling)and pass it on to the Binder driver in kernel. Which transfers it to Server after Unmarshalling the same message. Now, those who have implemented the Binder interface can communicate with each other.
***Note:-
1. System to kernel protocol is Ioctl.
2. Binder drivers are present in the BIONIC folder of the Android source code under driver/Binder.c
3. The YouTube tutorial below is enough for a deep understanding of framework code and examples.
4. Binder code is not POSIX compliance of Linux
References:-
1. (Best ) A YouTube video and slides via Aleksandar Gargenta - https://newcircle.com/s/post/1340/deep_dive_into_android_ipc_binder_framework_at_andevcon_iv#slide-9
2. Article of Android Binder via Thorsten Schreiber
3. Compilation of Aleksandar Garment article and other references at :-