Skip to content

Kotlin 2.0 Compose Compiler Requirement in Gradle

Problem Statement

When migrating an Android project to Kotlin 2.0+, you may encounter this error:

bash
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:

groovy
// 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:

  1. Kotlin plugin (2.0+) requires explicit Compose compiler setup
  2. Conflicting Kotlin versions (1.9.0 variable vs 2.0.20 plugin)
  3. kotlinCompilerExtensionVersion must match actual Kotlin version

Solution 1: Version Catalog Approach (Modern Best Practice)

Update libs.versions.toml:

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:

kotlin
plugins {
  alias(libs.plugins.jetbrains.kotlin.android) apply false
  alias(libs.plugins.compose.compiler) apply false
}

App Module build.gradle.kts:

kotlin
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:

groovy
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:

groovy
apply plugin: "org.jetbrains.kotlin.plugin.compose"

android {
  composeOptions.kotlinCompilerExtensionVersion = compose_compiler_version
}

Key Changes Explained

  1. Plugin Application: The new org.jetbrains.kotlin.plugin.compose Gradle plugin replaces the legacy bundled compiler
  2. Version Consistency: composeOptions.kotlinCompilerExtensionVersion must match your actual Kotlin version:
    groovy
    kotlinCompilerExtensionVersion = "2.1.0"  // Matches Kotlin 2.1.0
  3. 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 dependencies
  • plugins blocks
  • composeOptions Use the same version value everywhere.

Additional Steps

  1. Sync Gradle after changes
  2. Clean build (./gradlew cleanBuildCache)
  3. 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.