# Compatibility guide for Kotlin 2.4.x [Keeping the Language Modern](kotlin-evolution-principles.html) and [Comfortable Updates](kotlin-evolution-principles.html) are among the fundamental principles in Kotlin Language Design. The former says that constructs which obstruct language evolution should be removed, and the latter says that this removal should be well-communicated beforehand to make code migration as smooth as possible. While most of the language changes were already announced through other channels, like update changelogs or compiler warnings, this document summarizes them all, providing a complete reference for migration from Kotlin 2.3 to Kotlin 2.4. This document also includes information about tool-related changes. ## Basic terms In this document, we introduce several kinds of compatibility: * source: source-incompatible change stops code that used to compile fine (without errors or warnings) from compiling anymore * binary: two binary artifacts are said to be binary-compatible if interchanging them doesn't lead to loading or linkage errors * behavioral: a change is said to be behavioral-incompatible if the same program demonstrates different behavior before and after applying the change Remember that those definitions are given only for pure Kotlin. Compatibility of Kotlin code from the other languages perspective (for example, from Java) is out of the scope of this document. ## Language ### Drop support for `-language-version=1.9` and the K1 compiler Tip: Issue: [KT-80590](https://youtrack.jetbrains.com/issue/KT-80590) Component: Compiler Incompatible change type: source Short summary: Starting with Kotlin 2.4, the compiler no longer supports [-language-version=1.9](compiler-reference.html#language-version-version). As a result, the K1 compiler is no longer supported. Deprecation cycle: * 2.2.0: report a warning when using `-language-version` with version 1.9 * 2.4.0: raise the warning to an error ### Prohibit flexible explicit nullable type arguments for Java types Tip: Issue: [KTLC-284](https://youtrack.jetbrains.com/issue/KTLC-284) Component: Core language Incompatible change type: source Short summary: Previously, when calling Java APIs from Kotlin, the compiler could treat explicitly specified nullable type arguments as flexible type arguments. Kotlin 2.4.0 no longer applies this behavior for nullable type arguments, so the compiler now reports errors for code that could break type safety or fail at runtime. Deprecation cycle: * 2.2.0: report a warning for explicitly specified nullable type arguments that are treated as flexible types * 2.4.0: raise the warning to an error ### Prohibit always-false `is` checks for definitely incompatible types Tip: Issue: [KTLC-365](https://youtrack.jetbrains.com/issue/KTLC-365) Component: Core language Incompatible change type: source Short summary: The compiler now prevents meaningless `is` checks that are always false because the checked types are definitely incompatible. This keeps the behavior consistent with other operations involving incompatible types. Deprecation cycle: * 2.0.0: report a warning for `is` checks with definitely incompatible types * 2.4.0: raise the warning to an error ### Prohibit exposing types and declarations with lower visibility in inline functions Tip: Issue: [KTLC-283](https://youtrack.jetbrains.com/issue/KTLC-283) Component: Core language Incompatible change type: source Short summary: The compiler now prevents inline functions from exposing types and declarations that have lower visibility than the inline function itself. Deprecation cycle: * 2.3.0: report a warning for exposing types and declarations with lower visibility in inline functions * 2.4.0: raise the warning to an error ### Change default use-site target selection for annotations Tip: Issue: [KTLC-391](https://youtrack.jetbrains.com/issue/KTLC-391) Component: Core language Incompatible change type: binary Short summary: Kotlin 2.4.0 updates the defaulting rules for propagating annotations to parameters, properties, and fields. This can affect annotation processing, reflection, and binary metadata after recompilation. When you don't specify a use-site target, the compiler now uses `param` and `property` if they apply, and uses `field` only if `property` doesn't apply. You can specify a use-site target explicitly, such as `@param:Annotation` instead of `@Annotation`. To use the previous defaulting rule for your whole project, add `-Xannotation-default-target=first-only` to your build file. Deprecation cycle: * 2.2.0: report a warning when the new defaulting rule changes the chosen use-site targets * 2.4.0: enable the new defaulting rule ### Forbid implicit references to inaccessible types Tip: Issue: [KTLC-384](https://youtrack.jetbrains.com/issue/KTLC-384) Component: Core language Incompatible change type: source Short summary: Using declarations that implicitly reference inaccessible types from indirect dependencies now results in an error. To migrate, add an explicit dependency on the module that declares the inaccessible type, or update the intermediate API so it doesn't expose that type. Deprecation cycle: * 2.3.0: report a warning for implicit references to inaccessible types * 2.4.0: raise the warning to an error ### Enforce Jakarta nullability annotations Tip: Issue: [KTLC-285](https://youtrack.jetbrains.com/issue/KTLC-285) Component: Core language Incompatible change type: source Short summary: The compiler now enforces declared nullability in Kotlin for Java declarations that use [jakarta.annotation.Nullable](https://jakarta.ee/specifications/annotations/2.1/apidocs/jakarta.annotation/jakarta/annotation/nullable) or [jakarta.annotation.Nonnull](https://jakarta.ee/specifications/annotations/2.1/apidocs/jakarta.annotation/jakarta/annotation/nonnull). If you assign a Java declaration marked as nullable by these annotations to a non-null Kotlin type, the compiler reports an error. Deprecation cycle: * 2.2.0: report a warning for nullability mismatches in Java declarations annotated with Jakarta nullability annotations * 2.4.0: raise the warning to an error ### Report misplaced type arguments in callable reference qualifiers Tip: Issue: [KTLC-388](https://youtrack.jetbrains.com/issue/KTLC-388) Component: Core language Incompatible change type: source Short summary: The compiler now checks the left-hand side of callable references and reports a warning if an inner class contains type arguments in the wrong part of the qualifier. To migrate, update the reference so that each type argument belongs to the class that declares it. For example, write the full type `Outer.Inner::toString` instead of `Inner::toString`. Deprecation cycle: * 2.4.0: report a warning when type arguments in the left-hand side of a callable reference belong to another part of the qualifier ### Report errors for class literals from reified type parameters with nullable upper bounds Tip: Issue: [KTLC-370](https://youtrack.jetbrains.com/issue/KTLC-370) Component: Core language Incompatible change type: source Short summary: The compiler now reports an error when you use `::class` on an expression whose type comes from a reified type parameter with a nullable upper bound. If you use `::class` on such an expression, make the value non-null first with an explicit null check or the `!!` operator. Deprecation cycle: * 2.3.0: report a warning when `::class` is used on an expression whose type comes from a reified type parameter with a nullable upper bound * 2.4.0: raise the warning to an error ### Prohibit initialization before declarations in anonymous objects Tip: Issue: [KTLC-290](https://youtrack.jetbrains.com/issue/KTLC-290) Component: Core language Incompatible change type: source Short summary: Kotlin now reports an error when you initialize a property in an `init` block of an anonymous object before declaring that property. Deprecation cycle: * 2.2.20: report a warning when an `init` block in an anonymous object initializes a property before the property declaration * 2.4.0: raise the warning to an error ### Enforce exhaustiveness for `when` expressions with non-abstract Java sealed classes Tip: Issue: [KTLC-366](https://youtrack.jetbrains.com/issue/KTLC-366) Component: Core language Incompatible change type: source Short summary: Kotlin now checks exhaustiveness more strictly and requires an `else` branch or a branch that matches the sealed class itself when you use a `when` expression with a non-abstract Java sealed class. Previously, Kotlin could treat such `when` expressions as exhaustive even though the Java sealed class itself could be instantiated directly. Deprecation cycle: * 2.3.0: report a warning for non-exhaustive `when` expressions with non-abstract Java sealed classes * 2.4.0: raise the warning to an error ### Prohibit `operator` modifier on `getValue()` and `setValue()` functions with too many parameters Tip: Issue: [KTLC-289](https://youtrack.jetbrains.com/issue/KTLC-289) Component: Core language Incompatible change type: source Short summary: When you mark the [getValue()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-read-only-property/get-value.html) or [setValue()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-read-write-property/set-value.html) functions with the `operator` modifier, the compiler now checks that they have the required number of value parameters. The `getValue()` function must have exactly two value parameters, and the `setValue()` function must have exactly three. To migrate, remove the `operator` modifier or change the function signature. Deprecation cycle: * 2.2.20: report a warning for `operator` `getValue()` and `setValue()` functions with too many value parameters * 2.4.0: raise the warning to an error ### Prohibit inconsistent type arguments in generic calls Tip: Issue: [KTLC-373](https://youtrack.jetbrains.com/issue/KTLC-373) Component: Core language Incompatible change type: source Short summary: When you specify type arguments in a generic call, the compiler now reports an error if one type argument violates an upper-bound constraint that depends on another type argument. If type parameters depend on each other, use type arguments that match those constraints, for example `Container()` instead of `Container()`. Deprecation cycle: * 2.3.0: report a warning when explicit type arguments in a generic call violate upper-bound constraints between type parameters * 2.4.0: raise the warning to an error ### Deprecate references to the `javaClass` property Tip: Issue: [KTLC-375](https://youtrack.jetbrains.com/issue/KTLC-375) Component: Kotlin/JVM Incompatible change type: source Short summary: Kotlin 2.4.0 deprecates property references to the [javaClass](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.jvm/java-class.html) property to reduce confusion with `::class.java`. Use `.javaClass` to get the runtime Java class of an object, or `::class.java` to get a Java class reference. Deprecation cycle: * 2.4.0: report a warning for property references to the `javaClass` property ### Report errors for implicit enum constructor calls that require opt-in Tip: Issue: [KTLC-359](https://youtrack.jetbrains.com/issue/KTLC-359) Component: Core language Incompatible change type: source Short summary: Kotlin now reports an error when an enum entry implicitly calls an enum primary constructor that requires opt-in. To migrate, add `@OptIn` to the enum class or to each enum entry that calls the constructor. Deprecation cycle: * 2.2.20: report a warning when an enum entry implicitly calls an enum primary constructor that requires opt-in * 2.4.0: raise the warning to an error ### Forbid `inline` modifier on enum entries Tip: Issue: [KTLC-361](https://youtrack.jetbrains.com/issue/KTLC-361) Component: Core language Incompatible change type: source Short summary: Kotlin now reports an error when you use the `inline` modifier on an enum entry. Deprecation cycle: * 2.3.0: report a warning when the `inline` modifier is used on an enum entry * 2.4.0: raise the warning to an error ### Prohibit array literals outside annotation calls and parameter defaults Tip: Issue: [KTLC-369](https://youtrack.jetbrains.com/issue/KTLC-369) Component: Core language Incompatible change type: source Short summary: Using array literals outside annotation calls and default values for annotation parameters now results in an error. To migrate, use `arrayOf(...)`, for example `Roles(arrayOf("admin", "user"))` instead of `Roles(["admin", "user"])`. Deprecation cycle: * 2.3.0: report a warning for array literals outside annotation calls and default values for annotation parameters * 2.4.0: raise the warning to an error ### Prohibit `_root_ide_package_` in CLI compiler mode Tip: Issue: [KTLC-378](https://youtrack.jetbrains.com/issue/KTLC-378) Component: Compiler Incompatible change type: source Short summary: Using the IDE-only `_root_ide_package_` qualifier in CLI compiler mode now results in an error. Deprecation cycle: * 2.3.20: report a warning for `_root_ide_package_` references in CLI compiler mode * 2.4.0: raise the warning to an error ### Correct equality for function references with vararg conversions Tip: Issue: [KTLC-385](https://youtrack.jetbrains.com/issue/KTLC-385) Component: Kotlin/JVM Incompatible change type: behavioral Short summary: Kotlin/JVM now treats function references with different conversions as unequal. Previously, Kotlin/JVM ignored vararg conversion in equality checks when the same function reference also used another conversion, so `getDefault(::foo) == getDefaultAndVararg(::foo)` could return `true` even though only one side used vararg conversion. Deprecation cycle: * 2.4.0: introduce the new behavior ### Enforce opt-in for companion object access Tip: Issue: [KTLC-386](https://youtrack.jetbrains.com/issue/KTLC-386) Component: Core language Incompatible change type: source Short summary: Kotlin now reports an opt-in error when a class name reference resolves to a companion object that requires opt-in. For example, `val p = C` requires opt-in if `C` resolves to a companion object marked with an opt-in annotation. Deprecation cycle: * 2.3.20: report a warning when companion object access requires opt-in * 2.4.0: raise the warning to an error for `ERROR`-level opt-in requirements ### Report type mismatches from supertypes with nested generic arguments Tip: Issue: [KTLC-372](https://youtrack.jetbrains.com/issue/KTLC-372) Component: Core language Incompatible change type: source Short summary: Kotlin now reports an error when the compiler detects a type mismatch involving a supertype with nested generic arguments. Previously, the compiler could miss this mismatch, which later failed with a `ClassCastException`. To migrate, use a type argument that matches the receiver's generic type, or remove the explicit type argument so the compiler can infer it. Deprecation cycle: * 2.4.0: report an error for type mismatches involving supertypes with nested generic arguments ### Prohibit inferred types with inaccessible declarations Tip: Issue: [KTLC-363](https://youtrack.jetbrains.com/issue/KTLC-363) Component: Core language Incompatible change type: source Short summary: Using an inferred type that contains a declaration inaccessible in the current scope now results in an error. Deprecation cycle: * 2.3.0: report a warning when an inferred type contains a declaration that isn't accessible in the current scope * 2.4.0: raise the warning to an error ## Standard library ### Deprecate `kotlin.io.readLine()` function Tip: Issue: [KTLC-394](https://youtrack.jetbrains.com/issue/KTLC-394) Component: kotlin-stdlib Incompatible change type: source Short summary: The [kotlin.io.readLine()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.io/read-line.html) function is deprecated. Use the [readln()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.io/readln.html) function instead of `readLine()!!`, and the [readlnOrNull()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.io/readln-or-null.html) function instead of `readLine()`. Deprecation cycle: * 2.4.0: report a warning when using `kotlin.io.readLine()` ### Deprecate `AbstractCoroutineContextKey` and related APIs Tip: Issue: [KT-84970](https://youtrack.jetbrains.com/issue/KT-84970) Component: kotlin-stdlib Incompatible change type: source Short summary: The [AbstractCoroutineContextKey](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-abstract-coroutine-context-key/) class and its related APIs were experimental since Kotlin 1.3 and proved to be error-prone. For this reason, this class and the related [getPolymorphicElement()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/get-polymorphic-element.html) and [minusPolymorphicKey()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/minus-polymorphic-key.html) functions are deprecated. Deprecation cycle: * 2.4.0: report a warning when using the deprecated APIs ### Change `Random.nextDouble()` contract for infinite bounds Tip: Issue: [KT-84368](https://youtrack.jetbrains.com/issue/KT-84368) Component: kotlin-stdlib Incompatible change type: behavioral Short summary: The documented contract for [Random.nextDouble(until)](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.random/-random/next-double.html) now requires the `until` bound to be finite. Use a finite bound instead. Deprecation cycle: * 2.4.0: enable the new behavior ## Tools ### Deprecate legacy Kotlin/JS compiler type selection APIs Tip: Issue: [KT-64275](https://youtrack.jetbrains.com/issue/KT-64275), [KT-84753](https://youtrack.jetbrains.com/issue/KT-84753) Component: Gradle Incompatible change type: source Short summary: Kotlin 2.4.0 removes deprecated Gradle APIs related to selecting the legacy Kotlin/JS compiler type. Additionally, the `KotlinJsCompilerType` enum and the `KotlinProjectExtension.js()` overloads with a compiler type parameter are deprecated. To migrate, remove the compiler type argument from the `js()` target declaration and use the `js {}` block instead. Deprecation cycle: * 1.8.0: deprecate legacy Kotlin/JS compiler type constants * 2.4.0: remove the deprecated legacy compiler type APIs and report a warning when using `KotlinJsCompilerType` or `KotlinProjectExtension.js()` overloads with a compiler type parameter ### Deprecate `sourceSets` in the Kotlin Android extension Tip: Issue: [KT-74451](https://youtrack.jetbrains.com/issue/KT-74451) Component: Gradle Incompatible change type: source Short summary: The `sourceSets` property in `KotlinAndroidProjectExtension` is deprecated. To migrate, configure source sets through the Android Gradle plugin's `android { sourceSets { ... } }` block instead. Deprecation cycle: * 2.4.0: report a warning when accessing `sourceSets` from `KotlinAndroidProjectExtension` ### Remove consumable configurations for Kotlin/Native Apple frameworks Tip: Issue: [KT-74503](https://youtrack.jetbrains.com/issue/KT-74503), [KT-82230](https://youtrack.jetbrains.com/issue/KT-82230) Component: Gradle Incompatible change type: source Short summary: Kotlin 2.4.0 removes generated consumable Gradle configurations that expose Kotlin/Native Apple frameworks as outgoing artifacts. Deprecation cycle: * 2.4.0: remove consumable configurations for Kotlin/Native Apple frameworks ### Remove deprecated task, compilation, and DSL APIs from the Kotlin Gradle plugin Tip: Issue: [KT-85509](https://youtrack.jetbrains.com/issue/KT-85509) Component: Gradle Incompatible change type: source Short summary: Kotlin 2.4.0 removes the following deprecated Kotlin Gradle plugin APIs: Compile task configuration APIs: * `KotlinJvmCompile.parentKotlinOptions` * `KotlinJvmCompile.moduleName` * `KotlinJvmFactory.createKotlinJvmOptions()` * `BaseKotlinCompile.moduleName` from `KotlinCompile` and `Kotlin2JsCompile` tasks Kotlin Multiplatform hierarchy and target APIs: * `DeprecatedKotlinTargetHierarchyDsl` * `KotlinMultiplatformExtension.targetHierarchy` * `KotlinTargetComponent.sourcesArtifacts` * `KotlinTarget.sourceSets` * `KotlinHierarchyBuilder.withoutCompilations()` * `KotlinHierarchyBuilder.filterCompilations()` * `KotlinHierarchyBuilder.withWasm()` * `KotlinCompilation.defaultSourceSetName` Kotlin compilation task APIs: * `KotlinCompilation.compileKotlinTaskProvider` * `KotlinCompilation.compileKotlinTask` Kotlin dependency handler APIs: * `KotlinDependencyHandler.enforcedPlatform()` * `KotlinDependencyHandler.platform()` Other deprecated task and extension APIs: * `KaptExtension.processors` * `KotlinTest.excludes` * `KotlinTest.fileResolver` * `KotlinTest.execHandleFactory` * `IncrementalSyncTask.destinationDir` To migrate, remove usages of these APIs and use the replacements suggested by the deprecation diagnostics. Deprecation cycle: * 2.4.0: remove the deprecated APIs ### Deprecate explicit shrunk classpath snapshot configuration Tip: Issue: [KT-75837](https://youtrack.jetbrains.com/issue/KT-75837) Component: Build tools API Incompatible change type: source Short summary: The `shrunkClasspathSnapshot` configuration parameter in `ClasspathSnapshotBasedIncrementalCompilationApproachParameters` is deprecated. The shrunk classpath snapshot is an internal incremental compilation cache, so the compiler now creates and manages it automatically under the incremental compiler metadata `workingDirectory`. To migrate, use the automatically managed snapshot file, instead of passing a value to `shrunkClasspathSnapshot`. Deprecation cycle: * 2.4.0: report a warning when using `shrunkClasspathSnapshot` ### Remove redundant ABI validation Gradle DSL elements Tip: Issue: [KT-80685](https://youtrack.jetbrains.com/issue/KT-80685) Component: Gradle Incompatible change type: source Short summary: Kotlin 2.4.0 simplifies the [ABI validation](gradle-binary-compatibility-validation.html) Gradle DSL and removes redundant configuration entries. To migrate, configure report settings directly in `abiValidation {}` instead of `abiValidation { legacyDump { ... } }`, remove `abiValidation { klib { enabled = ... } }`, and use `keepLocallyUnsupportedTargets` instead of `klib.keepUnsupportedTargets`. Deprecation cycle: * 2.4.0: remove redundant ABI validation DSL elements ### Deprecate obsolete Compose compiler Gradle plugin options Tip: Issue: [KT-85343](https://youtrack.jetbrains.com/issue/KT-85343) Component: Gradle Incompatible change type: source Short summary: In Kotlin 2.4.0, the following deprecated Compose compiler Gradle plugin options now report an error when used: * `generateFunctionKeyMetaClasses` * `enableIntrinsicRemember` * `enableNonSkippingGroupOptimization` * `enableStrongSkippingMode` * `stabilityConfigurationFile` * `ComposeFeatureFlag.StrongSkipping` * `ComposeFeatureFlag.IntrinsicRemember` Use `featureFlags` instead of the deprecated feature options, and `stabilityConfigurationFiles` instead of `stabilityConfigurationFile`. Deprecation cycle: * 2.0.20: report warnings for `enableIntrinsicRemember`, `enableNonSkippingGroupOptimization`, and `enableStrongSkippingMode` * 2.1.0: report a warning for `stabilityConfigurationFile` * 2.4.0: raise the warnings to errors ### Report errors for obsolete Kotlin/Native Gradle task APIs Tip: Issue: [KT-85510](https://youtrack.jetbrains.com/issue/KT-85510) Component: Gradle Incompatible change type: source Short summary: The following deprecated Kotlin/Native Gradle task APIs now report an error when used: `AbstractKotlinNativeCompile` properties: * `additionalCompilerOptions` * `languageSettings` * `progressiveMode` `KotlinNativeCompile` properties: * `moduleName` * `konanDataDir` * `konanHome` * `languageVersion` * `apiVersion` * `enabledLanguageFeatures` * `optInAnnotationsInUse` * `additionalCompilerOptions` `CInteropProcess` properties: * `outputFile` * `konanDataDir` * `konanHome` * `defFile` `KotlinNativeLink` properties: * `languageSettings` * `additionalCompilerOptions` * `konanDataDir` * `konanHome` Additionally, the `KotlinNativeLink.compilation` property is removed. Deprecation cycle: * 2.4.0: report an error for the deprecated Kotlin/Native Gradle task APIs, remove the `KotlinNativeLink.compilation` property ### Report warnings for case mismatches in compiler argument values Tip: Issue: [KT-86059](https://youtrack.jetbrains.com/issue/KT-86059) Component: Build tools API Incompatible change type: source Short summary: Compiler arguments that accept a fixed set of values previously handled letter case inconsistently: some accepted any letter case, while others required an exact match. The [Build tools API](build-tools-api.html) now accepts any letter case for these values but reports a warning, for example `Case mismatch for -module-kind: expected 'commonjs', got 'CommonJS'`. To migrate, use the letter case listed for the argument in the [compiler reference](compiler-reference.html). Deprecation cycle: * 2.4.20: report a warning when the letter case of a compiler argument value doesn't match the expected value