Kotlin Help

What's new in Kotlin 1.9.0-Beta

Released: May 24, 2023

The Kotlin 1.9.0-Beta release is out! Here are some highlights from this preview version of Kotlin:

IDE support

The Kotlin plugins that support 1.9.0-Beta are available for:

IDE

Supported versions

IntelliJ IDEA

2022.3.x, 2023.1.x

Android Studio

Giraffe (223)

New Kotlin K2 compiler updates

The Kotlin team continues to stabilize the K2 compiler. The 1.9.0-Beta release introduces further advancements, including basic support for Kotlin/Native and improved Kotlin/JS stability in the K2 compiler. It's an important step towards full support of multiplatform projects. We would appreciate your feedback to help us with it.

Also, starting with 1.9.0-Beta and until the release of Kotlin 2.0, you can easily test the K2 compiler in your projects. Add kotlin.experimental.tryK2=true to your gradle.properties file or run the following command:

./gradlew assemble -Pkotlin.experimental.tryK2=true

This Gradle property automatically sets the language version to 2.0 and updates the build report with the number of Kotlin tasks compiled using the K2 compiler compared to the current compiler:

##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) ##### :lib:compileKotlin: 2.0 language version :app:compileKotlin: 2.0 language version ##### 100% (2/2) tasks have compiled with Kotlin 2 #####

Share your feedback on the new K2 compiler

We'd appreciate any feedback you might have!

Stable ..< operator for open-ended ranges

The new ..< operator for open-ended ranges that was introduced in Kotlin 1.7.20 is Stable in 1.9.0-Beta. The standard library API for working with open-ended ranges is also Stable in this release.

Our research shows that the new ..< operator makes it easier to understand when an open-ended range is declared. If you use the until infix function, it's easy to make the mistake of assuming that the upper bound is included.

Here is an example using the until function:

fun main() { for (number in 2 until 10) { if (number % 2 == 0) { print("$number ") } } // 2 4 6 8 }

And here is an example using the new ..< operator:

fun main() { for (number in 2..<10) { if (number % 2 == 0) { print("$number ") } } // 2 4 6 8 }

For more information about what you can do with this operator, see What's new in Kotlin 1.7.20.

New path utility to create parent directories

In 1.9.0-Beta there is a new extension function createParentDirectories() that you can use to create a new file with all the necessary parent directories. When you provide a file path to createParentDirectories(), it checks whether the parent directories already exist. If they do, it does nothing. However, if they do not, it creates them for you.

createParentDirectories() is particularly useful when you are copying files. For example, you can use it in combination with the copyToRecursively() function:

sourcePath.copyToRecursively( destinationPath.createParentDirectories(), followLinks = false )

Preview of Gradle configuration cache in Kotlin Multiplatform

Kotlin 1.9.0-Beta comes with support for Gradle configuration cache in multiplatform libraries. If you're a library author, you can already benefit from the improved build performance.

Gradle configuration cache speeds up the build process by reusing the results of the configuration phase for subsequent builds. The feature has become Stable since Gradle 8.1. To enable it, follow the instructions in the Gradle documentation.

Changes for Android target support in Kotlin Multiplatform

We continue our efforts to stabilize Kotlin Multiplatform. An essential step in this direction is to provide first-class support for the Android target. We're excited to announce that in the future, the Android team from Google will provide its own Gradle plugin to support Android in Kotlin Multiplatform.

To open the way for the new solution from Google, we're renaming the android block to androidTarget in the current Kotlin DSL in 1.9.0-Beta. This is a temporary change that is necessary to free the android name for the upcoming DSL from Google.

The Google plugin will be the preferred way of working with Android in multiplatform projects. When it's ready, we'll provide the necessary migration instructions so that you'll be able to use the short android name as before.

No object initialization when accessing constant values in Kotlin/Native

Starting with Kotlin 1.9.0-Beta, the Kotlin/Native backend doesn't initialize objects when accessing const val fields:

object MyObject { init { println("side effect!") } const val y = 1 } fun main() { println(MyObject.y) // no initialization at first val x = MyObject // initialization occurs println(x.y) }

Now the behavior is unified with Kotlin/JVM, where the implementation is consistent with Java and objects are never initialized in this case. You can also expect some performance improvements in your Kotlin/Native projects thanks to this change.

Ability to configure standalone mode for iOS simulator tests in Kotlin/Native

By default, when running iOS simulator tests for Kotlin/Native, the --standalone flag is used to avoid manual simulator booting and shutdown. In 1.9.0-Beta, you can now configure whether this flag is used in a Gradle task via the standalone property. By default, the --standalone flag is used so standalone mode is enabled.

Here is an example of how to disable standalone mode in your build.gradle.kts file:

tasks.withType<org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest>().configureEach { standalone.set(false) }

How to update to Kotlin 1.9.0-Beta

Install Kotlin 1.9.0-Beta in any of the following ways:

  • If you use the Early Access Preview update channel, the IDE will suggest automatically updating to 1.9.0-Beta as soon as it becomes available.

  • If you use the Stable update channel, you can change the channel to Early Access Preview at any time by selecting Tools | Kotlin | Configure Kotlin Plugin Updates in your IDE. You'll then be able to install the latest preview release. Check out these instructions for details.

Once you've installed 1.9.0-Beta don't forget to change the Kotlin version to 1.9.0-Beta in your build scripts.

Last modified: 02 June 2023