Upgrade from Kotlin 1.3.31 -> 1.3.40 breaks Android build

Viewed 5746

If I try to upgrade to the latest Kotlin version in my Gradle project, I get the following error building my Android subproject:

e: /(redacted)/AndroidLauncher.kt: (8, 15): Cannot access built-in declaration 'kotlin.Unit'. Ensure that you have a dependency on the Kotlin standard library

Downgrading back to 1.3.31 builds fine. I also tried upgrading my com.android.tools.build version from 3.4.0 -> 3.4.1, but no difference.

The error makes it pretty clear that there's trouble finding the Kotlin STL. Is there some new dependency I need to add or plugin I need to apply?

2 Answers

This behavior is the result of the recently fixed problem https://youtrack.jetbrains.com/issue/KT-19227. Previously, some built-in declarations like kotlin.Unit were loaded from the compiler internals when the compiled module didn't have a dependency on the Kotlin stdlib. Currently such a situation causes the build to fail, which is a less surprising behavior.

To make your project compile again, add the implementation(kotlin("stdlib")) dependency in your android subproject.

I don't know if it can help for someone, I've a similar problem but going from 1.3.40 to 1.3.61 (not from 1.3.31 to 1.3.40); with Android Studio in my situation it worked this workaround:

  • in Project build.gradle I've left kotlin version 1.3.61 and put gradle:3.5.0 instead of 3.6.1 which doesnt works. with the error in the title:

    
    buildscript {
        ext.kotlin_version = '1.3.61'
        ...
        dependencies {
             classpath 'com.android.tools.build:gradle:3.5.0'
             ...
        }
    
  • in Module build.gradle I've put apply plugin: 'kotlin-android' before apply plugin: 'kotlin-android-extensions'

Hope it works for others too.

Related