@Parcelize not resolved with androidExtensions experimental mode set to true

Viewed 9383

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?

7 Answers

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'

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.

Ah! I located a working solution. Set the experimental flag in a Groovy-based androidExtensions.gradle then apply that file in your Kotlin-based build.gradle.kts.

I wanted to pass it along but credit goes to toxxmeister.

Add this Plugin in your build.gradle(app) file.

id 'kotlin-android-extensions'

For Kotlin KTS

this solution worked for me

androidExtensions {
    configure(delegateClosureOf<AndroidExtensionsExtension> {
        isExperimental = true
    })
}

My build.gradle.kts file

plugins {
    id(Dependencies.androidApplication)
    id(Dependencies.google)
    id(Dependencies.kotlinAndroid)
    id(Dependencies.kotlinAndroidExtensions)
    id(Dependencies.kapt)
    id(Dependencies.crashlytics)
}

androidExtensions{
    configure(delegateClosureOf<org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension> {
        isExperimental = true
    })
}

android {
......
}

for more details https://github.com/gradle/kotlin-dsl-samples/issues/644#issuecomment-398502551

Make sure you have this import in your list of imports

import kotlinx.parcelize.Parcelize

In my case even though I had plugins applied as mentioned in the answers, Android studio was importing the wrong plugin.

Related