1. Open AIDL in build.gradle under the project app directory
Taking build.gradle.kts as an example
android {
....
//Enable AIDl
buildFeatures {
aidl = true
}
}
2. Create the AID file
1. Create an AIDEL folder in app/src/main, download the AIDL compressed file, and extract it into the AIDL directory (full path is AIDL.cloud.API.server)
3. Connect the AID service in the app project
Using kotiln code as an example
1. Create a service service
val mControl = object : ServiceConnection {
override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
LogUtils.d("Created AIDL")
control = ControlInterface.Stub.asInterface(p1)
MyAidlManager.setControlAidl(control)
}
override fun onServiceDisconnected(p0: ComponentName?) {
LogUtils.d("Disconnect the connection")
//After disconnecting, it reconnects
GlobalScope.launch {
delay(3000)
startMyService()
}
}
}
val mRoot = object : ServiceConnection {
override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
LogUtils.d("Created AIDL")
root = RootInterface.Stub.asInterface(p1)
MyAidlManager.setRootAidl(root)
}
override fun onServiceDisconnected(p0: ComponentName?) {
LogUtils.d("Disconnect the connection")
//After disconnecting, it reconnects
GlobalScope.launch {
delay(3000)
startMyService()
}
}
}
2.Set up a globally available utility
object MyAidlManager {
var control : ControlInterface? = null
var root : RootInterface? = null
fun setControlAidl(control : ControlInterface?){
this.control = control
}
fun setRootAidl(root : RootInterface?){
this.root = root
}
}
3.Bind the AID service
fun startMyService(){
val intent = Intent("aidl.cloud.api.ControlServer")
intent.setPackage("com.cloud.rtcgesture")
bindService(intent, mControl, BIND_AUTO_CREATE)
val intent2 = Intent("aidl.cloud.api.RootServer")
intent2.setPackage("com.cloud.rtcgesture")
bindService(intent2, mRoot, BIND_AUTO_CREATE)
}
4.Service shutdown
override fun onDestroy() {
super.onDestroy()
unbindService(mControl)
unbindService(mRoot)
}
5.Launch service
startMyService()
4. Add adaptation under the AndroidManifest.xml in the app directory
<manifest>
...
<queries>
<package android:name="com.cloud.rtcgesture" />
</queries>
</manifest>
AIDL File Download
[Click to download (aidl.zip)] (/aidl.zip)