Detailed explanation of Android system Binder

Detailed explanation of Android system Binder

[[422252]]

This article is reprinted from the WeChat public account "Embedded Linux System Development", written by asonangel. Please contact the WeChat public account "Embedded Linux System Development" to reprint this article.

Recently the blogger is studying the binder of the Android system.

What is binder? It can be understood as a method of inter-process communication in the Android system. Although the Android system is based on Linux, it does not use the inter-process communication method that comes with Linux, but uses a more efficient binder. Understanding binder is crucial to understanding the Android system. The binder principle diagram is as follows:

Binder communication uses a client/server architecture, which is the same as Socket, but slightly more complicated. The binder component includes Client, Server, ServiceManager, and binder driver, among which ServiceManager is used to manage various services in the system.

Specific communication process: Server first registers a service with ServiceManager, which is actually just a string. Then Client obtains the service from ServiceManager, and the keyword is the registered string. In this way, Client and Server can communicate. The real data flow goes through the binder driver in the underlying Linux kernel space, but this is encapsulated, so you don’t need to care about the real binder driver, just call functions between Client and Server to send and receive data. It is the onTransact function on the server and remote()->transact(TEST, data, &reply) on the client. The sent data is stored in data, and the returned data is stored in reply. Both Client and Server can send and receive data.

Android system architecture diagram:

After understanding binder, let's analyze it in detail: the binder driver must be in the bottom Linux kernel, so the binder client and server can exist anywhere. For example:

1. The binder server is in the app layer, and the client is in the app layer. 2. The binder server is in the native layer, and the client is in the app layer. 3. The binder server is in the app layer, and the client is in the native layer. 4. The binder server is in the native layer, and the client is in the native layer.

All four situations are possible.

This is a bit abstract, so I will post two articles to link. I have verified the codes of these two articles and they can be used. It should be noted that the binder of the APP layer is written in Java, and the binder of the native layer is written in C++. But in fact, the interfaces are very similar.

But to understand the code, you need to understand several classes first:

The real inheritance relationship of native layer binder is as follows:

A little more symmetry may make it easier to understand:

We mainly inherit from BnInterface and BpInterface to implement our own server and client.

bp: binder proxy

bn:binder native

sm:ServiceManager

<<:  Goodbye WeChat! My WeChat chat history is not worth 180 yuan

>>:  Five UI styles for the second half of 2021, there must be one you like!

Recommend

Apple Watch Sport disassembly: Component cost is 500 RMB

Market research firm IHS iSuppli released a disas...

Major brands are leveraging New Year’s Day posters, take them!

Every year end Various festivals have also become...

How can you write a good copy to attract users’ attention?

For an advertisement, the importance of copywriti...

Ten elements of good copywriting

In the advertising industry, copywriting is the l...

Let’s take a look at BAT’s layout in the AR field. How would you rate it?

[[170825]] The so-called AR (augmented reality) i...

iPhone 6 vs. 5s from all angles

Recently, the Italian website macitynet.it release...

Android 5.1 is not available

Android 5.0 is the latest version released by Goo...

Can we distinguish different leopards by their calls? | Nature Trumpet

This is the 73rd issue of the Nature Trumpet colu...

Cleverly use leverage to attract 1 million users

How to define seed users ? Three conditions for d...