无法实现@Parcelize 注解

问题描述

我正在尝试将 kotlin 中的 @Parcelize 注释实现到数据类,但看起来好像我忘记了实现某些东西。即使我尝试导入 kotlinx.parcelize,Android Studio 也不知道 parcelize 选项。我实现了 kotlin-parcelize,kotlin 版本是 1.4.21,应该没问题。

这是我的 Gradle 文件

plugins {
    id 'com.android.application'
    id 'kotlin-parcelize'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.benzeneapps.ciphers"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "0.0.1"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        viewBinding true
        dataBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation "androidx.core:core-ktx:1.3.2"

    implementation "androidx.room:room-ktx:2.2.6"
    kapt "androidx.room:room-compiler:2.2.6"
    androidTestImplementation "androidx.room:room-testing:2.2.6"

    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.2.0"

}
repositories {
    maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    mavenCentral()
}

项目 Gradle 文件

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我是如何尝试实现它的。

import kotlinx.parcelize.Parcelize
import android.os.Parcelable
@Parcelize
data class Cipher(val id: Int,val name: String): Parcelable

解决方法

kotlin-android之前申请kotlin-parcelize

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-parcelize'
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...