The previous two pieces of article explain the Android IPC through Aidl and Messenger two ways. This article is not about the third IPC, but the first two ways to encapsulate, so that we do not have to directly copy the Aidl file, Java files to the client, but to provide the client with an AAR (anroid Archive) package. By encapsulating the Aidl or messenger through this AAR package, the client can end up calling the generic Java class without having to deal with the communication content. (In fact, it was said to be packaged as a jar package, but in the new Java Library module, I failed to create the Aidl file successfully, and finally I chose AAR.) How to create an AAR please read: Building an AAR Library in Android Studio).
This article does not explain the code, if not clear, please read first:
Android programming:pushing The Limits--Chapter 7:android IPC--Aidl
Android programming:pushing The Limits--Chapter 7:android IPC--Messenger
The sample code for this article:
Service:https://github.com/ldb-github/wrappedaidlservice
Client:https://github.com/ldb-github/wrappedaidlclient
Service side:
The project consists of two module:
The app module is where the service resides.
The Wrappedaidl module is an Android Library that eventually generates an AAR package that is provided to the client. This module actually encapsulates the code that originally communicates with the server in the client.
This project contains two ways of Aidl and Messenger. which
APIWrapper is the encapsulation of the Aidl way.
Apiwrapperbymessenger is the encapsulation of Messenger mode.
In order to be able to callback the client, add a callback interface Apicallback,apiwrapper or Apiwrapperbymessenger will call the appropriate callback method at different times of the communication to pass the message to the client.
Client:
This allows the client to add the AAR package instead of directly copying the server's files:
You also have to modify the module's Build.gradle file:
This allows apicallback,apiwrapper and apiwrapperbymessenger to be used on the client.
Client UI:
Android programming:pushing The Limits--Chapter 7:android IPC--APIWrapper