为什么升级到 jetpack compose 1.0.0-beta01 后没有隐式导入 kotlin.collections?

Why kotlin.collections is not implicitly imported after upgrading to jetpack compose 1.0.0-beta01?

升级到 jetpack compose 1.0.0-beta01 后,我尝试使用 kotlin.collections 中的 arrayListOf、listOf,但它们似乎没有隐式导入。

您的问题可能与您使用的Kotlin 版本有关。

I guess I went through a similar process as you did when I was updating to the new version of Jetpack Compose library, where as a "side effect" I was forced to update kotlin and kotlin-gradle-plugin version what then indirectly caused your (and mine) problem. Following workaround should fix it.

您很可能在将 Jetpack Compose 更新为 1.0.0-beta01 后使用 Kotlin 1.4.30。将 Kotlin 更新为固定 1.4.31 版本,您的问题将 ALMOST“已解决”。

I think the whole problem is somehow related to the following bug in 1.4.30 if you are more interested https://youtrack.jetbrains.com/issue/KT-44845

现在,在尝试构建您的项目后,您会收到一个很好的错误提示 This version (1.0.0-alpha13) of the Compose Compiler requires Kotlin version 1.4.30 but you appear to be using Kotlin version 1.4.31 which is not known to be compatible. Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

suppressKotlinVersionCompatibilityCheck 是一个编译参数,在我的例子中,我已经在 android -> kotlinOptions 下的模块 build.gradle 文件中这样设置:

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8
    useIR = true
        
    //here -->
    freeCompilerArgs += ["-P", "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"]
} 

It also depends on the type of build.gradle files. Read more about how to set those compile arguments at where are described different ways for groovy and also for kotlin based gradle files.

现在你应该没问题了,但请记住,一旦有新版本的 Jetpack Compose 依赖于更新版本的 Kotlin,你就想摆脱 suppressKotlinVersionCompatibilityCheck 参数。