如何解决无法实现@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'
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。