【问题标题】:Cannot implement @Parcelize annotation无法实现@Parcelize 注释
【发布时间】:2021-01-07 21:53:05
【问题描述】:

我正在尝试在 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

【问题讨论】:

  • 导入这条指令import kotlinx.android.parcel.Parcelize怎么样?
  • 该项目的 Gradle 版本是什么?我看到 Gradle 插件是 4.1.1,Gradle 是 6.5+?

标签: android kotlin parcelable


【解决方案1】:

kotlin-parcelize之前申请kotlin-android

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

【讨论】:

  • 如果我应用您的解决方案,我开始收到与 cocopods 插件相关的以下错误:Cocoapods Integration requires version of this project to be specified。请在项目的构建文件中添加行 'version = ""'。在带有 Adroid Studio 的 Windows 10 中是否有任何替代方案?
  • 我假设您正在谈论一些我不熟悉的适用于 Android 和 iOS 的多平台方法。我建议您提出一个包含更多详细信息的单独问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多