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

360 Mobile executives make their debut: Zhou Hongyi fights Lei Jun again

96 days after injecting US$409 million into Coolp...

Web caching via HTTP protocol

Dear, do you know what cache is? In fact, caching...

Is Meizu mobile phone still alive? Does it have a chance to rise again?

On Weibo, there is a classic question with a play...

6 aspects to explain how brands "brainwash" consumers

First, let me ask you a question: Assuming the pr...

Disassembling 9 human marketing skills and cases

Today I continue to share 9 human marketing cases...

How to improve the conversion rate of APP landing page?

Landing page quality and user matching are two ke...

Fission 1w+, live broadcast sharing you have to do this

Nowadays, the cost of acquiring customers for onl...