Sunday, April 14, 2013

Adding custom return type for marshalling/unmarshalling the data type in AIDL:

In Android application, while working with AIDL, I faced this problem.

If I am going to return any custom datatype or class in interface's functions, then we need to write .aidl and .java file.

For example from my AIDL interface function, I am going to return Rect custom class. Then I have to do the following:


Rect.aidl:
=========
package android.graphics;

// Declare Rect so AIDL can find it and knows that it implements
// the parcelable protocol.
parcelable Rect;


Rect.java
==============


import android.os.Parcel;
import android.os.Parcelable;

public final class Rect implements Parcelable {
    public int left;
    public int top;
    public int right;
    public int bottom;

    public static final Parcelable.Creator<Rect> CREATOR = new
Parcelable.Creator<Rect>() {
        public Rect createFromParcel(Parcel in) {
            return new Rect(in);
        }

        public Rect[] newArray(int size) {
            return new Rect[size];
        }
    };

    public Rect() {
    }

    private Rect(Parcel in) {
        readFromParcel(in);
    }

    public void writeToParcel(Parcel out) {
        out.writeInt(left);
        out.writeInt(top);
        out.writeInt(right);
        out.writeInt(bottom);
    }

    public void readFromParcel(Parcel in) {
        left = in.readInt();
        top = in.readInt();
        right = in.readInt();
        bottom = in.readInt();
    }
}

I have to include this Rect.java in android compilation make file [make];

we should not include the .aidl file in Android.mk for this scenario.

This can happen when we are using NDK compilation. In NDK case, we will include java/include files in Android.mk.

Error:

* in my Android.mk file, I have added:
LOCAL_SRC_FILES += \
        src/com/mycompany/mypackage/Rect.aidl \

But when we compile we will
Aidl: Test <= src/com/mycompany/mypackage//Rect.aidl
src/com/mycompany/mypackage/Rect.aidl:19 aidl can only generate
code for interfaces, not parcelables,
src/com/mycompany/mypackage/Rect.aidl:19 .aidl files that only
declare parcelables don't need to go in the Makefile.

Labels: , ,

Android Application AIDL error:

In android application, while working with AIDL, I observed an error.
My function is returning the class Employee. But I got the below error

Aidl: huey <= external/testapp/IMyService.aidl
external/testapp/Ioffice.aidl:16: couldn't find import for class com.sundar.Employee
make: *** [out/target/common/obj/JAVA_LIBRARIES/MyService_intermediates/src/com/IMyService.java] Error 1


IMyService.aidl

interface IOffice
{
 Employee get();
};


I have added the Employee.java which is derived from Parcelable class. I have also added this java file in make file.
But still I got this error.

How to resolve it:
I created the Employee.aidl and copied the below contents

package com.MyService;
parcelable Employee;

I copied the Employee.aidl and copied to the folder where Employee.java is there.Afterwards, it is compiling fine without any error.
 But Employee.aidl is not at all added in Android.mk

Labels: , , ,