Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

java - AIDL interface can't find import for Parcelable class

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



}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...