You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Kotlin 1.9的Android项目为何提示需Compose Compiler Gradle插件?

问题

我正在使用Kotlin 1.9.0开发Android项目,构建时遇到如下错误:

Starting in Kotlin 2.0, the Compose Compiler Gradle plugin is required when compose is enabled. See the official Android documentation for more information.

项目的build.gradle文件片段如下:

allprojects {
buildscript {
    ext.kotlin_version = '1.9.0'
    repositories {
        google()
        mavenLocal()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        maven { url 'https://developer.huawei.com/repo/' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.5.2'
        classpath "io.realm:realm-gradle-plugin:10.18.0"
        classpath 'com.google.gms:google-services:4.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.20"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.2'
        classpath 'com.huawei.agconnect:agcp:1.9.1.303'
        classpath 'com.huawei.hms.plugin:analytics:5.0.1.300'
        classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.8.0'
    }
}
}

android {
   compileOptions {
    coreLibraryDesugaringEnabled true
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
}

buildFeatures {
    dataBinding = true
    compose = true
}

composeOptions {
    kotlinCompilerExtensionVersion "1.5.14"
}
}

明明项目声明使用Kotlin 1.9.0,且已在composeOptions中设置kotlinCompilerExtensionVersion为"1.5.14",为何会出现这个针对Kotlin 2.0的错误提示?

解决分析
  • 核心原因:Kotlin Gradle插件版本与声明的Kotlin版本不一致
    你在ext.kotlin_version里声明了1.9.0,但buildscript的依赖中,kotlin-gradle-plugin用的是2.0.20。Gradle实际运行时会使用插件的2.0.20版本,而非你声明的1.9.0,因此触发了Kotlin 2.0对Compose Compiler插件的强制要求。

  • 修复步骤

    1. 统一Kotlin版本,将kotlin-gradle-plugin版本改为与ext.kotlin_version一致的1.9.0,推荐使用变量引用避免手动修改不一致:
      classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
      
      也可以直接指定版本号:
      classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0"
      
    2. 匹配Compose Compiler与Kotlin版本:Kotlin 1.9.0对应的兼容Compose Compiler版本为1.5.4,调整composeOptions中的版本号:
      composeOptions {
          kotlinCompilerExtensionVersion "1.5.4"
      }
      
    3. 同步Gradle项目后重新构建,即可解决该错误。

内容的提问来源于stack exchange,提问作者alexC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 16:13:13