Skip to main content

A Developer’s Roadmap to Mastering Kotlin Multiplatform

Jaewoong EumNovember 27, 2024About 9 minJavaKotlinAndroidArticle(s)blogdroidcon.comjavakotlinandroid

A Developer’s Roadmap to Mastering Kotlin Multiplatform ꎀ렚

Android > Article(s)

Article(s)

A Developer’s Roadmap to Mastering Kotlin Multiplatform
In modern mobile development, cross-platform frameworks are gaining popularity because they offer key advantages, such as reducing the resources needed to develop separate native apps and maintaining code consistency across different platforms.

In modern mobile development, cross-platform frameworks are gaining popularity because they offer key advantages, such as reducing the resources needed to develop separate native apps and maintaining code consistency across different platforms.

By allowing developers to write shareable code, these frameworks enable faster development and easier maintenance, making them attractive not only to startups and large companies but also to individual developers. With a single codebase, it’s possible to build multiple platform-specific apps, making cross-platform solutions highly efficient for both time and cost management.

Kotlin Multiplatform (KMP) is another rising star in cross-platform development driven by the collaboration between JetBrains and Google. It allows developers to share business logic seamlessly across multiple platforms, including Android, iOS, Desktop, and Web, all while using Kotlin. Compared to other cross-platform solutions like React Native or Flutter, Kotlin Multiplatform offers performance that is closer to native, making it an attractive choice for developers seeking efficiency and native-like performance without sacrificing the benefits of cross-platform development.

In this article, you’ll explore the Kotlin Multiplatform (KMP) ecosystem using the Kotlin Multiplatform Developer Roadmap (skydoves/kmp-developer-roadmap) as your guide. The roadmap is designed to offer a comprehensive overview of the current KMP ecosystem, which provides suggested learning paths to help you better understand the various concepts involved in KMP development.

skydoves/kmp-developer-roadmap
đŸ—ș The Kotlin Multiplatform Developer Roadmap offers comprehensive learning paths to help you understand KMP ecosystems.

Kotlin Multiplatform Architecture

Kotlin Multiplatform doesn’t inherently provide a UI framework. Instead, it focuses on sharing business logic across multiple platforms while enabling developers to create platform-specific UIs for Android, iOS, and other targets. The core idea is to write common logic in Kotlin once and reuse it across platforms like Android, JVM, iOS, macOS, JavaScript, and Linux, while maintaining full control over each platform’s native UI. This approach provides flexibility, as demonstrated in the illustration below:

Let’s jump into the architecture with some code exploration! Before setting up your first Kotlin Multiplatform (KMP) project, ensure the following prerequisites:

Kotlin Multiplatform - IntelliJ IDEs Plugin | Marketplace
The Kotlin Multiplatform plugin helps you develop applications that work on both Android and iOS. With the Kotlin Multiplatform plugin for Android Studio, you can...

After meeting these requirements, open the Kotlin Multiplatform Wizard. This will generate a project structure similar to the one below, giving you a foundation for cross-platform development.

After selecting Android, iOS, and Web, the Kotlin Multiplatform Wizard downloads a project pre-configured with a multiplatform architecture. You can then open the composeApp module’s build.gradle.kts file to view the platform setup. This file outlines each targeted platform’s configuration, dependencies, and shared code structure, helping you better understand the cross-platform architecture and how shared business logic integrates with platform-specific code below:

In the code above, you can configure target platforms and create hierarchical structures by grouping similar platforms, like iosX64 and iosArm64. This setup allows you to share common code between similar platform targets, simplifying maintenance and reducing redundancy.

You can set up the targets below to configure KMP project platform architecture:

Each target broadens the reach of your KMP project by allowing the core business logic to run across different operating systems and architectures. This flexibility helps maximize code reuse while supporting the specifics of each platform’s native capabilities.

If you’re ready to dive into building your first Android and iOS mobile applications with Kotlin Multiplatform, check out this guide: Build Your First Android and iOS Mobile App With Kotlin Multiplatform.

Build Your First Android and iOS Mobile App With Kotlin Multiplatform
Explore how to set up and build your first Android and iOS application with Kotlin & Compose Multiplatform.

Share Code

Kotlin Multiplatform lets you define a common interface specification across multiple platforms, unifying the interface hierarchy while allowing platform-specific implementations. To achieve this, you can use expected and actual declarations, which enable access to platform-specific APIs from Kotlin Multiplatform modules. This approach allows you to maintain platform-agnostic APIs in the common code, simplifying cross-platform development.

The expect keyword is used in the shared (common) code to declare a function, class, or property without defining its implementation. This acts as a placeholder, allowing you to specify the API that platform-specific modules will need to implement. For example, if you want to declare a shared function that logs across different platforms, you can declare a function called log like the code below:

// In common module
expect fun log(message: String)

The actual keyword is used in platform-specific modules to provide the implementation of the expect declarations. For each platform (e.g., Android, iOS), you create the actual implementation of the expected functionality. For instance, if you want to provide the actual implementation for the expected log function, you can define it differently for each specific platform, as shown in the example below:


// In Android module
actual fun log(message: String) {
  Log.d("TAG", message) 
}

// In iOS module
actual fun log(message: String) {
  NSLog(message)
}

Now, you can use the log function across the common module. This approach provides flexibility by allowing platform-specific code without altering the common code. Additionally, it enables you to maintain platform-independent logic in the common module, maximizing code reuse. For more information about the expect and actual declarations, check out Expected and actual declarations.


Compose Multiplatform

You’ve discovered that Kotlin Multiplatform is designed to share business logic across platforms but doesn’t include any UI solutions by default. This can be confusing, as most cross-platform frameworks provide UI components or layers that enable building screens with a unified codebase across different platforms.

A great solution for cross-platform UI in Kotlin Multiplatform is Compose Multiplatform, which builds on Kotlin Multiplatform and enables developers to share UI code written in Jetpack Compose across various platforms. JetBrains has forked the Jetpack Compose library (for Android) into the JetBrains/compose-multiplatform repository and created compatible Compose UI clients for multiple platforms, including iOS, Desktop, and WebAssembly (WASM).

JetBrains/compose-multiplatform
Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.

With Compose Multiplatform, you can develop both Android and iOS applications using a unified UI implementation while sharing business logic written in Kotlin. Currently, Android and Desktop (Windows, macOS, Linux) support is stable, iOS is in beta, and Web is in alpha. For more details on future plans, check out the Kotlin Multiplatform Development Roadmap for 2025.


AndroidX Library Compatibility

If you’re an Android developer, you might be wondering about the best ways to minimize migration costs from native Android to Kotlin Multiplatform, and that must be using the same tech stacks as much as possible, such as Jetpack libraries. The Android team is aware of this and has begun officially supporting KMP for several Jetpack libraries, as listed below:

Jetpack library releases for Android and iOS meet strict quality and compatibility standards. As Jetpack expands its Kotlin Multiplatform (KMP) support to other platforms, however, tooling and infrastructure are still developing. Check out the sample project on GitHub (android/kotlin-multiplatform-samples) for KMP-supported Jetpack libraries.

android/kotlin-multiplatform-samples
Samples showcasing the experimental Kotlin Multiplatform Jetpack libraries

According to the official documentation, Jetpack libraries categorize platform support into three tiers:

For additional information on Kotlin Multiplatform support for Jetpack libraries, refer to the Kotlin Multiplatform Overview.


Asynchronous

Asynchronous solutions are essential in mobile development due to limited resources compared to desktops. Both Android and iOS use the main thread (or UI thread) to handle all UI-related tasks, such as rendering elements, dispatching events, and managing interactions within the user interface.

For I/O or computationally intensive tasks like network requests or database queries, it’s best to offload them to a worker thread. This keeps the main thread free for rendering and user interactions, ensuring a responsive UI. In Kotlin, Coroutines provide a powerful asynchronous solution, supported at the language level and enhanced through libraries, making it ideal for efficient concurrency.

Coroutines fully support Kotlin Multiplatform, allowing you to use them seamlessly across multiple platforms. They offer a lightweight concurrency solution with robust error-handling APIs, providing greater flexibility than traditional threads. This makes Coroutines one of the most promising asynchronous solutions for Kotlin Multiplatform development.

If you’re an avid ReactiveX user, consider exploring Reaktive on GitHub (badoo/Reaktive), which brings ReactiveX extensions to Kotlin Multiplatform.

badoo/Reaktive
Kotlin multi-platform implementation of Reactive Extensions

Network

On Android, Retrofit and OkHttp are the go-to solutions for handling type-safe HTTP network requests, streaming, and more. However, they don’t support Kotlin Multiplatform. Instead, there is another excellent HTTP asynchronous library r, designed for creating multiplatform microservices and HTTP clients. Ktor is lightweight, flexible, and fully compatible with Coroutines, making them ideal for Kotlin Multiplatform projects.

Ktor is an asynchronous framework created by JetBrains for building applications and microservices in Kotlin. It supports both client-side and server-side development, making it versatile for building HTTP clients, REST APIs, web applications, and microservices across multiple platforms, including Android, JVM, JavaScript, and iOS. The key features of Ktor are:

  1. Asynchronous by Design: Built with coroutines in mind, Ktor is fully asynchronous, leveraging Kotlin’s coroutines to provide high concurrency with minimal overhead.
  2. Multiplatform Support: Ktor can be used in Kotlin Multiplatform projects, allowing you to write HTTP clients that work across different platforms (Android, iOS, JVM, etc.) with the same codebase.
  3. Extensible and Modular: Ktor is modular, allowing you to add only the features you need, such as authentication, serialization, WebSocket support, and more, by including individual dependencies.
  4. Flexible Routing: Ktor’s routing system is flexible, supporting path parameters, query parameters, and more. It enables organized API endpoints in server applications.
  5. Built-In Serialization: Ktor integrates with Kotlin’s serialization library, making it easy to handle JSON, XML, and other serialization formats.

On the other hand, a solution called Foso/Ktorfit is built on top of Ktor. Ktorfit is an HTTP client and Kotlin Symbol Processor designed for Kotlin Multiplatform, inspired by Retrofit. It offers a similar interface-based approach to define HTTP request methods, making it familiar for those who have used Retrofit.

A Developer’s Roadmap to Mastering Kotlin Multiplatform

In modern mobile development, cross-platform frameworks are gaining popularity because they offer key advantages, such as reducing the resources needed to develop separate native apps and maintaining code consistency across different platforms.