Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Parcelize not resolved with androidExtensions experimental mode set to true

After switching to Gradle Kotlin DSL Gradle is not able to resolve @Parcelize annotation or package import kotlinx.android.parcel.Parcelize ("Unresolved reference" error). That happens with stable Kotlin plugin and latest Canary one. Build fails in Android Studio and also from console when using Gradle Wrapper.

Top level build.gradle.kts

buildscript {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.2.1")
        classpath(kotlin("gradle-plugin", version = "1.3.11"))
    }
}

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

App build.gradle.kts

import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
    id("com.android.application")
    kotlin("android")
    kotlin("android.extensions")
}

android {
    compileSdkVersion(28)
    defaultConfig {
        applicationId = "com.a.b.c"
        minSdkVersion(15)
        targetSdkVersion(28)
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
        }
    }

    // experimental mode is enabled -> @Parcelize should be resolved
    androidExtensions {
        isExperimental = true
    }
}

dependencies {
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
    ... other dependencies ...

I tried moving

    androidExtensions {
        isExperimental = true
    }

from android to top level but still outcome was the same. Anyone had similar issue and managed to solve this?

like image 481
marcinm Avatar asked Jan 12 '19 09:01

marcinm


People also ask

How to configure Android fragments with @parcelize?

Configuring Android Fragments with @Parcelize 1 Parcelable. Parcelable is an Android interface that allows you to serialize a custom type by manually writing/reading its data to/from a byte array. 2 Kotlin Android Extensions. ... 3 Combining @Parcelize and Fragments. ...

How to implement parcelable with Kotlin-Android-extensions?

With kotlin-android-extensions we can implement Parcelable in much simpler way using @Parcelize and Parcelable. Android extensions are not considered production-ready yet, so we need to turn experimental mode on in app's build.gradle file

How to add @parcelize annotation to a model in Android?

You also need to add apply plugin: ‘kotlin-android-extensions’ to your gradle file. However, if you have created your project using Kotlin support, it should be added automatically. Then add @Parcelize annotation to your model. That’s it!

Why is @parcelize annotation not working after switching to Gradle Kotlin DSL?

After switching to Gradle Kotlin DSL Gradle is not able to resolve @Parcelize annotation or package import kotlinx.android.parcel.Parcelize ("Unresolved reference" error). That happens with stable Kotlin plugin and latest Canary one. Build fails in Android Studio and also from console when using Gradle Wrapper.


2 Answers

All you need to do is add below plugin to your app level gradle file

apply plugin: 'kotlin-parcelize'

PS: apply plugin: 'kotlin-android-extensions' is deprecated.

like image 105
Ercan Avatar answered Sep 19 '22 12:09

Ercan


In my case. Just swap 2 line of code kotlin-android and kotlin-android-extensions then it work.

So the sequence below is work:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
like image 30
HuyTTQ Avatar answered Sep 21 '22 12:09

HuyTTQ