Published Jul 24. 2023 | Marcel Kulina

Kotlin Multiplatform for Mobile: The Future of Cross-Platform Development

| Why use Flutter or React when you can be truly native?

note

This article was orginally released on medium on Apr 23 2023.

What is Kotlin Multiplatform for Mobile?

Kotlin Multiplatform for Mobile(KMM) is a technology that enables developers to write shared code for iOS and Android platforms using just Kotlin. It allows developers to share common code between both platforms while also allowing platform-specific code to be written for each platform without leaving the IDE or language. KMM aims to reduce development time and costs by providing a single codebase for both platforms. The fundamental idea of KMM is that apps are still native, by using native languages and tooling for the UI, while keeping the business logic shared.

How does it work?

KMM works by providing a common language for both iOS and Android platforms. Developers write code in Kotlin and then use platform-specific libraries to access platform-specific features. KMM also includes a plugin for Android Studio and IntelliJ IDEA that allows developers to easily create Kotlin Multiplatform projects.

Shared Code

When the code for both platforms is identical, only a single implementation is needed and that implementation is usable from Android and iOS right away. Especially when there is a lot of business logic, KMM can reduce code duplication, that would happen if both platforms implemented the same business logic in each language.

Expect and Actual

One of the key features of KMM is the ability to write actual and expect declarations. In this article, we’ll explain what actual and expect are in KMM and how they are used.

Expect Declarations:

Expect declarations are declarations that define a common API for shared code that is expected to be implemented on specific platforms. An expect declaration is similar to an interface in Kotlin or a protocol in Swift. It defines a contract that must be implemented by platform-specific code.

expect class UserService() {
    fun getUsers()
}

In this example, we have defined an expect class called UserService. This class has a single method called getUsers() that must be implemented by the platform-specific code. The calling code does not know or care what the actual implementation is. In fact, the actual definition is compiled and replaced per platform.

Actual Declarations:

Actual declarations are declarations that provide the implementation for expect declarations. Actual declarations are platform-specific and provide the implementation for the common API defined by the expect declaration.

Here is an example of an actual declaration:

actual class UserService{
    actual fun getUsers() {
        // platform-specific implementation
    }
}

Note how the actual code looks identical to the expect code, expect having a body. Each platform needs its own actual implementation for any expect declaration.

Expect and Actual is an awesome feature. All native APIs are directly usable from within Kotlin, so if there is the need for different implementations per platform, it’s easy to just import these in the corresponding actual part of the code.

Advantages & Limitations

Advantages

The main advantage of KMM is that it allows developers to write a single codebase that can be used on both platforms. This not only reduces development time and costs but also improves code consistency and maintainability. Developers can write shared business logic, data models, and networking code while still being able to write platform-specific code for each platform.

Limitations

While KMM has many advantages, it also has some limitations. The technology is relatively new, and there are not many third-party libraries available for it. It also requires a deeper understanding of Kotlin and platform-specific libraries, which may make it difficult for some developers to adopt. Especially the lack of proper library integration for iOS can become problematic.

Conclusion

Kotlin Multiplatform for Mobile is a promising technology that offers a lot of potential for mobile app development. It provides a single codebase for both iOS and Android platforms, reduces development time and costs, and improves code consistency and maintainability. While it has some limitations, the benefits of Kotlin Multiplatform for Mobile outweigh the disadvantages, making it a great option for cross-platform mobile development.

Where To Go From Here

  • Explore the KMM documentation: The official KMM documentation is a great resource to learn more about KMM, including advanced topics such as interop with Objective-C and Swift, advanced Kotlin features, and building custom Gradle tasks. The documentation can be found here: https://kotlinlang.org/docs/multiplatform.html
  • Learn about testing: Testing is an important part of the development process, and KMM provides tools for testing your shared code. You can learn more about testing in KMM by reading the documentation or exploring sample projects.