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 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.
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.
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.