【问题标题】:This version (1.1.1) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.5.31Compose 编译器的此版本 (1.1.1) 需要 Kotlin 版本 1.6.10,但您似乎使用的是 Kotlin 版本 1.5.31
【发布时间】:2022-07-28 05:56:10
【问题描述】:

我正在使用最新的 Android Studio,并且我能够将 compose_version 设置为 1.0.5 来构建和运行我的应用程序。但是,我想使用最新的稳定 compose 版本1.1.1

我尝试简单地更新项目build.gradle,使其包含以下指向所需的compose版本和相应的兼容kotlin版本。这些值在应用的build.gradle 中引用。

buildscript {
    ext {
        compose_version = '1.1.1'
        kotlin_version = '1.6.10'
    }

在 Android Studio 中,我转到工具 > Kotlin > 配置 Kotlin 插件更新并下载最新的 Kotlin 插件(抢先体验)。

如果我打开工具 > Kotlin > Kotlin REPL,我会看到 Welcome to Kotlin version 1.7.0-RC2-release-258 (JRE 11.0.12+0-b1504.28-7817840)

现在,我尝试重建项目。

我得到错误: This version (1.1.1) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.5.31 which is not known to be compatible. Please fix your configuration (or suppressKotlinVersionCompatibilityCheck but don't say I didn't warn you!).

鉴于警告,我不希望 suppressKotlinVersionCompatibilityCheck,但我什至尝试了该选项并得到其他构建错误。

为什么要使用 Kotlin 版本 1.5.31?更新 Kotlin 插件是否应该让 Android Studio 切换到更新的 Kotlin 版本(如 Kotlin REPL 消息所建议的那样)?我怎样才能使它使用 Kotlin 1.6.10 并且我停止收到错误?

【问题讨论】:

  • 运行 ./gradlew app:dependencies 以查看 1.5.31 依赖项来自何处。 this guide 也有帮助 - 寻找 resolutionStrategy
  • 检查buildscript依赖classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:x.x.xx"

标签: android-studio kotlin android-jetpack-compose


【解决方案1】:

Compose 使用在 buildscript 块中定义的 kotlin 编译器:

buildscript {
   ext.kotlin_version = '1.6.10'
   //....
   dependencies {
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }
}

如果您在settings.gradlebuild.gradle 中使用plugins 块:

pluginManagement {

    plugins {
        id 'org.jetbrains.kotlin.android' version '1.6.10' 
    }
}    

【讨论】:

  • 更新应用程序 build.gradle 中的 buildscript 块似乎可以解决问题。但是,对plugins 块的更改导致了构建错误。我最终在plugins 块中使用了id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
  • @blu 如果您使用的是 buildscript 块,则不需要使用 plugins 块
【解决方案2】:

使用这个帮助我。

build.gradle (:app)

composeOptions {
    kotlinCompilerExtensionVersion = "1.2.0-beta03"
}

【讨论】:

  • 但这不是一个稳定的版本。
  • 还需要设置org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21
【解决方案3】:
buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.3'
    }
}

plugins {
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

和 build.gradle(Module)

composeOptions {
    kotlinCompilerExtensionVersion '1.1.1'
}

【讨论】:

    【解决方案4】:

    在使用 Compose 1.1.1. 和 Kotlin 1.7.0 时遇到同样的错误。更改了我的 build.gradle (app module)

    中的以下块
    composeOptions {
        kotlinCompilerExtensionVersion 1.2.0
    }
    

    这是我在 build.gradle(项目)中的插件块:

    plugins {
        id 'com.android.application' version '7.2.1' apply false
        id 'com.android.library' version '7.2.1' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
        id 'org.jetbrains.kotlin.jvm' version '1.7.0' apply false
    }
    

    这个 Android 开发者页面有助于选择兼容版本:-

    Compose to Kotlin Compatibility Map

    别忘了同步和重建。

    【讨论】:

      猜你喜欢
      • 2022-12-23
      • 2023-01-16
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      相关资源
      最近更新 更多