Kotlin 1.5.30-RC With Java Toolchains in Gradle, JS IR Backend in Beta, Stdlib Improvements, and More

The 1.5.30 release is approaching, and now we’re presenting the release candidate with the updated opt-in requirements, Kotlin/JS IR backend in Beta, support for Java toolchains in the Gradle plugin, improvements to the Regex and Duration API in the standard library, and more. Try it and report any issues you face to help us prepare the final release.

We’ve unveiled the first set of Kotlin 1.5.30 features with the 1.5.30-M1 preview. In this post, we’ll briefly describe some more of the new features that you can try with Kotlin 1.5.30-RC:

Try new features

Opt-in requirements

Kotlin’s opt-in requirement mechanism has been available in the Experimental state for quite some time, and releasing it as Stable is on our roadmap. In 1.5.30, we’re changing the behavior of the opt-in requirements to make it more consistent and less error-prone:

  • An opt-in is now required for implicit usages of the marked API, for example, if it’s used as a function’s return type.
  • Opt-in annotations are now prohibited on more declarations, including local variables, value parameters, backing fields, and getters.
  • Overriding methods can only have opt-in annotations that are present on their basic declarations.
  • Opt-in annotations can’t have TYPE and TYPE_PARAMETER targets.

Instantiation of annotation classes

The ability to create instances of annotation classes by calling their constructors is another improvement on the Kotlin roadmap. In 1.5.30-RC, we’re adding the opt-in support for this ability to the JVM.

To allow creation of annotation instances with constructors, switch to the language version 1.6 by adding the -language-version 1.6 compiler option. You can find more details about this feature in this KEEP.

JS IR backend in Beta

The IR-based backend for the Kotlin/JS compiler has been in Alpha since Kotlin 1.4.0. We never stopped working on it, and in 1.5.30 it will become Beta. This means that we don’t expect breaking changes anymore and its future development will be mainly focused on stabilization. 

How to migrate

Gradle: Java toolchain support and JVM options for Kotlin daemon in the build script

Gradle 6.7 introduced the support for Java toolchains – an easy way to select a JDK for the project compilation. Just declare the version you need in the build script, and Gradle does the rest, finding it on your host or even downloading and installing it if it’s not there yet.

Kotlin 1.5.30-RC enables Java toolchain support for Kotlin compilation tasks:

kotlin {
    toolchain {
        (this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(<MAJOR_JDK_VERSION>)
    }
}

Other Gradle-related improvements include new ways to provide JVM options for the Kotlin Gradle daemon. Now you can specify them in a separate line in gradle.properties:

kotlin.daemon.jvmargs = "-Xmx486m -Xms256m -XX:+UseG1GC"

or in build.gradle.kts:

kotlin {
   kotlinDaemonJvmArgs = listOf("-Xmx486m", "-Xms256m", "-XX:+UseG1GC")
}

Regex and Duration API improvements

Kotlin’s Regex API is receiving new experimental functions:

  • matchesAt() checks if a regex has a match in the specified position of a String. It’s accompanied by matchAt() that returns the match itself if it is found.
val releaseText = "Kotlin 1.5.30 is coming!"
// regular expression: one digit, dot, one digit, dot, one or more digits
val versionRegex = "\d[.]\d[.]\d+".toRegex()
println(versionRegex.matchesAt(releaseText, 7)) // "true"
println(versionRegex.matchAt(releaseText, 7)?.value) // "1.5.30"
  • splitToSequence() – a lazy counterpart of split() –  splits the string around matches of the given regex, but returns the result as a Sequence. A similar function is also added to CharSequence.
val phoneNumber = "+7 (123) 456-78-90"
val regex = "[ ()-]+".toRegex()
val parts = phoneNumber.splitToSequence(regex)
// or
// val parts = regex.splitToSequence(phoneNumber)

// any processing operation on parts are executed lazily

The experimental Duration API has also been updated in 1.5.30-RC:

  • The output of Duration.toString() has become more readable. For example, Duration.minutes(2415).toString() produces 1d 16h 15m instead of 40.3h.
  • Duration also receives new functions for parsing from strings:
    • parse() parses Duration objects from strings formatted as Duration’s toString() or strings representing ISO 8601 durations (such as toIsoString() outputs).
    • parseIsoString() parses Duration objects from strings representing ISO 8601 durations. Both parsing functions come with *OrNull() counterparts.
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

@ExperimentalTime
fun main(){
    val isoFormatString = "PT1H30M"
    val defaultFormatString = "1d 1h 15m"
    println(Duration.parseIsoString(isoFormatString)) // "1h 30m"
    println(Duration.parse(defaultFormatString)) // "1d 1h 15m"
    //println(Duration.parseIsoString(defaultFormatString)) // throws exception
    println(Duration.parseIsoStringOrNull(defaultFormatString)) // "null"
}

Try the new features and report issues

All Kotlin 1.5.30 features (including those presented in the 1.5.30-M1 preview) are available in the release candidate 1.5.30-RC. You can easily install it in your IDE – IntelliJ IDEA or Android Studio – in any of these ways: 

  • If you use the Early Access Preview update channel, the IDE will suggest updating to 1.5.30-RC automatically once it is out.
  • 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, then you’ll be able to install the latest preview release. Check out these instructions for details.

You can always download the latest versions of these IDEs to get extensive support for Kotlin:

  • IntelliJ IDEA for developing Kotlin applications for a variety of platforms.
  • Android Studio for developing Android and cross-platform mobile applications.

Note: the latest version of IntelliJ IDEA – 2021.2 – doesn’t support Kotlin 1.5.30 previews. If you have already upgraded to this version, you can easily install the previous version (2021.1) using the JetBrains Toolbox App and try Kotlin 1.5.30-RC in that version.

Once you’ve installed 1.5.30-RC, change the Kotlin version to 1.5.30-RC in your build scripts. 

If you run into any problems:

Read more