Last Updated:

What is AIDL in Android? With Code Examples

AIDL in Android

We continue the series of articles on services in Android. In past articles, we've learned about the main types of services, including an example of working with Messenger.

If you want to understand the full picture of how services work, check out the previous material before reading this article.

What is AIDL in Android?

AIDL is an interface detection language in Android. This powerful service-client communication mechanism can handle any task remotely using IPC (Interprocess Communication) technology. AIDL processes data between clients remotely and securely, but the prerequisite is that the client itself must communicate with these services.

How does AIDL work?

AIDL supports data processing using Parcelable. Everything happens in the IPC mechanism between two different applications.

Using AIDL via IPC in a Simple Example


Suppose we have two android applications: AppServer will act as a remote server running the AIDL service and the AppClient application – the client itself that sends requests to this remote server. What does an AIDL service do? It receives some data from the client, performs processing and returns the response/result to the client.

In this article, we'll look at AIDL with the following example:

A conditional difficult task: add two numbers.

Workflow: Using a client application, we send two numbers to the AIDL service and receive the result of the processing in response.

This is a simple task for such a powerful mechanism; in real applications, instead of adding numbers, it will download files, audio or video from the Internet, but this does not change the essence of the mechanism and method of implementation.

Example of working with AIDL in Android

To communicate between two different applications or processes, we need to create an AIDL file in the same package inside each of the applications in this we will be helped by Android Studio, which knows how to do this out of the box.

Go to Android Studio and create 2 applications named AppServer and AppClient.

Let's start with AppServer.

Change the project view from Android to Project:


Create an AIDL file with a name using the command: New -> AIDL -> AIDL fileISumNumsAIDL


Now let's create an AidlService that you want to inherit from the Service class.


As you can see, in the onBind method, we use a stub to implement our AIDL interface.

Stub is a class that implements a remote interface in such a way that you can use it as if it were local. It handles data marshaling and sending/receiving from a remote service. The term "Stub" or "stub" is commonly used to describe this function in other RPC methods (COM, Java RMI – Remote Method Call Programming Interface, etc.).


Now in the application-level build.gradle file, add the following code before the block's closing bracket:android {}


By doing so, we pointed out to the project builder that the AIDL files are in the aidl package of the main folder.

Also, do not forget to describe the service in the AndroidManifest file.xml inside the tag :application

This is the end of the AppServer application. We just created AIDL and an Android service that implements it. Let's start implementing the AppClient client application.

Creating a client application for an AIDL service

We need to access the service, send data for processing and get the result.

To do this, create the same aidl file in the same package as in the "server application". The structure of the project should look like this:

Importantly! Also in the build.gradle file at the application level, don't forget to add sourceSets for aidl just as we did in the first application.

Now we need to link our application and service using ServiceConnection. In the activity of the client application, we will initialize our interface in the method and at the start we will bind it using the AIDL service on the "server application" side.



It sounds complicated, but it is implemented quite simply, like many other things in Android development.


Throw a simple form for entering numbers with a send button:


Our MainActivity looks like this:


Now take your Android device or run the emulator and install first the "server app" to start the service to work with AIDL, and then the "client app". Result of the work:

As you can see, we managed to contact the service, send data and get the result of processing.

This was a simple example of working with AIDL on Android. App projects for Android Studio can be found here.