What's new in Kotlin 2.4.20-RC
The Kotlin 2.4.20-RC release is out! Here are some details of this EAP release:
Standard library: Support for coroutine stack trace recovery and new features for checking equality and uniqueness of collection elements
Kotlin/Native: New Swift export features and automatically generated
Package.swiftfiles for SwiftPM dependenciesKotlin/JS: New DSL for browser testing and support for exporting suspend lambdas as async functions
Build tools API: Support for new targets: Kotlin/JS, Kotlin/Wasm, and Kotlin metadata
Kotlin compiler: Experimental release of the native image
Update to Kotlin 2.4.20-RC
The latest version of Kotlin is included in the latest versions of IntelliJ IDEA and Android Studio.
To update to the new Kotlin version, make sure your IDE is updated to the latest version and change the Kotlin version to 2.4.20-RC in your build scripts.
New features
The following pre-stable features are available in this release. This includes features with Beta, Alpha, and Experimental status:
Standard library
Kotlin 2.4.20-RC adds support for coroutine stack trace recovery and introduces new functions to check collection elements for equality and uniqueness.
Support for coroutine stack trace recovery
Kotlin 2.4.20-RC adds the StackTraceRecoverable interface to the standard library. This improves integration with the kotlinx.coroutines library because it lets you define how to create new exception instances for stack trace recovery without adding a dependency on kotlinx.coroutines.
Stack trace recovery helps with debugging when one coroutine throws an exception and another rethrows it. It lets you see where the exception originates and where another coroutine rethrows it.
The kotlinx.coroutines library performs stack trace recovery by creating a new exception instance with additional coroutine stack trace information. This happens automatically for exceptions with constructors that take only an exception message, a cause, both, or no arguments.
If an exception constructor has additional required arguments, such as a line number or an error code, implement the StackTraceRecoverable interface to define how the kotlinx.coroutines library creates a new instance of that exception.
To implement the interface, override the copyForStackTraceRecovery() function. In the override, return a new exception instance for stack trace recovery, or null if you don't want the kotlinx.coroutines library to copy the exception.
These APIs are Experimental and require opt-in with the @OptIn(ExperimentalStdlibCoroutineSupportApi::class) annotation.
Here's an example of a custom exception that preserves a line property when it creates a new instance for stack trace recovery:
For more information, see the feature's KEEP.
We would appreciate your feedback in YouTrack.
New functions to check collection elements for equality and uniqueness
Before Kotlin 2.4.20-RC, if you wanted to check whether collection elements were all distinct or all equal, you had to use inefficient code patterns.
Kotlin 2.4.20-RC introduces experimental functions to fill this gap:
Function | Checks |
|---|---|
| Every value in the collection is unique. |
| Every object has a unique value for the selected property. |
| Every value in the collection is the same. |
| Every object has the same value for the selected property. |
You can use these functions on collections, sequences, and arrays. They compare elements using structural equality just like other collection operations.
These functions are Experimental and require opt-in with the @OptIn(ExperimentalStdlibApi::class) annotation or the -opt-in=kotlin.ExperimentalStdlibApi compiler option:
We would appreciate hearing your feedback on your experience with these functions in YouTrack.
Kotlin/Native
Kotlin 2.4.20-RC brings new Swift export features, including support for sealed classes and cross-language inheritance, and automatic generation of Package.swift files for SwiftPM dependencies.
New Swift export features
Sealed classes
Kotlin 2.4.20-RC adds support for sealed classes and interfaces to Swift export.
Previously, you had to write a default case for every switch statement over a sealed type. Now, sealed hierarchies defined in Kotlin are mapped to Swift enums, enabling exhaustive switch statements with full autocompletion in Xcode.
Swift export generates a .sealedType() method on each sealed type. This method returns a Swift enum whose cases match the direct subclasses of the sealed hierarchy. You can nest these calls to match deeper levels of the hierarchy.
For example, declare a sealed interface with a class hierarchy in Kotlin:
On the Swift side, you can use an exhaustive switch without a default case:
Because the switch is exhaustive, the compiler warns you if a new subclass is added to the sealed hierarchy, so you can handle it immediately instead of relying on a default case.
Cross-language inheritance in Swift export
Kotlin 2.4.20-RC introduces cross-language inheritance support to Swift export.
A common use case for this feature is the reverse import pattern, where you define a contract in Kotlin and provide platform-specific implementations on the Swift side.
For example, you can declare a Kotlin interface, implement it in Swift, and then pass the Swift object to Kotlin functions that accept that interface. This is especially useful when you need to use pure Swift libraries that can't be directly imported into Kotlin.
For example, declare a Kotlin interface and a function that accepts it:
On the Swift side, implement this interface using a pure Swift library and pass it back to Kotlin:
When Kotlin receives a Swift object, it treats it like an implementation of a regular interface, executing Swift code.
For more details on Swift export, see our documentation.
Generated Package.swift for SwiftPM dependencies
When exporting an XCFramework that depends on SwiftPM packages, you must publish the resulting SwiftPM package for it to resolve correctly. To help with this, the assembleSharedXCFramework Gradle task now generates a Package.swift file to be distributed along with the XCFramework.
For details, see the SwiftPM export page.
Kotlin/Wasm
Kotlin 2.4.20-RC changes how Kotlin/Wasm handles top-level require() calls in @JsFun declarations, aligns companion object initialization order with JVM behavior, and adds support for Wasmtime as a runtime for the wasmWasi target in the Kotlin Gradle plugin.
Changes to top-level require() calls in @JsFun declarations
Kotlin/Wasm now reports an error when a @JsFun declaration uses the top-level require() function.
Previously, the compiler generated a require variable in the import-object.mjs file, allowing @JsFun declarations to call require().
This behavior unintentionally exposed a compiler implementation detail. To support migration away from it, Kotlin/Wasm removes this generated require declaration, and the compiler now reports errors for such calls. For example:
To prepare for this change, replace top-level require() calls in @JsFun declarations with the @JsModule annotation:
For dynamic module loading, use the import() expression instead. Add the /* webpackIgnore: true */ magic comment to prevent webpack from parsing the dynamic import:
You can also use the import() expression conditionally. For example, you can load a module only when running in Node.js:
If your project relies on dependencies that require a top-level require() function, add it as a property of globalThis as a workaround:
If you run into any issues, share your feedback in our issue tracker.
Improved companion object initialization order
Kotlin/Wasm now initializes superclass companion objects before subclass companion objects, matching the JVM behavior. Previously, the initialization could be reversed, leading to inconsistent behavior across platforms.
The update improves cross-platform consistency and reduces platform-specific differences in class initialization behavior. It also enables correct handling of companion object initialization in deeper inheritance hierarchies, including cases where intermediate classes don't declare companion objects.
Support for Wasmtime in the Kotlin Gradle plugin
Kotlin 2.4.20-RC introduces support for Wasmtime as a runtime for the wasmWasi target in the Kotlin Gradle plugin.
Previously, the wasmWasi target supported only the Node.js runtime, which required a JavaScript bootstrap to run WASI applications. With Wasmtime support, you can now run Kotlin/Wasm applications on a standalone WebAssembly runtime.
To use Wasmtime as the runtime for the wasmWasi target, add wasmtime() to your Gradle build file:
We would appreciate your feedback in YouTrack.
Kotlin/JS
Kotlin 2.4.20-RC introduces a new experimental DSL for browser testing and adds support for exporting suspending lambdas as JavaScript async functions.
A new DSL for browser testing
Kotlin 2.4.20-RC introduces a new experimental DSL for running Kotlin/JS tests in a browser environment.
Currently, the Kotlin Gradle plugin uses Karma as a browser launcher to run JavaScript tests across different browsers. The Karma project has been deprecated for two years now, which has led us to explore alternative ways to support browser testing.
The new DSL is intended to replace Karma as a manager of different tools under the hood and includes:
Mocha as a test runner.
Webpack as a bundler (will be replaced with Vite in future releases).
Playwright as a browser driver and a distribution manager that supports the Chromium, Firefox, and WebKit (Safari) browser engines.
To try out the new testing DSL, add the opt-in test{} block inside browser{} for your Kotlin/JS target:
The new DSL is in active development. We would appreciate your feedback in YouTrack.
Support for exporting suspending lambdas as async functions
With Kotlin 2.4.20-RC, you can now export suspending lambda expressions as JavaScript async functions.
Previously, there was no way to export declarations containing suspending lambdas from Kotlin/JS libraries. Now the Kotlin compiler automatically handles the bridging between Kotlin's suspend functions and JavaScript's native async/await model, which is useful for mixed Kotlin/TypeScript codebases.
To enable this feature, add the following compiler option to your build.gradle.kts file:
Then, mark the relevant declarations with @JsExport:
From the TypeScript side, the suspending lambda appears as a regular async function:
For more information on the @JsExport annotation, see our documentation.
Build tools API
Support for Kotlin/JS, Kotlin/Wasm, and Kotlin metadata
In Kotlin 2.2.0, the build tools API (BTA) became available for Kotlin/JVM. Kotlin 2.4.20-RC takes the next step toward BTA stabilization by adding support for new targets: Kotlin/JS, Kotlin/Wasm, and Kotlin metadata.
This makes the Kotlin Gradle plugin interact with the compiler more consistently. In some cases, you can also benefit from faster, more stable compilation.
The BTA is a universal API that acts as an abstraction layer between build systems and the Kotlin compiler ecosystem. It helps support Kotlin features and compatibility with the Kotlin compiler in available build tools.
In Kotlin 2.4.20-RC, BTA is available as an opt-in for the new targets. To try it out, add the corresponding properties to your gradle.properties file:
Starting with Kotlin 2.5.0, we plan to enable BTA in Kotlin/JS, Kotlin/Wasm, and Kotlin metadata by default.
If you're curious about the BTA proposal or want to share your feedback, see this KEEP.
Kotlin compiler: Native image
Kotlin 2.4.20-RC features the first Experimental release of the Kotlin compiler native image. The native image provides a drop-in replacement for the standard kotlinc command-line tool, while offering faster startup time and higher performance.
To try out the native image, download the build from GitHub Releases.
The native image also bundles the following compiler plugins you can use with the -Xplugin or -Xcompiler-plugin CLI options:
For more information on the Kotlin compiler native image, see its README.