JetBrains offers first stable release of Kotlin Multiplatform

JetBrains offers first stable release of Kotlin Multiplatform

JetBrains has made Kotlin Multiplatform available in its first stable release, enabling code sharing across iOS, Android, desktop, web and server – though the piece for sharing user interface code, Compose Multiplatform, is stable only for Android and desktop.

Kotlin is a JVM (Java Virtual Machine) language developed by JetBrains and adopted by Google as the primary language for Android development. Since Java is already designed for cross-platform code, what does Kotlin Multiplatform add? The answer is that Kotlin is not only a JVM language. Kotlin/Native compiles to self-contained executables for macOS, iOS, Linux, Windows with MinGW (a GCC toolchain for Windows), and the Android NDK. Kotlin/JS transpiles Kotlin to JavaScript. Kotlin Wasm, still experimental, compiles to WebAssembly. 

Kotlin Multiplatform is a technology for sharing non-GUI code across all these platforms. It addresses two common issues with code-sharing, these being the need for some code being only partially shared, for a subset of all the target platforms, and the need to access platform-specific APIs. 

Expect and Actual in Kotlin Multiplatform, addressing the need to call native platform APIs

The native API problem is addressed via a mechanism of expected and actual declarations. The expect keyword marks a declaration as one that will be matched with code marked with the actual keyword, that may be platform specific. JetBrains recommends using expect/actual only for platform APIs, otherwise using normal interfaces.

A survey in 2022 showed code sharing of up to 63% for Kotlin Multiplatform applications

Kotlin Multiplatform is for non-GUI code but there is a related project, Compose Multiplatform, which is for created shared UIs. Compose Multiplatform is based on Google’s Jetpack Compose, for building a UI for Android. Compose Multiplatform is also stable for desktop platforms, macOS, Linux and Windows, but Alpha for iOS and experimental for Web. According to JetBrains, Kotlin Multiplatform has already been used by companies including Netflix and VMWare.

If Compose Multiplatform is not yet ready, how do developers support iOS? This is possible by using SwiftUI, Apple’s official UI design language which is based on declarative code. There are code samples which take exactly this approach.

JetBrains also said that it is aiming to deliver a beta of Compose Multiplatform for iOS in 2024, and that Kotlin/Wasm is under active development too. 

Last year, Google expressed some support for Kotlin Multiplatform by introducing an “experimental preview” of some other Jetpack libraries, not for use in production but for “feedback about using these Jetpack libraries in multiplatform projects targeting Android and iOS applications.” Note that Google also offers Flutter for cross-platform development using the Dart language and Flutter UI.

Another reason for using Kotlin rather than Java is for its language features, including lambda expressions, extension functions, type inference, null safety, and more. Kotlin does not have checked exceptions, on the grounds that these do not increase productivity or code quality.

Kotlin Multiplatform and Compose Multiplatform are free to use. The Kotlin code is on Github and the license is Apache 2.0.