Kotlin 2.0.0 Officially Released with Multiple Enhancements

TapTechNews May 22nd news, JetBrains officially announced the formal release of Kotlin 2.0.0, and its code has been uploaded to the GitHub repository.

 Kotlin 2.0.0 Officially Released with Multiple Enhancements_0

Kotlin 2.0 Highlights

K2 CompilerNow in Stable State:

Default for all platforms (JVM, Native, Wasm, and JS), and with significant performance enhancements. In terms of performance, the JetBrains team compiled 10 million lines of code to ensure its quality stability, involving more than 18,000 developers and over 80,000 projects.

 Kotlin 2.0.0 Officially Released with Multiple Enhancements_1

IDE Support:

The new Kotlin plugin has been integrated into IntelliJIDEA and AndroidStudio.

The New Compose Compiler Has Been Integrated with the Gradle Plugin:

Starting from Kotlin 2.0.0, the org.gradle.jvm.environmentGradle property will be defaulted to be announced along with the Kotlin version to better distinguish the JVM and Android versions of the Kotlin multi-platform libraries.

Kotlinx-metadata-jvm Library:

The Kotlinx-metadata-jvm library is now in stable state

Lambda Function:

Kotlin 2.0.0 introduces a new default method for using generated lambda functions, invokedynamic, which can generate smaller binary files.

Monitoring GC Performance in Kotlin/Native Using Markers (signposts) on the Apple Platform:

Previously, developers could only monitor the performance of the Kotlin/Native garbage collector (GC) by looking at the logs. However, these logs were not integrated into XcodeInstruments (a toolkit for debugging performance issues in iOS applications). Starting from Kotlin 2.0.0, the GC will report pauses through the special markers provided in Instruments, allowing for custom logging within the application.

Using Objective-C Methods to Resolve Conflicts in Kotlin/Native:

Objective-C methods can have different names but must have the same number and type of parameters. For example, locationManager:didEnterRegion: and locationManager:didExitRegion:. In Kotlin, they all have the same signature, so attempting to use them also triggers a conflict overload error.

In the past, developers had to manually resolve the conflicts to avoid this compilation error. To improve the interoperability between Kotlin and Objective-C, Kotlin 2.0.0 introduces the new @ObjCSignatureOverride annotation. This annotation instructs the Kotlin compiler to ignore the conflicting overloads to prevent multiple functions with the same parameter types but different parameter names from being inherited from an Objective-C class.

Support for Named Exports in Kotlin/Wasm:

Support for named exports and unsigned primitive types' @JsExport functions, which improves readability and helps developers better manage dependencies between modules, and also makes code sharing between Kotlin and JavaScript modules easier.

In addition, Kotlin 2.0.0 also supports generating TypeScript declaration files in Kotlin/Wasm (TapTechNews note: experimental feature, may be deleted at any time), which can generate TypeScript definitions based on the declarations in the @JsExportKotlin code, so that IDE and JavaScript tools can use these definitions to provide code auto-completion functions.

Default Use of Binaryen to Optimize Production Versions:

The Kotlin/Wasm toolchain now applies the Binaryen tool by default to all projects during the production compilation process without the need for manual settings. The official estimate is that this will further improve the runtime performance of the project and reduce the binary file size. Of course, this change only affects the production compilation. The development compilation process remains the same.

New GradleDSL Experimental Feature for Compiler Options in Multi-Platform Projects (May Be Deleted at Any Time):

Kotlin 2.0.0 introduces a brand new GradleDSL that can be used to more easily configure compilation options in multi-platform projects. Previously in Kotlin, using Gradle to configure compilation options could only be for lower levels such as individual tasks, compilation processes, or source code sets. With this new GradleDSL, you can directly set the global configuration compilation options.

Stable Way to Replace Generic Function for Enumeration Class Value

In Kotlin 2.0.0, the way to access the values of an enumeration class becomes more stable. Kotlin 2.0.0 introduces a new dedicated stable function enumEntries<T>(), which can return a list of all enumeration entries of a given enumeration type T.

Kotlin previously also introduced a property entries of the enumeration class, which is now in stable status and will replace the no longer recommended values() together with the above function.

Stable AutoCloseable Interface

Starting from Kotlin 2.0.0, the commonly used AutoCloseable interface by developers is announced to be in stable status, which allows developers to easily close resources and contains two useful functions:

use() extension function, used to execute a given block function on the selected resource and ensure that the relevant resource can be correctly closed regardless of whether an exception is thrown.

AutoCloseable() constructor, used to create an instance of the AutoCloseable interface.

Likes