Kotlin Multiplatform — MVVM & Clean Architecture
During the KotlinConf 2019 there were several talks about Kotlin Multiplatform, in one of them Alexander talked about different libraries developed by IceRock to achieve MVVM pattern, share resources or widgets using the same code on different platforms (Android, iOS, JS or web).
This talk sparked my curiosity to try the MVVM pattern + LiveData for Android and iOS apps and Clean Architecture on the common shared code.
So I decided to get down to work. On this GitHub repository you could check how I have developed an example of app using MVVM + live data on both platforms (Android & iOS):
Our target could be summarized on the next schema. Apps on Android or iOS just have to implement the observer to get data from the sharedCode. And SharedCode will be in charge of get data, from disk or network through a repository pattern.

Libraries used (Updated to Kotlin 1.4.31)
- MOKO-mvvm: This is a Kotlin Multiplatform library that provides architecture components of Model-View-ViewModel for UI applications.
- Ktor: In addition to HTTP serving, Ktor also includes a flexible asynchronous HTTP client. This client supports several configurable engines, and has its own set of features.
- Serialization: Kotlin serialization consists of a compiler plugin, which automatically produces visitor code for classes, and runtime library, which uses generated code to serialize objects without reflection.
- Kodein-DI: Kodein is a very simple and yet very useful dependency retrieval container. It is very easy to use and configure.
- Kotlinx.coroutines: Library support for Kotlin coroutines with multiplatform support
Here you can see the common build.gradle file with the dependencies used:
For more details about dependencies navigate here.
Presentation LAYER — Android & iOS 📱
How we said before, on both platforms (Android & iOS), we only have to implement an observer where the the corresponding viewModel bring us the data. Here you can see the implementation on Android (Kotlin) and iOS (Swift):
Presentation Layer — Shared Code 🚀
This layer is shared by Android & iOS, and this is developed on Kotlin. Here is where we have to call the different use-cases of the domain layer. We are using an implementation of ViewModel developed by IceRock. To make the call async we are using kotlin coroutines. Here you can see an example on how to obtain user repositories from GitHub.
Domain Layer — Model & UseCases 🏗 🏭
Here we are defining our model (GitHubRepo.kt) and all the use-cases that we need for our application. In this case we only have a use-case to obtain the repo list and its request.

Data Layer — Repository Pattern 📡 💾
For this layer we are using a repository pattern to decide which is the source of our data.
For networking we are using Ktor and for JSON deserialisation Kotlinx serialization.
For now, we don’t have included disk source, but on next stories we will explain how to use SQLDelight to achieve it.
Update 04/2021:
- Migrate to Kotlin 1.4.31. To see changes, please click here. Be careful importing Kotlin Serialization dependency, so this dependency is imported inside KTor and could trigger some problems.
- Added SQLDelight to save in database UserEntity. You can see this changes here.
- Unit Tests: added some unitTests to test GitHubRepository. Changes here.
🙋🏻♂️ Other Examples❗️
If you want to take a look at an example of Kotlin Multiplatform for Android, iOS, Web and Desktop implementation go to the next GitHub repository. This app shows how to create a simple Android/iOS/JVM/JS project sharing Kotlin code. This app saves on a local database (SQLDelight) your favorites locations and get the current weather of them from OpenWeatherMap API (using KTOR).
Here you can see the talk “Kotlin Multiplatform in action” by Alexandr Pogrebnyak:
👏🏼 Thanks to Alexandr Pogrebnyak and IceRock dev team for your support. 👏🏼