Kotlin Multiplatform Mobile (KMM)
Tags: android, ios, app, kotlin
Published: 2022-05-23 09:18:41

## Basics about the KMM
Kotlin Multiplatform Mobile (KMM) is an SDK that allows us to use the same business logic code in both iOS and Android applications. It offers all the combined benefits of creating cross-platform and native apps. It reduces the time needed for writing and maintaining the same code for different mobile platforms while retaining the flexibility and benefits of native programming. With Kotlin, we can use a single codebase for the business logic of iOS and Android apps and write platform-specific code only where necessary in order to implement a native UI or when working with platform-specific APIs.

## Benefits
Share code for logic elements that often fall out of sync while keeping the advantages of native programming, including great app performance and full access to the Android and iOS SDKs.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/924rmmpi4yoqrydf42r5.png)

## How KMM works
An app consists of 4 main layers that includes UI logic layer, presentation logic layer, business logic layer, and data Layer. The data layer which has classes with the responsibility of making network calls and database queries can be 100% shared. The presentation and UI logic can be partially shared depending upon the device types and other platform specific peculiarities.

KMM can seamlessly be integrated with the mobile project. Shared code, written in Kotlin, is compiled to JVM bytecode with Kotlin/JVM and to native binaries with Kotlin/Native, so you can use your KMM business-logic modules just like any other regular mobile library.

Kotlin harbours multiplatform libraries that can reuse the multiplatform logic in common and platform-specific code such as HTTP, Serialization etc. To interoperate with platforms, platform-specific versions of Kotlin can be used. Platform-specific versions of Kotlin (Kotlin/JVM, Kotlin/JS, Kotlin/Native) include extensions to the Kotlin language, and platform-specific libraries and tools. Through these platforms one can access the platform native code (JVM, JS, and Native) and leverage all native capabilities.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1a3ndknounjfo428ym9.png)

## Case studies
- Quizlet migrated their business logic from a shared JavaScript approach to KMM and drastically improved the performance of both their Android and iOS applications.
- Fastwork introduced KMM for the domain and data layers of their application to share business logic and API service between mobile platforms, significantly boosting their team’s productivity.
- Yandex.Disk started out by experimenting with the integration of a small feature, and when the experiment proved successful, they implemented their whole data synchronization logic in KMM.

- Also, there are big projects that are using KMM in production ( i.e Netflix, Phillips, and more ) and probably it was not mentioned but Unit Testing is also possible using KMM.

## The project structure
As you can see below, the project is divided into three modules:
- androidApp—contains all the native code for the Android application
- iosApp—contains all the native code for the iOS application
- shared—contains all the shared code (eg business logic) to be used in both the Android and iOS platform
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwxfsxr6uv7vpm7qiyql.png)

## Benefits of using KMM
- Reduced development time and effort
- Improved code quality
- No Impact on performance
- Easy to use 
- UI can be split for each target platform – the app will feel consistent with any given ecosystem
- Shared logic allows developers to add new features and fix bugs on both operating systems at the same time
- Easy to integrate: Whether it’s a fresh project or already existing project you can start integrating KMM anytime.
- Easy to learn: Kotlin syntax is very similar to other popular languages such as Swift and Java. So it’s pretty easy to learn and get started with KMM.

## Finish line!
And that's it for now. KMM is a good option if there is something that needs to be shared on different mobile platforms, as new options there are limitations that progressively will be fixed.

Kotlin is an awesome language and Kotlin Multiplatform brings that to other platforms to help us create scalable and maintainable apps using a single codebase. Although the code-sharing is slightly less than that with other cross-platform solutions as UI cannot be shared, that gives us the advantage of creating a user experience that is in line with the platform’s guidelines and best practices.

Basically think about KMM as a new option to share code between platforms that will be improved by the Kotlin community and you have the control of what needs to be shared and what’s not.
 
Let’s Kotlin!