无法在 aidl 中使用自定义 类

Can't use custom classes in aidl

我有两个应用程序通过 aidl 接口进行通信。当我使用 String 或 int 等基本类型时一切正常,但是根据 this 我应该也可以使用自定义类型。但是代码(甚至非常简化)在我的例子中无法编译

这是它的样子:

app/src/main/aidl/edu/cat/ion/TestClass.java:

package edu.cat.ion;

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

public final class TestClass implements Parcelable {

    public TestClass() {
    }

    protected TestClass(Parcel in) {
    }

    public static final Creator<TestClass> CREATOR = new Creator<TestClass>() {
        @Override
        public TestClass createFromParcel(Parcel in) {
            return new TestClass(in);
        }

        @Override
        public TestClass[] newArray(int size) {
            return new TestClass[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    }
}

app/src/main/aidl/edu/cat/ion/TestClass.aidl:

package edu.cat.ion;

parcelable TestClass;

此时代码可以编译,但是在其他一些 class 我只是这样做:

import edu.cat.ion.TestClass;

我得到:

error: cannot find symbol

import edu.cat.ion.TestClass;

你知道什么是错误的吗?

您看到错误是因为 TestClass.java 文件的路径不匹配。

目前您的 TestClass.java 位于其他 java 类 无法找到的 aidl 文件夹下。

因此移动您的

app/src/main/aidl/edu/cat/ion/TestClass.java

文件到 java 文件夹。

app/src/main/java/edu/cat/ion/TestClass.java