Search code examples
androidkotlin-android-extensionsgradle-kotlin-dsl

@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?


Solution

  • 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.