14

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?

marcinm
  • 281
  • 1
  • 2
  • 11

7 Answers7

25

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.

Ercan
  • 2,601
  • 22
  • 23
  • I don't think this works anymore. `kotlin-parcelize` is not found by gradle. – Adam Apr 05 '22 at 21:10
  • Thank you for this suggestion! PS: Note that you should change the import if your previously used the kotlin-extension to "import kotlinx.parcelize.Parcelize" – Atte Backenhof Oct 05 '22 at 12:21
23

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'
HuyTTQ
  • 388
  • 3
  • 7
6

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.

es0329
  • 1,366
  • 1
  • 18
  • 35
  • 1
    Thank you for posting this workaround. This also works: https://github.com/gradle/kotlin-dsl/issues/644#issuecomment-454594727 There is also bug filed at https://youtrack.jetbrains.com/issue/KT-22213 to fix this issue. – marcinm Jan 19 '19 at 17:02
1

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

id 'kotlin-android-extensions'

0

https://github.com/gradle/kotlin-dsl-samples/issues/644#issuecomment-482479624 - As commented on this link,

androidExtensions { isExperimental = true }

works fine for me when I upgraded the Kotlin version to the latest one i.e. 1.3.60 and no need to do any workaround.

Srushti
  • 131
  • 7
  • Throws error : `Could not set unknown property 'isExperimental' for object of type org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension.` – Ramakrishna Joshi Dec 13 '19 at 03:49
  • 1
    @RamakrishnaJoshi are you using kotlin DSL for gradle? and have you upgraded kotlin version to the latest as mentioned above? – Srushti Dec 14 '19 at 14:44
  • My bad, I am using a separate module for network calls which is independent of Android. So can't use androidExtensions { isExperimental = true } there. Your answer is not an issue. – Ramakrishna Joshi Dec 16 '19 at 06:29
0

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

Rohan Pawar
  • 1,875
  • 22
  • 40
0

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.

Devrath
  • 42,072
  • 54
  • 195
  • 297