[App Development] Use AIDL for Inter-process Communication in Android Apps!
Introduction to AIDL
Sometimes you want to communicate from process A to process B.
In this case, you can use AIDL to communicate between different processes.
- It can be used for communication between two apps.
- It can be used for communication between an app and the native kernel layer (but you must build AOSP and put the written application into the underlying layer).
- Or communication between an app and a service… etc.
Additionally, AIDL is a commonly used IPC solution.
I have also written another article summarizing common IPC solutions.
If needed, you can refer to it:
IPC Inter-process Communication Solutions
AIDL Implementation
-
In this step, you can write freely in a notebook.
Or you can use an IDE to write (here I am using Android Studio).
As long as it is a place where you can type, you can write.
Next, define what interfaces your AIDL will expose.
For example, if you want a function to get all update lists,
you can create an interface similar to getUpdateList.
If you want to get the status, write a getStatus interface.
And so on…
-
Finally, you need to save the file with the extension .aidl.
-
After completing the above AIDL interface,
you need to build it.
You can use the CLI
./gradle build
to build it,
or use theBuild > Rebuild Project
option in Android Studio to build it.
-
After building, a .java file with the same name will be generated.
-
Usually, in an Android app, you will use a Service to call the AIDL interface.
So first, extend the Service and override the necessary lifecycle methods.
An example is as follows:
-
Next, add the AIDL instance generated after the build to the same service:
The three images below are illustrative:
Add the Service from Step 2
to AndroidManifest.xml and add an action
The action here corresponds to the action that will be used in Step 5
to bind the ServiceConnection
action android:name="elegant.access.service.IRSSmartService"
- Here you can start to add your AIDL interface + implement the service that instantiates AIDL
for other cross-process apps or different layers to use
In Android, you can directly start it with an Intent wherever you need
Refer toSteps 5~7
for details
- If you want to use your AIDL Service at the application layer, you can refer to the code below
Just use Intent to bind the AIDL service
Then you can directly operate the methods already implemented inside
For example:
eleAcesAPI.getUpdateList();
- If you want to use it in the
non-application layer
Here is ashared experience
: previously encountered a project that required defining the AIDL interface at the application layer
but the actual use was to be placed in the framework kernel layer (coincidentally, the project was doing OTA)
So if you are also
just need to open the AIDL interface
This way, you can complete it by just doingStep 3
Then see if the layer you want to implement supports AIDL
For example: in my encountered project, we were doing OTA ourselves
able to define a custom binder
So I wrote a service that connected to the AIDL interface using C#
Then just bind it at the app layer
Here, we used reflection to bind the service written in the kernel
Binding example:
- Here, because the project had some specific return value requirements
such as Calendar
but AIDL originally did not support this type
so I defined another abstract layer
When I implement it, I just need to remember to add logic in the middle
- At that time, the requirement was to directly build a jar file using groovy syntax
so that other processes could use it directly
You can do it like this: