AIDL interface can't find import for Parcelabl

2019-01-26 03:01发布

问题:

My issue seems to be similar to this question and this one but it is a bit different.

I am making an AIDL service that is in a Library project and using it in my application. I have a class Vehicle that is in my application that I have made parcelable so that I can use it in my interface. (I would like to get a List of vehicles from my service that is in the library to my application)

Do I need a Vehicle.java in both the application and the library?

Do I need a Vehicle.aidl in both?

I had Vehicle.java AND Vehicle.aidl in both application and library and I began running into a problem in my application that when I called a method from my interface eclipse wanted me to define it as the Vehicle of the library class and not the application(although they are the same and both parcelable).

public List<Vehicle> getVehicles(){...code... }

In an effort to resolve this, I tried to make it the application's vehicle class rather than the library's vehicle class in my IRemoteInterface.aidl(in the listed variation below, i get an error that it can't find the import. In other variations like having it be List and no import, it says unknown return type).

  package LIBRARY;


import LIBRARY.RelPoint;
import LIBRARY.IRemoteServiceCallback;
import LIBRARY.FleetStatus;
import APPLICATION.Vehicle;


interface IRemoteInterface {

    int getPid();

    void registerCallback(IRemoteServiceCallback callback);
    void unregisterCallback(IRemoteServiceCallback callback);

    List<Vehicle> getVehicles();

}

Here is my parcelable Vehicle class from the application :

package APPLICATION;

import java.util.Date;

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


public class Vehicle implements Parcelable {
    public static final String TAG = "Vehicle";

    long vehicleID;
    long trackID;
    String vehicleName;



     public static final Parcelable.Creator<Vehicle> CREATOR = new Parcelable.Creator<Vehicle>() {

            public Vehicle createFromParcel(Parcel src) {
                return new Vehicle(src);
            }

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

        };



        public Vehicle(Parcel src) {
            readFromParcel(src);
        }

        public void writeToParcel(Parcel dest, int flags) {
            dest.writeLong(vehicleID);
            dest.writeLong(trackID);
            dest.writeString(vehicleName);
        }

        public void readFromParcel(Parcel src) {
            vehicleID = src.readLong();
            trackID   = src.readLong();
            vehicleName = src.readString();

        }

        public int describeContents() {
            // nothing special
            return 0;
        }


    public Vehicle() {

    }

    //getter and setter methods below that I removed



}

回答1:

I'm reasonably certain you need to have the Parcelable classes in the same package on both ends (which is what you end up with by using the one from APPLICATION on the library side, I would just do it the other way around). This package has to also be the one declared in the corresponding aidl file.

I'd suggest to use a subpackage like com.example.interop to make this cleaner (i.e., separate the shared objects in their own package). Both sides should then have a Java file in that package + an aidl file that declares it.