构建Kotlin+Jetpack Compose桌面项目遇ComposeComponentRegistrar兼容性错误
我在IntelliJ中使用Kotlin、Jetpack Compose和Gradle开发桌面项目,构建阶段遇到如下错误:
Kotlin: The provided plugin androidx.compose.compiler.plugins.kotlin.ComposeComponentRegistrar is not compatible with this version of compiler.
java.lang.AbstractMethodError: Receiver class androidx.compose.compiler.plugins.kotlin.ComposeComponentRegistrar does not define or inherit an implementation of the resolved method 'abstract void registerProjectComponents(com.intellij.mock.MockProject, org.jetbrains.kotlin.config.CompilerConfiguration)' of interface org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar.
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.registerExtensionsFromPlugins$cli(KotlinCoreEnvironment.kt:666)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$ProjectEnvironment.registerExtensionsFromPlugins(KotlinCoreEnvironment.kt:168)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.configureProjectEnvironment(KotlinCoreEnvironment.kt:569)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:198)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:107)
我的build.gradle.kts配置如下:
import org.jetbrains.compose.desktop.application.dsl.TargetFormat plugins { kotlin("multiplatform") id("org.jetbrains.compose") } group = "com.example" version = "1.0-SNAPSHOT" repositories { google() mavenCentral() maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") } kotlin { jvm { jvmToolchain(11) withJava() } sourceSets { val jvmMain by getting { dependencies { implementation(compose.desktop.currentOs) } } val jvmTest by getting } } compose.desktop { application { mainClass = "MainKt" nativeDistributions { targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) packageName = "demo" packageVersion = "1.0.0" } } }
gradle.properties文件当前配置如下(已尝试调整但无效):
kotlin.code.style=official kotlin.version=1.8.0 agp.version=7.3.0 compose.version=1.3.0
我已尝试更换不同版本的Kotlin和Jetpack Compose,但仍出现相同错误。需说明的是,程序编译后可正常运行,期望项目能无错误构建成功。
1. 严格匹配Kotlin与Compose版本
Compose编译器插件和Kotlin版本存在强依赖,必须使用官方配套版本:
- Kotlin 1.8.0 对应 Compose 1.4.0(而非当前的1.3.0)
- 修改
gradle.properties中的版本配置:
kotlin.code.style=official kotlin.version=1.8.0 agp.version=7.3.0 compose.version=1.4.0 compose.compiler.version=1.4.0
- 若使用Compose Multiplatform,需在
settings.gradle.kts中指定插件版本与Compose版本一致:
plugins { id("org.jetbrains.compose") version "1.4.0" apply false }
2. 显式指定Compose Compiler依赖
在build.gradle.kts中强制声明Compiler版本,避免依赖冲突:
kotlin { jvm { jvmToolchain(11) withJava() } sourceSets { val jvmMain by getting { dependencies { implementation(compose.desktop.currentOs) } } val jvmTest by getting } compilerOptions { freeCompilerArgs.add("-Xplugin=${project.configurations.composeCompiler.singleFile}") } } dependencies { val composeCompilerVersion = "1.4.0" val composeCompiler = "androidx.compose.compiler:compiler:$composeCompilerVersion" compileOnly(composeCompiler) composeCompiler(composeCompiler) }
3. 清理缓存并重新同步
- 执行Gradle命令清理缓存:
./gradlew clean build --refresh-dependencies - 在IntelliJ中点击
File->Invalidate Caches...,选择Invalidate and Restart
4. 对齐IntelliJ Kotlin插件版本
确保IntelliJ安装的Kotlin插件版本与项目中kotlin.version一致(1.8.0):
- 打开
Settings->Plugins->Kotlin,查看版本并更新到对应版本
内容的提问来源于stack exchange,提问作者Matteo Tatoni

