1

我正在尝试在 Android 平台上使用 kotlin android 扩展插件 Parcelize。

我在 Kotlin 1.3.72 上。

我在没有插件的情况下成功地生成了一个单一的哑 Rect.java(通过手动实现 parcelable 方法)但是这对于很多aidl对象来说有点烦人,特别是因为 Kotlin 有一个代码生成插件可以为你自动完成。

使用@Parcelize 注释时出现以下错误:

错误:类 'Rect' 不是抽象的,也没有实现抽象成员 public abstract fun writeToParcel(dest: Parcel!, flags: Int): Unit defined in android.os.Parcelable

你能告诉我哪里错了吗?

遵循带(工作)和不带(不工作)注释的类 Rect 以及 Android.bp 文件:

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

class Rect() : Parcelable {
    var left: Int = 0
    var top: Int = 0
    var right: Int = 0
    var bottom: Int = 0

    companion object CREATOR : Parcelable.Creator<Rect> {
        override fun createFromParcel(parcel: Parcel): Rect {
            return Rect(parcel)
        }

        override fun newArray(size: Int): Array<Rect> {
            return Array(size) { Rect() }
        }
    }

    private constructor(inParcel: Parcel) : this() {
        readFromParcel(inParcel)
    }

    override fun writeToParcel(outParcel: Parcel, flags: Int) {
        outParcel.writeInt(left)
        outParcel.writeInt(top)
        outParcel.writeInt(right)
        outParcel.writeInt(bottom)
    }

    private fun readFromParcel(inParcel: Parcel) {
        left = inParcel.readInt()
        top = inParcel.readInt()
        right = inParcel.readInt()
        bottom = inParcel.readInt()
    }

    override fun describeContents(): Int {
        return 0
    }
}

import kotlinx.android.parcel.Parcelize
import android.os.Parcelable

@Parcelize
class Rect() : Parcelable {
    var left: Int = 0
    var top: Int = 0
    var right: Int = 0
    var bottom: Int = 0
}

java_library {
    name: "my_module",
    srcs: ["src/main/java/**/*.java", "src/main/java/**/*.kt", "src/main/aidl/**/I*.aidl"],
    java_version: "1.8",
    aidl: {
        local_include_dirs: ["src/main/aidl"],
    },
    static_libs: [
        "kotlin-android-extensions"
    ],
}


java_import {
    name: "trove4j",
    jars: ["trove4j-1.0.20200330.jar"],
    host_supported: true,
    sdk_version: "current",
}

java_import {
    name: "kotlin-daemon-embeddable",
    jars: ["kotlin-daemon-embeddable-1.3.72.jar"],
    host_supported: true,
    sdk_version: "current",
}

java_import {
    name: "kotlin-script-runtime",
    jars: ["kotlin-script-runtime-1.3.72.jar"],
    host_supported: true,
    sdk_version: "current",
}

java_import {
    name: "kotlin-compiler-embeddable",
    jars: ["kotlin-compiler-embeddable-1.3.72.jar"],
    libs: ["kotlin-stdlib", "kotlin-reflect", "kotlin-script-runtime", "kotlin-daemon-embeddable", "trove4j"],
    host_supported: true,
    sdk_version: "current",
}

java_import {
    name: "kotlin-android-extensions",
    jars: ["kotlin-android-extensions-1.3.72.jar"],
    libs: ["kotlin-compiler-embeddable"],
    host_supported: true,
    sdk_version: "current",
}
4

0 回答 0