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

How much does it cost to develop a small moving program in Baise?

The launch of mini programs has brought convenien...

Analysis of Tmall Double 11 event operations in 2019!

This article attempts to analyze the overall game...

How to promote on Bilibili?

Bilibili , a video content community that started...

How much does it cost to make an underwear mini program on the Anyang market?

How much does it cost to produce the Anyang under...

Switching to the "Google Model": Alibaba's new path for home Internet

The concept of home Internet continues to be popu...

A guide to traffic channels for Douyin live streaming rooms!

When it comes to Tik Tok live streaming , the mos...

The battle between the two: a game of efficiency

What is the most essential meaning of competition...

6 suggestions to create an operational promotion plan that can be implemented!

Whether you are a novice in website operation or ...

Bamboo is a golden bachelor, reproducing sexually once every 130 years

I first learned about the name of Phyllostachys b...

Baimei VIP course (focused on honest people transformation)

Baimei VIP course (focused on honest people trans...