Kotlin 2.0 Compose Compiler Requirement in Gradle
Problem Statement
When migrating an Android project to Kotlin 2.0+, you may encounter this error:
Starting in Kotlin 2.0, the Compose Compiler Gradle plugin is required when compose is enabled.
This occurs because Kotlin 2.0+ no longer bundles the Compose compiler directly, requiring explicit plugin configuration. The error persists even if you have composeOptions
configured, as shown in this problematic build configuration:
// build.gradle
ext.kotlin_version = '1.9.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.20" // Inconsistency!
android {
buildFeatures.compose = true
composeOptions.kotlinCompilerExtensionVersion = "1.5.14" // Incorrect version
}
Key issues:
- Kotlin plugin (2.0+) requires explicit Compose compiler setup
- Conflicting Kotlin versions (
1.9.0
variable vs2.0.20
plugin) kotlinCompilerExtensionVersion
must match actual Kotlin version
Recommended Solutions
Solution 1: Version Catalog Approach (Modern Best Practice)
Update libs.versions.toml
:
[versions]
kotlin = "2.1.0" # Ensure ≥2.0
[plugins]
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
Project-level build.gradle.kts
:
plugins {
alias(libs.plugins.jetbrains.kotlin.android) apply false
alias(libs.plugins.compose.compiler) apply false
}
App Module build.gradle.kts
:
plugins {
alias(libs.plugins.jetbrains.kotlin.android)
alias(libs.plugins.compose.compiler)
}
android {
buildFeatures.compose = true
composeOptions.kotlinCompilerExtensionVersion = libs.versions.kotlin.get()
}
Solution 2: Without Version Catalogs
Project-level build.gradle
:
buildscript {
ext.compose_compiler_version = "2.1.0" # Must match actual Kotlin version
dependencies {
classpath "org.jetbrains.kotlin.plugin.compose:org.jetbrains.kotlin.plugin.compose.gradle.plugin:$compose_compiler_version"
}
}
App Module build.gradle
:
apply plugin: "org.jetbrains.kotlin.plugin.compose"
android {
composeOptions.kotlinCompilerExtensionVersion = compose_compiler_version
}
Key Changes Explained
- Plugin Application: The new
org.jetbrains.kotlin.plugin.compose
Gradle plugin replaces the legacy bundled compiler - Version Consistency:
composeOptions.kotlinCompilerExtensionVersion
must match your actual Kotlin version:groovykotlinCompilerExtensionVersion = "2.1.0" // Matches Kotlin 2.1.0
- Migration Note: Remove legacy Compose compiler dependencies that conflict with the new plugin:diff
- implementation "androidx.compose.compiler:compiler:1.5.14" + // This package is obsolete with Kotlin ≥2.0
Version Mismatch Risks
Inconsistent versions will break compilation. Verify Kotlin versions in:
classpath
dependenciesplugins
blockscomposeOptions
Use the same version value everywhere.
Additional Steps
- Sync Gradle after changes
- Clean build (
./gradlew cleanBuildCache
) - Update Compose dependencies to compatible versions:groovy
implementation "androidx.compose.ui:ui:1.8.0" // Check compatibility
When to Apply This Fix
- Projects using Kotlin 2.0+
- Android builds with
buildFeatures.compose = true
- Projects receiving the "Compose Compiler Gradle plugin is required" error
By explicitly declaring the Compose compiler plugin and aligning versions, the project will successfully build using Kotlin 2.0+ features without compatibility warnings. For reference, see the official Android Compose Compiler documentation.