Kotlin 2 and the K2 compiler

Kotlin language Mid-level Kotlin 2.0+ API 23+ Reviewed By Best Latest Apps

Pick the fix

After upgrading to Kotlin 2.x, the build fails with a Compose compiler version error. What's the fix?

Options
Hint 1 of 1

Where does the Compose compiler come from in Kotlin 2?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Apply the org.jetbrains.kotlin.plugin.compose plugin and remove kotlinCompilerExtensionVersion

Correct. The Compose compiler now ships with Kotlin as a Gradle plugin.

Why not the others?
  • B Pin the old Compose compiler extension version. Old extension versions don't support Kotlin 2.
  • C Downgrade Compose UI libraries. The UI libraries aren't the problem.
  • D Switch Compose off. That's not a fix.

30-second answer

Kotlin 2.0 made K2 the default compiler. It has a new frontend that is much faster to compile, unified across JVM, Native, JS and Wasm, and smarter at smart casts. The Compose compiler moved into the Kotlin repository and ships with each Kotlin release as a Gradle plugin. Most code compiles unchanged, but some edge-case errors and warnings changed.

Practise saying it · 35 s. No recording, just a timer

Explain what Kotlin 2 and K2 changed.

Say your answer out loud, then check which of these points you covered:

  • K2 is the default compiler
  • Faster, smarter smart casts
  • Compose compiler ships with Kotlin
  • KSP over kapt

Deep dive

K2 smart-casts after || checks, inside more loops, and for local variables captured in some lambdas.

kapt runs through K2 support now, but KSP is the faster, recommended choice for Room and Hilt.

Compose projects apply org.jetbrains.kotlin.plugin.compose, matching the Kotlin version, instead of setting kotlinCompilerExtensionVersion.

Newer releases build on K2 with features like guard conditions in when and multi-dollar string interpolation.

Common mistakes

  • Still setting composeOptions.kotlinCompilerExtensionVersion after moving to Kotlin 2.
  • Assuming K2 changes runtime behaviour; it's a compiler change.
  • Keeping kapt when KSP is available for the processors you use.

Senior take & follow-ups

Show you've migrated a project: mention the Compose Gradle plugin and KSP.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why move from kapt to KSP?

    Show hint

    KSP is faster and doesn't need Java stubs.

  2. What's the Compose compiler version now?

    Show hint

    The same as the Kotlin version, through the Gradle plugin.

Suggest a fix if something here is wrong or out of date.