Reputation: 1435
A simple question...how do you create AIDL files in an Android project using Eclipse? For example, if I try to select File -> New -> Other...
, there is no option for ADT to create an AIDL. An AIDL is more or less an interface, but if I create an interface (class), the file is generated with a .java extension. In Eclipse, I can't simply refactor the class to give it an .aidl extension.
Maybe I'm relying on ADT too much for a task that is so trivial, but I'm surprised to not see an easy way to create an AIDL file. Of course, I can also just create a new file and give it its own extension, but then the AIDL file doesn't have automatic syntax highlighting.
Upvotes: 2
Views: 8254
Reputation: 301
First let me explain what AIDL file is :
Every Android application runs in its own process. So one application cannot access another application's memory space OR in other words, one process cannot access memory of other process. So to let this happen we need to decompose their objects into primitives that the operating system can understand and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL - meaning, Inter Process communication (IPC) can be handled easily through AIDL. For better understanding, view this as a communication between client and server applications. Client sends a request and server responds back. This tutorial will explain this inter-process communication in simple manner with clear example and source code. To make our example simple, lets take calculation request. Client application will get two numbers from the user and sends to the server application. Server application will perform addition of these numbers and returns the sum to the client application.
AIDL can be achieved following the below steps : Define AIDL Interface - AIDL that Interfaces your applications. Implement the Interface - The above interface will be implemented in the service so that clients can access. Expose the Interface to clients- To expose the interface for your service, extend Service and implement onBind()
Define AIDL Interface This file defines programming interface with method signature. AIDL interface should be defined in a .aidl file. In our case its IAdd.aidl with the below code.
IAdd.aidl
package com.example.android_additionservice;
interface IAdd
{
int add(int num1, int num2);
}
When we build our application, Android SDK generates .java file corresponding to this .aidl file with the same name like IAdd.java. The generated interface includes a subclass named Stub that is an abstract implementation of its parent interface and declares all the methods from the .aidl file
Implement the Interface (AdditionService.java) To implement the interface generated from the .aidl, extend the generated Binder interface and implement the methods inherited from the .aidl file. On our case we need to implement the add method here. Now the mBinder is an instance of the Stub class (a Binder), which defines the RPC interface for the service.
private final IAdd.Stub mBinder = new IAdd.Stub() {
@Override
public int add(int num1, int num2) throws RemoteException {
return (num1 + num2);
}
};
Expose Interface to Clients (AdditionService.java) After implementing the interface, we need to expose it to other applications to access it. For which extend service and implement onBind() to return an instance of your class that implements the generated stub.
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
AdditionService.java
package com.example.android_additionservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import com.example.android_additionservice.IAdd;
public class AdditionService extends Service {
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
// IAdd definition is below
private final IAdd.Stub mBinder = new IAdd.Stub() {
@Override
public int add(int num1, int num2) throws RemoteException {
// TODO Auto-generated method stub
return (num1 + num2);
}
};
}
Register Service in AndroidManifest.xml Since the server application is a service, lets register this in the AndroidManifest.xml file.
Thats it.. We are done with the server application.
Upvotes: 1
Reputation: 782
right click the package -> new -> File
then enter your interfacename together with your aidl extension so for example:
myEclipseAIDLInterface.aidl
Upvotes: 5