Skip to main content

Android AIDL deep dive

In "Yet Another Deep Dive of Android Binder Framework", we introduced the native Binder/Interface class hierarchy and explained how to use those classes to create a service that supports IPC. In this post, we will switch from native world to the Java word, explaining how to achieve similar goal. 


In Java word, there are similar classes,  as shown in diagram below.

Class Diagram for Binder/Interface in Android Java Layer

Java AIDL Generator 

Luckily, Java application developer don't really need to deal with the Binder class directly. Instead, they use the AIDL tool to generate the code that will take care of the IPC stuff.  There are lots of excellent tutorials out there explaining how to define the interface using AIDL syntax and generate the proxy/stub code automatically and base on that implement your own service.  You can start with this official doc

Here, what I want to show you is what is actually generated and how it is related with the structure
we have shown in the native layer.  As shown in above diagram, the AIDL tool actually generated interface class definition, the proxy and stub code. Compared with the digram in native layer, the ICoolService.stub.proxy is equivalent to BpInterface, and the ICoolService.stub is equivalent to BnInterface.  So, the benefit is don't need to code those tedious marshaling and marshaling code in the step 2 and 3 as shown here. That enable you focus on implementing your your business logic.  Life is much easier!

C++ AIDL Generator

Good news is, recently, we can also generate similar stuff for the native code using cpp-aidl.
Actually, You can generate both the Java and c++ helper classes from single .aidl file, which is a great way to keep things in sync if , say, you have a native service and want to expose it to Java client.

Parcelable

For primitive type, such as string and int, aidl generator knows how to pack and unpack it and send it through the IPC channel. But for composite or user defined type, aidl generator don't know how to do it and will need your assistance.  That is what Parcelable for.  When pack it, aidl generator will call writeToParcel. When unpack it, aidl generator will call readFromParcel.  It is your responsibility to implement those two methods correctly. The exactly syntax or convention varies a little bit between C++ and Java but the theory is exactly the same. Actually, you can pack it from C++ and unpack it in the Java side, or vise versa.

For a comprehensive introduction of how to use C++ AIDL, read this. Many Android system components has already switched from hand crafted interface writing to use C++ AIDL. Probably, you want too.

Popular posts from this blog

Android Camera2 API Explained

Compared with the old camera API, the Camera2 API introduced in the L is a lot more complex: more than ten classes are involved, calls (almost always) are asynchronized, plus lots of capture controls and meta data that you feel confused about.

No worries. Let me help you out. Whenever facing a complex system need a little bit effort to understand, I usually turns to the UML class diagram to capture the big picture.

So, here is the class diagram for Camera2 API.




You are encouraged to read this Android document first and then come back to this article, with your questions. I'll expand what is said there, and list the typical steps of using camera2 API. 

1. Start from CameraManager. We use it to iterate all the cameras that are available in the system, each with a designated cameraId. Using the cameraId, we can get the properties of the specified camera device. Those properties are represented by class CameraCharacteristics. Things like "is it front or back camera", "outpu…

Java Collections Framework Cheat Sheet

Java Collections Framework (JCF) implements the Abstract Data Type  for Java platform. Every serious Java programmer should familiar himself on this topic and be able to choose the right class for specific need.  A thorough introduction to JCF is not the target of this small article and to achieve that goal you can start with this excellent tutorial . 

Instead, I'd like to
1) Provide an overview of JCF's classes ,   2) Provide a cheat sheet you can post in your cubicel for daily reference, 3) Underline the relationship between JCF's implementation and the data structure and algorithm you learned in your undergraduate course

With these goals in mind, I came up following diagram - Java Collection Cheat Sheet. You can click it to zoom in. There is no necessity for more explanation once your familiar with UML class diagram and have a basic understanding of common data structures.


Android Security: An Overview Of Application Sandbox

The Problem: Define a policy to control how various clients can access different resources. A solution: Each resource has an owner and belongs to a group.Each client has an owner but can belongs to multiple groups.Each resource has a mode stating the access permissions allowed for its owner, group members and others, respectively. In the context of operating system, or Linux specifically, the resources can be files, sockets, etc; the clients are actually processes; and we have three access permissions:read, write and execute.