AndroidPub
Published in

AndroidPub

Android AIDL

Have you ever had 2 apps which have same logic or have same database values and your thought would be “Man, Is there any way I can copy my existing data to new app??”. Well, good news for you, Android provides a way to share data between your existing app and the new app to share data .

So. Let’s get started with AIDL (Android Interface Definition Language[not really a separate language]).

Overview

● Works like client & server application.

● Client app initiate some specific action, pass some data/request to server app for process, Server app process the data/request passed by client and return the appropriate result.

● Possible to pass custom object across apps.

Simple Data Passing (Primitive Type data)

● You can send any primitive data across client and server app without any other hassle.

To use AIDL we have to make 2 apps in which one will act as server and another as client which will access remote methods in server app.

Getting Started with server app

● Create one server project (Normal project which acts as serving to another app).

● Add empty activity which act as launching activity

● Add one AIDL file.

● AIDL file defines programming interface with method signature. AIDL interface should be defined in a “.aidl” file

● Here we have 2 methods one is addNumbers with 2 arguments which will return int data and List with string type with no argument. Both methods will be implemented in class AdditionService.java class.

● Make one java file in this case it is AdditionService.java which extends Service class and implement your interface with your method implementation. Here Stub is an automatically generated class by android for you and you might need to rebuild project several times if needed.

● Server’s AndroidManifest.xml File

● You must declare your service in manifest file and make process to .remote and give unique name to action in intent-filter through which your client application will call your service.

Creating Client app

● Here for just for explaining the concept create your basic UI with number input for addition and displaying result.

● Make one package with same name as your server app’s package name in which your AIDL file resides and in that put same “.aidl” files of server.

● Make object of your interface in this case IAdd and ServiceConnection class.

● Make one function in MainActivity, initConnection()

● That’s it you just have to perform calling function like any other function.

This procedure is for simple and primitive types.

User defined object passing

● Android AIDL also supports user defined object passing.

● For passing user defined objects you have to make some changes in both client and server app.

Server App changes

● Make your model class and implements it with Parcelable.

● The Parcelable interface have one method Creator<T> this method you need to override. Maybe it is possible Android studio will do the job for you

● Make another “.aidl” file with same name as class name and in the same package where other “.aidl” files resides.

DO NOT WRITE ANYTHING EXCEPT FOLLOWING LINE in custom class “.aidl”

parcelable “Your class name”

// personAIDL.aidl
package com.chintan.aidlserver;
parcelable Person;

● Now come to your main “.aidl” file and do the following change.

● Write import statement for your custom class
Import “package name.classname”

At last insert your function name which connects to your custom class in interface

Client App changes

● Make exact same copy of the model class.

● Copy same custom class’s “.aidl” file to client’s aidl folder without any change.

● Do the same import statement for your client’s main “.aidl” as server.

● Make the appropriate call to get your data.

Method to run

● First Install server application in your phone or emulator.

● Install client application in your phone or emulator.

Output

You might be wondering who uses that. Well, If you have used In-app billing or subscription this is where Google uses the IInAppBillingService.aidl which you needs to integrate in your project. All the steps are well described in doc.

References

Official Documentation

Simple Data passing (primitive data type)

Complex Data Passing (User Defined type)

Github project

--

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Recommended from Medium

Implementing Limited Ammo Feature in our 2D Galaxy Shooter Game in Unity — Part 02!

Jmeter: How to upload multiple Images by Apache Jmeter?

Eagle Eye Infrastructure Overview

How ansible helps in solving challenges faced by big industries?

Build a Ruby .DevContainer for Development

Useful Git commands that might save your life

Add Kubernetes users with Ansible

API Manage with Google ESP + Google Endpoints + Ingress in Kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chintan Desai

Chintan Desai

Be Creative, Be Productive, Android Developer

More from Medium

Video and Audio in Android

RecycleView Kotlin — Android Studio

BookLogApplication: Connecting the dots with more logic

Localized Android Strings for any UI