Kotlin lovers have a reason to groove! Yes, the much-anticipated Kotlin 1.8.20 release is now out. With this release, developers can relish enhanced and optimized features for fluid coding.
As expected, this new release is not just about showcasing Kotlin’s commitment to improvement but also brings potential solutions for businesses to streamline their processes using advanced tools.
This post outlines the Kotlin release and how it would benefit businesses and the coder fraternity alike.
Kotlin/Wasm target
Kotlin 1.8.20 has introduced a new compilation target called WebAssembly, broadening the language’s capabilities in the web development realm.
Leveraging this feature developers can build and deploy applications that run seamlessly across multiple platforms.
WebAssembly’s integration into Kotlin significantly streamlines the development process, as it minimizes compatibility issues that often arise between different platforms.
As a result, Kotlin maintains its position at the forefront of modern programming languages and solidifies its presence in the constantly evolving technological landscape.
This opens up a wide range of possibilities, from building high-performance web applications to serverless functions.
How to enable Kotlin/Wasm
plugins { kotlin("multiplatform") version "1.8.20-RC2" } kotlin { wasm { binaries.executable() browser { } } sourceSets { val commonMain by getting val commonTest by getting { dependencies { implementation(kotlin("test")) } } val wasmMain by getting val wasmTest by getting } }
To run a Kotlin/Wasm project, you need to update the settings of the target environment
For Chrome
For version 109:
- Run the application with the –js-flags=–experimental-wasm-gc command line argument.
For version 110 or later
- Go to chrome://flags/#enable-webassembly-garbage-collection in your browser.
- Enable WebAssembly Garbage Collection.
- Relaunch your browser.
For Firefox ( for version 109 or later)
- Go to about:config in your browser.
- Enable javascript.options.wasm_function_references and javascript.options.wasm_gc options.
- Relaunch your browser.
For Edge ( for version 109 or later)
- Run the application with the –js-flags=–experimental-wasm-gc command line argument.
IDE Support
The Kotlin plugins that support kotlin latest version are now readily available for developers to access and utilize in their projects. These plugins offer enhanced functionality and improved performance compared to previous versions, ensuring a smoother development experience.
IDE | Supported Versions |
IntelliJ IDEA | 2022.2.x, 2022.3.x, 2023.1.x |
Android Studio | Flamingo (222) |
New Kotlin K2 Compiler Updates
In the latest 1.8.20 release, the Kotlin K2 compiler now includes a preview version of the serialization plugin, as well as Alpha support for the JS IR compiler.
To enable and test the Kotlin K2 compiler, use the new language version with the following compiler option:
-language-version 2.0
Developers can specify it in the build.gradle(.kts) file:
kotlin { sourceSets.all { languageSettings { languageVersion = "2.0" } } }
Language
With Kotlin 1.8.20, there are preview versions for new language features in a modern and performant replacement of the Enum class values function, data objects for symmetry with data classes, and lifting restrictions on secondary constructors with bodies in inline classes
These added features aim to enhance the overall functionality and performance of the programming language.
A modern and performant replacement of the Enum class values function
enum class Color(val colorName: String, val rgb: String) { RED("Red", "#FF0000"), ORANGE("Orange", "#FF7F00"), YELLOW("Yellow", "#FFFF00") } @OptIn(ExperimentalStdlibApi::class) fun findByRgb(rgb: String): Color? = Color.entries.find { it.rgb == rgb }
How to enable the entries property
tasks .withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask<*>>() .configureEach { compilerOptions .languageVersion .set( org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9 ) }
Preview of data objects for symmetry with data classes
package org.example object MyObject data object MyDataObject fun main() { println(MyObject) // org.example.MyObject@1f32e575 println(MyDataObject) // MyDataObject }
Semantics of data objects
data object MyDataObject { val x: Int = 3 } fun main() { println(MyDataObject) // MyDataObject }
Preview of lifting the restriction on secondary constructors with bodies in inline classes
@JvmInline value class Person(private val fullName: String) { // Allowed since Kotlin 1.4.30: init { check(fullName.isNotBlank()) { "Full name shouldn't be empty" } } // Preview available since Kotlin 1.8.20: constructor(name: String, lastName: String) : this("$name $lastName") { check(lastName.isNotBlank()) { "Last name shouldn't be empty" } } }
Kotlin/JVM
Kotlin 1.8.20-RC2 brings several exciting features to the table, improving the developers’ experience while working with the language.
One of the highlights includes a preview of Java synthetic property references, which allows for more concise and intuitive code while interacting with Java libraries.
In addition, support for the JVM IR backend in the kapt stub generating task is now enabled by default, contributing to enhanced performance and reliability of the build process.
Kotlin 1.8.20 can create references to Java synthetic properties, for example, for such Java code:
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
How to enable Java synthetic property references
For Kotlin
tasks .withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask<*>>() .configureEach { compilerOptions .languageVersion .set( org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9 ) }
For Groovy
tasks .withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask.class) .configureEach { compilerOptions.languageVersion = org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9 }
Kotlin/Native
Kotlin 1.8.20 introduces a range of updates aimed at enhancing the user experience for developers working with Kotlin/Native targets.
Among these updates is an enhancement in interoperability with Objective-C, allowing for smoother integration and cooperation between the two languages.
These modifications work together to provide a seamless development experience and contribute to the overall versatility of the Kotlin ecosystem.
Update for Kotlin/Native targets
The following targets have been deprecated with Kotlin release date and will be removed in 1.9.20:
- iosArm32
- watchosX86
- wasm32
- mingwX86
- linuxArm32Hfp
- linuxMips32
- linuxMipsel32
Deprecation of the legacy memory manager
Since the release of version 1.8.20, the legacy memory manager has been officially deprecated and is set to be removed in the upcoming 1.9.20 update.
This significant change aims to improve overall performance and enhance the user experience within the software.
Users are encouraged to transition to the new memory management system as soon as possible to ensure smooth operation and to take advantage of the upgraded functionalities.
Support for Objective-C headers with @import directives
Kotlin/Native has recently added support for importing Objective-C headers using @import directives, bringing significant improvements for developers working with Swift libraries.
This new feature is particularly beneficial when dealing with auto-generated Objective-C headers from Swift libraries or utilizing CocoaPods dependencies authored in Swift.
Support for the link-only mode in Cocoapods Gradle plugin
With the release of Kotlin 1.8.20, developers are now able to utilize Pod dependencies with dynamic frameworks solely for linking purposes, without the need for generating C-interop bindings.
It significantly streamlines the development process, as it provides a more efficient way to leverage external dependencies.
Kotlin Multiplatform
Kotlin 1.8.20 is set to improve the developer experience with:
New approach to source set hierarchy- This feature simplifies the process of managing and integrating different target platforms, streamlining the entire development workflow. By utilizing the default target hierarchy, developers can ensure a more consistent and efficient project structure across varying platforms.
Preview of Gradle composite builds support in Kotlin Multiplatform- Kotlin Multiplatform now supports Gradle composite builds, enhancing the functionality and flexibility of cross-platform projects.
Improved output for Gradle errors in Xcode- This particularly benefits the embedAndSignAppleFrameworkForXcode task, allowing seamless connection of the iOS framework from multiplatform projects to iOS applications in Xcode.
Kotlin/JavaScript
Kotlin 1.8.20 introduces significant alterations to the generation of TypeScript definitions, enhancing the overall developer experience.
Developers can now leverage the benefits of both programming languages, expanding the potential for innovative applications.
Gradle
Kotlin 1.8.20 has been designed with full compatibility for Gradle versions 6.8 through 7.6, making it a reliable choice for a wide range of development environments.
This compatibility enables seamless integration and upgradation, thus simplifying project management for developers.
However, it is important to note that some special cases may arise when utilizing the Multiplatform plugin with this version.
Standard library
Kotlin 1.8.20 has brought a variety of new features, such as:
- Support for the AutoCloseable interface
- Support for Base64 encoding and decoding
- Support for @Volatile in Kotlin/Native
- Bug fix for stack overflow when using regex in Kotlin/Native
Serialization updates
Kotlin 1.8.20 introduces a significant advancement in its progression with the addition of Alpha support for the Kotlin K2 compiler.
This latest update showcases the language’s dedication to maintaining its status as a modern and innovative programming solution. However, it is important to note the prohibition of serializer customization via companion objects in this new version.
Documentation updates
With this update, there are key documentation updates, such as:
- Get started with Spring Boot and Kotlin
- Scope functions
- CocoaPods integration
How to install Kotlin 1.8.20
- For existing users of IntelliJ IDEA 2022.2, 2022.3, IDE will suggest updating Kotlin to 1.8.20 automatically.
- For Android Studio Flamingo (222) and Giraffe (223)- this update will be delivered with upcoming Android Studios updates.
- To use a command-line compiler, it can be downloaded from the GitHub release page.
Summary
Kotlin 1.8.20 update is full of flavorful features and enhancements that will redefine the coding experience.
To get more such technology updates, keep watching this space.