使用Kotlin 2.1.0启用Compose,仍提示需Compose Compiler Gradle插件
问题:启用Compose后报错“Compose Compiler Gradle Plugin is required”
问题详情
使用Kotlin 2.1.0版本,已按官方文档配置Compose Compiler Gradle插件且启用了Compose,但构建项目时仍报错‘Compose Compiler Gradle Plugin is required’。
错误日志
A problem occurred configuring project ':app'. > com.android.builder.errors.EvalIssueException: Starting in Kotlin 2.0, the Compose Compiler Gradle plugin is required when compose is enabled. See the following link for more information: https://d.android.com/r/studio-ui/compose-compiler * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. For more on this, please refer to https://docs.gradle.org/8.10.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation. BUILD FAILED in 231ms
相关配置文件
build.gradle (:app)
plugins { id 'com.android.application' id 'kotlin-android' id 'com.google.gms.google-services' alias(libs.plugins.compose.compiler) apply false } android { compileSdk 35 defaultConfig {...} buildTypes {...} compileOptions { sourceCompatibility JavaVersion.VERSION_1.8 targetCompatibility JavaVersion.VERSION_1.8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { compose true } composeOptions { kotlinCompilerExtensionVersion '1.5.1' } packaging {...} } dependencies { implementation libs.androidx.core.ktx implementation libs.androidx.appcompat implementation libs.material implementation libs.androidx.constraintlayout implementation libs.androidx.lifecycle.runtime.ktx implementation libs.androidx.activity.compose implementation platform(libs.androidx.compose.bom) implementation libs.androidx.compose.foundation implementation libs.androidx.ui implementation libs.androidx.ui.graphics implementation libs.androidx.ui.tooling.preview implementation libs.androidx.material3 implementation libs.junit implementation libs.androidx.junit implementation libs.androidx.espresso.core //firebase google authentication implementation platform(libs.firebase.bom) implementation libs.firebase.auth implementation libs.play.services.auth androidTestImplementation platform(libs.androidx.compose.bom) androidTestImplementation libs.androidx.ui.test.junit4 debugImplementation libs.androidx.ui.tooling debugImplementation libs.androidx.ui.test.manifest }
build.gradle (项目级)
buildScript { ext.kotlin_version = '2.1.0' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.8.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms.google-services:4.4.2' } } tasks.register('clean', Delete) { delete rootProject.buildDir }
libs.versions.toml (版本目录)
[versions] activityCompose = "1.10.0" appcompat = "1.7.0" composeBom = "2025.02.00" constraintlayout = "2.2.0" coreKtx = "1.15.0" espressoCore = "3.6.1" firebaseBom = "33.9.0" junit = "4.13.2" junitVersion = "1.2.1" kotlin = "2.1.0" lifecycleRuntimeKtx = "2.8.7" material = "1.12.0" playServicesAuth = "21.3.0" [libraries] androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" } androidx-appcompat = { module = "androidx.aappcompat:appcompat", version.ref = "appcompat" } androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "composeBom" } androidx-compose-foundation = { module = "androidx.compose.foundation:foundation" } androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" } androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" } androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" } androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" } androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" } androidx-material3 = { module = "androidx.compose.material3:material3" } androidx-ui = { module = "androidx.compose.ui:ui" } androidx-ui-graphics = { module = "androidx.compose.ui:ui-graphics" } androidx-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4" } androidx-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest" } androidx-ui-tooling = { module = "androidx.compose.ui:ui-tooling" } androidx-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" } firebase-auth = { module = "com.google.firebase:firebase-auth" } firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" } material = { module = "com.google.android.material:material", version.ref = "material" } play-services-auth = { module = "com.google.android.gms:play-services-auth", version.ref = "playServicesAuth" } [plugins] org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
解决方案
1. 启用Compose Compiler插件
在build.gradle (:app)的plugins块中,移除apply false,确保插件被实际应用:
plugins { id 'com.android.application' id 'kotlin-android' id 'com.google.gms.google-services' alias(libs.plugins.compose.compiler) }
2. 删除过时的composeOptions配置
Kotlin 2.0+版本中,Compose Compiler插件会自动匹配兼容的扩展版本,无需手动指定,直接删除以下代码块:
composeOptions { kotlinCompilerExtensionVersion '1.5.1' }
3. 修复依赖拼写错误
在libs.versions.toml中,androidx-appcompat的模块名存在拼写错误,修正为:
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
4. 迁移到新版Gradle插件配置(可选)
项目级build.gradle仍使用旧的buildScript块,建议迁移到更简洁的plugins DSL:
plugins { id 'com.android.application' version '8.8.1' apply false id 'org.jetbrains.kotlin.android' version '2.1.0' apply false id 'com.google.gms.google-services' version '4.4.2' apply false } tasks.register('clean', Delete) { delete rootProject.buildDir }
完成以上修改后,重新同步Gradle并构建项目即可解决报错。
内容的提问来源于stack exchange,提问作者somnathchakrabarti
相关产品推荐
相关产品推荐

