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

升级Kotlin 2.1.0时找不到org.jetbrains.kotlin.plugin.compose插件求助

解决Kotlin 2.1.0升级中找不到org.jetbrains.kotlin.plugin.compose插件的问题

核心原因与解决步骤

1. 补全插件仓库配置(最常见遗漏)

Gradle 7+版本要求插件仓库必须在**settings.gradle(或settings.gradle.kts)**的pluginManagement块中声明,仅在项目级build.gradle的buildscript里配置是不够的。旧项目往往缺少这一步,导致Gradle无法拉取Compose插件:

// settings.gradle(Groovy语法)
pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
rootProject.name = "你的项目名称"
include ':App'

2. 简化项目级build.gradle的依赖

Kotlin 2.0+版本开始,Compose Compiler插件已经和Kotlin Gradle插件绑定,不需要额外添加Compose插件的classpath依赖。保留AGP和Kotlin插件即可:

// 项目级build.gradle
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:8.5.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.0"
        // 移除任何旧的compose compiler classpath依赖,比如:
        // classpath "org.jetbrains.kotlin:kotlin-compose-compiler-plugin:xxx"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

3. 模块级build.gradle的正确插件写法

app模块的plugins块需要按顺序应用插件:先Android应用插件,再Kotlin Android插件,最后Compose插件:

// app模块build.gradle
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.plugin.compose'
}

android {
    compileSdk 34 // 使用兼容的compileSdk版本
    defaultConfig {
        applicationId "你的包名"
        minSdk 21 // 根据项目需求调整
        targetSdk 34
        versionCode 1
        versionName "1.0"
    }

    buildFeatures {
        compose true // 开启Compose支持
    }

    composeOptions {
        // Kotlin 2.1.0对应Compose Compiler版本为1.5.10或1.5.11,必须严格匹配
        kotlinCompilerExtensionVersion "1.5.11"
    }
}

dependencies {
    // 添加Compose核心依赖
    implementation 'androidx.compose.ui:ui:1.6.8'
    implementation 'androidx.compose.material3:material3:1.2.1'
    implementation 'androidx.compose.ui:ui-tooling-preview:1.6.8'
    debugImplementation 'androidx.compose.ui:ui-tooling:1.6.8'
}

4. 清理Gradle缓存

如果以上配置都正确仍报错,执行缓存清理命令后重新同步:

# macOS/Linux
./gradlew cleanBuildCache

# Windows
gradlew cleanBuildCache

常见误区提醒

  • 不要同时保留旧版Compose Compiler的classpath和新版插件配置,会导致依赖冲突
  • kotlinCompilerExtensionVersion必须与Kotlin版本严格兼容,Kotlin 2.1.0对应Compose Compiler 1.5.10+
  • 插件应用顺序不能乱,Compose插件依赖Kotlin Android插件,必须放在后面

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 08:19:51