Dev News Daily ENDE

Kotlin Multiplatform gets opt-in separate compilation to match the IDE

JetBrains has described a new, optional way of compiling Kotlin Multiplatform modules, introduced in Kotlin 2.5.0-Beta1 as an experimental feature that is off by default. The company calls it separate compilation, and it targets two problems that multiplatform developers run into today.

The first is that the IDE and the compiler can disagree. IntelliJ IDEA analyses code in a shared source set such as commonMain against what a dependency exposes in its own common code. The compiler, when it builds a platform target such as the JVM, compiles commonMain together with the platform source set against the dependency's platform artifacts. Common code can therefore quietly resolve to a JVM-only declaration of a library: the compiler accepts it while the IDE reports an unresolved reference, or an overload and a type inference come out differently in the two.

The second is that commonTest code can unexpectedly call something that exists only in a platform source set, which breaks the assumption that shared code is shared. Under the new scheme, common source sets are compiled against the common declarations of their dependencies, the same view the IDE already takes, so problematic calls from common code into platform-only library code are reported consistently. JetBrains adds that the approach also makes incremental compilation of common source sets possible.

The feature is enabled with a compiler option in gradle.properties, and the post lists known issues.

Kotlin Multiplatform gets opt-in separate compilation to match the IDE
Kotlin Multiplatform gets opt-in separate compilation to match the IDE — Dev News Daily

What it means

For teams that have learned to trust the compiler over the red underline, this reverses the default: the stricter IDE view becomes the rule. Turning it on in an existing project is likely to surface calls that compiled until now, and those are exactly the places where a second platform target would have failed later.

Written by Victoria Shinder.