我可以在哪里放置 `suppressKotlinVersionCompatibilityCheck` 标志?

Where can I put the `suppressKotlinVersionCompatibilityCheck` flag?

我正在尝试使用 kotlin 的 1.4.21-2 版本,这是一个最新版本,它有一个修复程序,允许您使用 Compose + Kotlin 序列化而不会挂起构建。这一切都很好,但是,Compose 编译器不知道它并给出了以下(相当无用的)错误:

e: This version (1.0.0-alpha09) of the Compose Compiler requires Kotlin version 1.4.21 but you appear to be using Kotlin version 1.4.21-2 which is not known to be compatible. Please fix your configuration (or suppressKotlinVersionCompatibilityCheck but don't say I didn't warn you!).

我很想提供那个抑制标志,但是我不知道把它放在哪里......我花了大约一个小时试图把它放在我的 gradle 文件中的随机位置,例如在 composeOptions 中,但没有运气。我也尝试了所有我知道的 google-fu,但似乎没有人真正使用过它并写过任何关于它的东西。

有什么办法可以摆脱困境吗?

这是一个命令行参数。

看到这个example in a kts file here

-P plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true

我对消息有同样的问题:

e: This version (1.0.0-alpha11) of the Compose Compiler requires Kotlin version 1.4.21-2 but you appear to be using Kotlin version 1.4.21 which is not known to be compatible.  Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

添加编译器参数解决了我的问题:

"-P", "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"

您可以将其添加到所有 KotlinCompile 任务中。在应用级别 Gradle 中,它看起来如下所示:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs += [
                "-Xallow-jvm-ir-dependencies",
                "-P",
                "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        ]
    }
}

如果您使用的是 KTS,

android {
    ...

    kotlinOptions {
        jvmTarget = "1.8"
        useIR = true

        freeCompilerArgs += listOf(
            "-P",
            "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        )

    ...
}

您也可以随时简单地将 Kotlin Gradle 插件降级到它声明的版本,即:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    val compose_version by extra("1.0.0-beta07")

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.0-beta02")
        
        // This is the line you want to change to the needed version
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
    }
}

如果您正在使用 Gradle Grovy 脚本 (build.gradle),

    kotlinOptions {
        jvmTarget = "1.8"
        useIR = true

        freeCompilerArgs += [
                "-P",
                "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        ]
    }

对于多模块项目,将抑制标志添加到 allprojects {} 部分内项目的根级别 build.gradle

allprojects {
    repositories {
        google()
        mavenCentral()
    }

    // Suppress Compose Kotlin compiler compatibility warning
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            freeCompilerArgs += [
                    "-P",
                    "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
            ]
        }
    }
}

我遇到了同样的问题,我更新了兼容的最新版本 这样

Right click 'app' module > open module setting > suggestions > update the 
compose dependencies. 

更新并应用。