Boost your native development with Kotlin Multiplatform Mobile
KMM Architecture #1
When people hear about the KMM for the first time they usually ask the same major question: “How is it different from Flutter or React Native”. And this is a very valid one ☝️
I myself was very skeptical about another cross-platform technology. After some time spend with different mobile solutions including fully native apps, Flutter, React Native, Xamarin, Ionic and PWA it gave me an impression that there is no need for more.
But there is a new world between native and cross-platform
It was a huge surprise when I discovered what KMM actually is. JetBrains, creators of the Kotlin language, have found a completely new way how we can build mobile applications.
The main difference between KMM and other cross-platform solutions is the same as a difference between library and framework.
Flutter and React Native are frameworks
Technologies like Flutter or React Native are complete frameworks which allow us to build whole mobile applications. Frameworks isolate us from the native platform by introducing a layer of abstraction between our codebase and the system APIs. They put us in a sandbox where we need to follow the rules dictated by the framework.
These sandboxes usually simplify an app development process by offering abstractions which are easier to use compare to the original native implementations. But on the other hand if we want to use some native functionality it has to be supported by the abstraction layer.
KMM is a library
And KMM is not the next framework. It is a much simpler tool which allows us to build multiplatform libraries, not whole applications. With the KMM we don’t build a KMM app. We build two separate native applications, one for Android and second one for the iOS. We still use Android Studio and Activities for the first one and XCode and UIControllers for the second one. And what’s more, we have a full access to all the native tools and solutions which are available for the native applications.
The only thing which we are using the KMM for is extracting selected parts of our codebase to the Multiplatform libraries. Imagine that your two native applications use the same backend API. They reach the same endpoints and they parse the same JSONs to the same data models.
With the KMM we can just put data models and JSON parsing logic in the KMM module, implement it once and then plug-in to both Android and iOS apps as a simple library. We don’t need to implement a whole app with this technology.
KMM boosts the native apps, not replaces them
For me KMM is a powerful middle-ground solution between being fully native or fully cross-platform. First of all we should not consider KMM as a competition for Flutter or React Native. If you want to build a fully cross-platform app in a “write once, run everywhere” manner then of course you should choose Flutter or React Native.
However if your project requires native solutions but you still want to save some time, speed up the development and simplify the maintenance then KMM is here to help you.
The story behind this series
As a mobile engineer I’ve spend more than two years playing with the KMM. First out of the curiosity, then as an RnD, ending with production implementations as well. After finishing several KMM projects I see a lot of potential in this technology but also one important early-age problem.
Great flexibility comes with tough decisions to make
When we use Flutter or React Native a lot of things are already decided for us. These tools dictate us a specific way how to build the applications. They offer several opinionated architectural approaches where each of them is well documented and can be simply adopted in the project.
As I mentioned above, KMM is not a framework. As a result the way how we use it, how we connect it to the native app, how we structure our multiplatform code can vary greatly and it’s left to us to decide. These decisions can be very difficult to make and if make the wrong ones we can limit the benefits which KMM brings.
We need guides, good practices and architecture recommendations
And this series is going to provide them. In my projects I experimented with wide range of different approaches, architectures and 3rd party solutions. My team and I made many mistakes and learned even more lessons.
On canvas of this I would like to show you how to use KMM in your project effectively to achieve a great results, avoid problems and make both Android and iOS teams satisfied from using it.
What’s on the menu
During this series I’m going to talk about different architectural solutions which worked well in my projects and which I can can wholeheartedly recommend.
- We are going to start with high level architecture concepts where the first one is called Shared Services. It is super simple and works well when you want to share some business or data logic across platforms.
- In case you are eager to have more common code you can try to go with Shared MVI. In follows Google’s recommendations for Android but does it in a Multiplatform way with a shared View Model and fully native UI.
- Once you have an overview of a high level architecture I jump into the topic of modularization and explain you an Umbrella concept. When you want to split your KMM codebase into multiple modules you need to know how to connect it to the iOS app properly and Umbrella will help.
- After that we are going to make some coding related to the Dependency Injection. KMM does not work with Dagger or Hilt so you need to know how to make it right and without a headache.
- Modern mobile applications heavily rely on the asynchronous jobs and streams of data. The next episode explains how to use Coroutines to make them effective on both Android and iOS platforms.
- And lastly we are going to bring our beloved Testing topic. I’m going to show you how to work with the kotlin-test which replaces JUnit and how to deal with mocks without Mockito or Mockk.
KMM getting started
In the series I don’t run into the basics of the Kotlin Multiplatform Mobile. If you have already tried to build some KMM code feel free to proceed. If this is your first time with the KMM checkout an official documentation and some sample projects to have some solid foundation.