Why you should consider Kotlin multiplatform?

Why-you-should-consider-kotlin-multiplatform
icon
Dharmin Shah
date-time-icon
25 Feb, 2023

You may think of Kotlin as a Java-like JVM language, but JVM is only one of its available targets. Kotlin can be compiled to JavaScript (and work with React) and to native platform code with Kotlin/Native. This means that Kotlin to iOS, macOS, Linux, Windows, or even WebAssembly is possible.

Kotlin/Native lets you create an executable, a static or dynamic library with C headers, or an Apple framework. It can also use existing C, Swift, or Objective-C libraries.

In this article, I will create an Android + iOS multiplatform project. But you don’t have to stop there, because you can do all of the following with Kotlin:

Android app: Kotlin/JVM

iOS app: Kotlin/Native

Backend: Kotlin/JVM & Ktor or Spring Boot

Frontend: Kotlin/JS & React

Desktop app: Kotlin/Native

Kotlin Multiplatform Mobile

Kotlin Multiplatform Mobile is a set of tools that make it easier to create Android + iOS multiplatform apps. Just keep in mind that KMM is in alpha development and still somewhat unstable. When you install the official KMM plugin and create a new project, it’ll automatically set up these modules:

androidMain

commonMain

commonTest

iosMain

Architecture

Following the MVP pattern, the app should have three layers of code:

Model, important for data repositories like APIs or databases

View, responsible for displaying data from Presenter and passing input to it Presenter, important for processing the data for View

You want to keep your Model and Presenter in the shared common module and implement Views in both Android and iOS, where you want them to stay as small as possible.

Libraries

You cannot use Android’s View Model or Live Data in the shared common module, because it’s written in Java.  You also cannot use RxJava nor Retrofit.  This module needs to contain pure Kotlin code, which complicates things a little bit.

But Kotlin Multiplatform has a solution for that in the form of two new keywords: expect and actual, which can easily be understood as declaration and implementation.

Testing

By extracting your logic to the shared common module, you’re now able to write blazing-fast unit tests that can cover a lot of successful and unsuccessful use cases. Use mocked API responses and create scenarios that will cover both platforms at the same time!

In Summary

Kotlin Multiplatform holds a lot of promise, but it’s still in its early days.

Pros (+)

Native performance – no additional runtime in your app bundle

No legacy code – you can start integrating KMM anytime

No shared UI – pixel-perfect native views

It’s easy to just add iOS to your existing Android app

Cons (-)

Not many pure Kotlin libraries available in the shared common module

Still in alpha, not production-ready

Tags :

kotlin cross-platform kotlin multiplatform kotlin multiplatform mobile
Quick Support

Why Do You Wait?

We don't see any reason to wait to contact us. If you have any, let's discuss them and try to solve them together. You can make us a quick call or simply leave a message in our chat. We assure an immediate and positive response.

Call Us

Questions about our services or pricing? Call for support

contact +91 70165-02108 contact +91 99041-54240
chat

Chat Us

Our support will help you from  10 AM To 7 PM

chatLive chat now

Fill out the form and we'll be in touch as soon as possible.

round-shape
dot-border