Android applications are built from 4 types of components:

  1. Activity – Screen (code and GUI)

  2. Service – independent code supplying something

  3. Content Provider – Data service

  4. Broadcast Receiver – component for global events handling

The developer can create multiple components from each type and each one can be an entry point.

In this tutorial i will go over the steps to create and use an android service

To use a service from a client application (2 separate APKs) we are using binder IPC. The Binder is an IPC mechanism built into the kernel (as character device). In the native layer google wrote the libbinder library and with help of AIDL language and tool it make the binder very easy to use

AIDL – Android Interface Definition Language

To create a service we need to define an interface between the client and the server. AIDL is the way we do that , simply add a file with .aidl extension and the AIDL tool supplied with Android SDK will generate a code for using in the server (stub) and the client(proxy).

The best way to build client and server applications is to create the common code in a library

  • Create an Android Studio project – select “phone or tablet” template no activity – this will be the server application for hosting our services

  • From file menu select new -> New module and select Android Library. Name the library mylib

  • Add a new AIDL file to the library – right click the library and select new -> AIDL -> AIDL file. Name the file ISimpl.aidl

  • Declare a simple interface:

interface ISimp {
 int add(int a,int b);
 int sub(int a,int b);
  • Build the module – from the build menu select make module mylib

The generated code

If you change the view project files and go to mylib/build/generated/source , you will found a generated java file with the generated code from your AIDL

The generated code looks like this: (I removed the inner methods to simplify the explanation)

public interface ISimp