Kotlin Multiplatform Mobile

A New Era in Cross-Platform App Development

Configr Technologies
6 min readApr 9, 2024
Kotlin Multiplatform Mobile

Cross-platform development has emerged as the ideal solution, and Kotlin Multiplatform Mobile (KMM) is rapidly becoming a game-changer.

KMM lets developers write core business logic, data management, and networking code once in Kotlin and seamlessly share it between Android and iOS applications.

This article quickly examines Kotlin Multiplatform Mobile, its advantages, use cases, and how it can revolutionize your cross-platform development approach.

What is Kotlin Multiplatform Mobile (KMM)?

KMM is an SDK (Software Development Kit) that extends the cross-platform capabilities of Kotlin.

At its heart, KMM allows you to implement the non-UI portions of your mobile app in Kotlin, which can then be compiled to both Android bytecode and iOS frameworks.

With KMM, you maintain separate UI implementations using native technologies (SwiftUI for iOS, Jetpack Compose for Android), while the core of your app’s functionality resides in a shared codebase.

Key Advantages of Kotlin Multiplatform Mobile

  • Code Reusability: KMM’s primary strength is maximizing code sharing between platforms. You write vital components like business logic, network interactions, and data models once. This significantly accelerates development time and reduces the potential for platform-specific discrepancies.
  • Native Performance: Shared KMM code is compiled into native binaries for each platform. Consequently, you don’t compromise on performance as you would with hybrid frameworks that rely on web views or JavaScript bridges. Your app benefits from the full power of the underlying platforms.
  • Improved Code Maintainability: A single shared codebase leads to less code to maintain and update. Bug fixes and feature additions can often be applied in one place, impacting your Android and iOS apps simultaneously.
  • Leveraging Kotlin Expertise: Kotlin’s popularity is rising due to its conciseness, safety features, and interoperability with Java. KMM enables existing Kotlin developers to expand their skills into the iOS development realm.
  • Growing Ecosystem: KMM is supported by JetBrains, the creators of Kotlin. Although still maturing, it boasts an active community and a growing range of compatible tools and libraries.

When Does KMM Shine?

KMM offers a compelling value proposition for several use cases:

  • Apps with Heavy Business Logic: If your app involves complex data processing, algorithms, or intricate domain logic, KMM allows you to write this code platform-agnosticically.
  • Network-Centric Apps: KMM streamlines the implementation of network requests, data synchronization, and offline storage strategies within a shared module.
  • Teams with Existing Kotlin Proficiency: Organizations already using Kotlin on the backend or for Android development can easily transition knowledge and skills into KMM development.
  • New Projects with Cross-Platform Ambitions: Greenfield projects can benefit significantly from the code-sharing advantages of KMM from the outset.

Setting Up a KMM Project

Prerequisites: Ensure you have installed Android Studio (ideally the latest version with the KMM plugin), Xcode, and CocoaPods.

New KMM Project: Android Studio provides a “Kotlin Multiplatform Mobile” project template to get you started quickly.

Module Structure: A KMM project typically contains these modules:

  • shared: Platform-independent Kotlin code
  • androidMain: Android-specific Kotlin and Java code
  • iosMain: iOS-specific Kotlin and Objective-C/Swift code

Expectancy vs. Reality: Using KMM, you’ll still need to write UI code, as well as some platform-specific logic when you need to interact directly with native APIs.

Developing with KMM

Shared Code: The Heart of Your KMM Project

  • Focus on defining your core app functionality in your shared module. Let Kotlin's strengths shine, organize domain models, implement business rules, structure interactions with data sources (network or databases), and encapsulate complex algorithms your app might require.

Platform-Specific Expectations: When the shared Isn't Enough Utilize KMM's expect/actual mechanism to bridge gaps between Kotlin and the native worlds. Here's how:

  • expect Declarations: In the shared module, define functions or classes that you expect to have platform-specific implementations.
  • actual Implementations: Within the androidMain and iosMain modules, provide concrete implementations that utilize the native capabilities of Android (Java/Kotlin) and iOS (Objective-C/Swift).
  • Example: Accessing Device Information
// shared module
expect fun getDeviceName(): String

// androidMain
actual fun getDeviceName(): String {
return Build.MANUFACTURER + " " + Build.MODEL
}

// iosMain
actual fun getDeviceName(): String {
return UIDevice.currentDevice().name
}
  • Bridging the Kotlin and Swift Worlds For seamless interaction with Swift/Objective-C code, KMM automatically generates Objective-C headers. Occasionally, you may need to write bridge code to smooth over disparities.
  • Architectural Considerations for Smooth Sailing Adopting an architectural pattern like MVVM, MVP, or Clean Architecture keeps your shared code decoupled from UI specifics. This promotes reusability and testability. Choose an architecture that aligns with your team’s preferences and existing knowledge.

Best Practices and Design Considerations

  • Gradual Adoption: If you have an existing app, consider a gradual transition to KMM. Identify shareable modules (e.g., data layer, networking) and migrate them incrementally.
  • Platform-Aware Architecture: Design your shared code with architectural patterns like MVVM or MVP to minimize its exposure to platform-specific differences.
  • Dependency Injection: Use a dependency injection framework (Koin, Kodein, Dagger Hilt) to help manage dependencies and make your shared code more testable.
  • Threading: Be aware of cross-platform threading models. Kotlin Coroutines provides excellent support for structured concurrency across platforms.

Popular Libraries in the KMM Ecosystem

  • Ktor: A powerful networking library from JetBrains for creating HTTP clients and servers.
  • Kotlinx Serialization: Multiplatform library to convert Kotlin classes into various data formats (JSON, Protobuf, etc.).
  • SQLDelight: Simplifies database interactions and leverages SQL for structured data.
  • Koin: Lightweight dependency injection framework well-suited for KMM.

Real-World Examples of KMM in Action

Several companies are already successfully leveraging KMM in production environments:

  • Netflix: The streaming giant employs KMM to improve development velocity and consistency across their platforms.
  • Quizlet: KMM enabled Quizlet to optimize the performance of their shared code, resulting in smoother experiences for Android and iOS users.
  • Cash App: The popular payment app from Square uses KMM for select features within their app.

Challenges and Limitations of KMM

  • Maturity: KMM is still relatively young compared to traditional native development. Some tooling and library support might have rough edges.
  • UI Sharing Limitations: You’ll still need to develop UIs using native technologies, although exploring libraries that attempt to bridge this gap (like Compose Multiplatform) is insightful.
  • Swift Interoperability: At times, bridging the gap between Kotlin and Swift/Objective-C can require some additional effort.
  • Learning Curve: Teams new to Kotlin and iOS development must invest time onboarding to the KMM approach.

The Future of KMM

The trajectory of KMM appears very promising. With JetBrains’ backing, ongoing community contributions, and the potential to bridge gaps like UI sharing (e.g., with Compose Multiplatform), KMM is poised to become an indispensable tool for cross-platform mobile development.

Should You Adopt KMM?

Deciding whether KMM is the right fit for your project depends on several factors:

  • Team Composition: Do you primarily have Android developers, or are you open to bringing iOS expertise alongside Kotlin experience?
  • Project Complexity: How much logic is realistically shareable between platforms?
  • Tolerance for Early Adoption: Are you comfortable working with a technology still under active development?
  • Long-Term Aspirations: Does developing a unified long-term cross-platform strategy align with your product goals?

Kotlin Multiplatform Mobile presents an exciting opportunity to streamline cross-platform development efforts.

Its ability to facilitate code reuse, offer native performance, and leverage Kotlin's strengths makes it a compelling choice for many modern mobile projects.

Kotlin Multiplatform Mobile

As the KMM ecosystem matures, it has the potential to redefine how we approach building apps for both Android and iOS.

Follow me on Medium, LinkedIn, and Facebook.

Clap my articles if you find them useful, drop comments below, and subscribe to me here on Medium for updates on when I post my latest articles.

Want to help support my future writing endeavors?

You can do any of the above things and/or “Buy me a cup of coffee.

It would be greatly appreciated!

Last and most important, have a great day!

Regards,

George

--

--

Configr Technologies

Empowering Your Business With Technology Solutions That Meet Your Needs!