isCoreLibraryDesugaringEnabled not works in gradle kotlin dsl / kts

6.4k views Asked by At

To enable desugaring in our android-library module we must put this in build.gradle:

android {
  compileOptions {
    coreLibraryDesugaringEnabled true
  }
}

But we have all scripts migrated to gradle kotlin dsl, so the problem occurs in build.gradle.kts in all three ways:

android {
    compileOptions {
        isCoreLibraryDesugaringEnabled = true
    }
}
configure<BaseExtension> {
    compileOptions {
        isCoreLibraryDesugaringEnabled = true
    }
}
android {
    if (this is com.android.build.api.dsl.LibraryExtension<*, *, *, *, *, *, *, *, *, *, *>) {
        buildFeatures.viewBinding = true
    }
}

Every time it throws Unresolved reference: isCoreLibraryDesugaringEnabled.

Does anybody have an idea how to fix this?

2

There are 2 answers

2
Krystian Kaniowski On BEST ANSWER

When I switch to the newer android plugin version (4.1.0-rc02) it theoretically works. IDE says it's bad syntax, but it works during compilation.

if (this is com.android.build.api.dsl.LibraryExtension<*, *, *, *, *>) {
    compileOptions.isCoreLibraryDesugaringEnabled = true
}

However, it's not an ideal solution

----- FINAL SOLUTION -----

Solution is similar to How to exclude (ignore) android build variants in gradle kts

It wasn't work because of missing line in top-level build.gradle.kts this line:

classpath("com.android.tools.build:gradle:4.0.1")

1
Saurabh Thorat On

If you're using Android Gradle Plugin version >= 4.1, use:

isCoreLibraryDesugaringEnabled = true

For versions before that, use:

coreLibraryDesugaringEnabled = true