androidkotlinparcelable

Cannot implement @Parcelize annotation


I am trying to implement @Parcelize annotation in kotlin to a data class, but it looks as if I forgot to implement something. Even when I try to import kotlinx.parcelize, Android Studio does not know the parcelize option. I implemented kotlin-parcelize and kotlin version is 1.4.21, which should be okay.

Here are my Gradle files:

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()
}

Project Gradle File

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
}

How I tried to implement it.

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

Solution

  • Apply kotlin-android before kotlin-parcelize:

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