AIDL : IPC, The Android way

The Android Interface Definition Language (AIDL) is similar to other IDLs: it lets you define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).

Aidl consists of 3 parts:

  1. AIDL file

  2. Client application

  3. Server application

Creating AIDL file

  1. Enable build feature in your build.gradle.

     buildFeatures {
         aidl = true
     }
    
  2. Create a new AIDL file using File -> New -> AIDL -> AIDL File

  3. Let's name it ICalculator.aidl

  4. Declare aidl methods:

     interface IExampleAidlInterface {
         int add(int a, int b);
     }
    

Create an AIDL server

An AIDL server is an app that does the actual work and sends back the result to requesting clients. One service can connect with multiple clients via Binders.

Steps:

  1. Create a service, e.g. CalculatorService extends Service

  2. Create an Implementation of ICalculator.Stub, lets name it CalculatorImpl

  3. Implement methods:

     object CalculatorImpl : ICalculator.Stub() {
         override fun add(a: Int, b: Int): Int {
             return a + b
         }
     }
    
  4. Expose your stub implementation with a service binder:

     class CalculatorService : Service() {
         override fun onBind(intent: Intent): IBinder {
             return CalculatorImpl
         }
     }
    

Now your service is ready to be consumed by your clients, now let's create a client application.

Create an AIDL client

An AIDL client is an app that binds to an AIDL service, calls API, and shows a response inside its views.

Steps:

  1. Create a service connection:

     private var aidlService: ICalculator? = null
     private val serviceConnection = object : ServiceConnection {
         override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
             aidlService = ICalculator.Stub.asInterface(service)
             Toast.makeText(applicationContext, "Service Connected", Toast.LENGTH_LONG).show()
         }
    
         override fun onServiceDisconnected(name: ComponentName?) {
             aidlService = null
             Toast.makeText(applicationContext, "Service Disconnected", Toast.LENGTH_LONG).show()
         }
     }
    
  2. Connect to AIDL service via Binders.

     val serviceIntent = Intent()
     serviceIntent.component = ComponentName(BuildConfig.AIDL_PACKAGE, BuildConfig.AIDL_SERVICE)
     bindService(serviceIntent, serviceConnection, BIND_AUTO_CREATE)
    
  3. Call methods of AIDL after successful connection:

     aidlService?.add(2, 2)?.let {
         Toast.makeText(
             applicationContext,
             String.format(Locale.getDefault(), "Sum is: %d", it),
             Toast.LENGTH_LONG
         ).show()
     }
    

Limitations

There is one BIG gotcha with AIDL services, which is when the system is in doze mode, your service app might not start to respond to requests.


Here is an example application using AIDL in real life ๐Ÿ˜Ž

0
Subscribe to my newsletter

Read articles from Tej Pratap Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tej Pratap Singh
Tej Pratap Singh

Code, Movies and code for movies