Kotlin Multiplatform —Firebase + MVVM
When I started researching about Kotlin Multiplatform, the idea of using Firebase in my Multiplatform projects soon occurred to me. At first, there was no library that could accomplish this, until TouchLab released its implementation to use Firebase and Firestore sharing the business logic code. This library is FirestoreKMP.
In a previous post, I talked about creating an app using Kotlin Multiplatform and moko-mvvm library developed by IceRock that that provides architecture components of Model-View-ViewModel for UI applications. Now with this knowledge, I am going to try to explain how to use FirestoreKMP to get/write data on a database in Firebase.
As in the other example, apps on Android and iOS just have to implement the observer to get the result of the action launched from the shared code, and this common code will be in charge of doing the job, in this case, read or write in Firebase.

Libraries used
- FirestoreKMP: Library wrapping the Firestore SDK for Kotlin Multiplatform with clients for Android and iOS. Shared common code can run methods that get delegated to the platform-specific SDKs.
- MOKO-mvvm: This is a Kotlin Multiplatform library that provides architecture components of Model-View-ViewModel for UI applications.
- 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: 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 in the SharedCode module with the dependencies used:
If you want to try this example you can take a look on GitHub, but to test it for yourself you must create a Project in Firebase. Below are all the steps to achieve this.
1. Firebase
Create your project in Firebase
First of all, we have to create our project in Firebase, so we navigate to the Firebase Console and create a new project, I called it “KMP_Firebase_MVVM”.
Create data in the database — Firestore
Once you have created the Firebase project, we will go to the Database section and create the data that we will read after. For this example, we are disabling the authentication to read or write in the database, I know its not a good practice but is just to show in this example.

Download Google Services Files (json & plist)
After creating the users, we are going to register the Android and iOS app to enable to get data from Firebase. We register the apps in the settings section and download the google-services.json for Android and paste it in the AndroidApp folder, and download GoogleService-info.plist for iOS.

Now, if we launch our apps, they will be registered in Firebase.
2. Presentation LAYER — Shared Code 🏗🏠
In this layer, we have the ViewModels, in this case, FirebaseViewModel, which are shared by both platforms. Here we launch the use-cases to execute the necessary tasks and collect data to transfer to the Livedata and notify the UI that something has happened.
Note that to make the async requests we use Kotlin Coroutines.
3. Data Layer — Repository Pattern (Firebase 🔥)
When ViewModel launches the use-cases, repositories are called to execute the action required. For this example, we are going to get data from Firebase directly.
Here you can see how to achieve this. Note that we are using the Touchlab library (co.touchlab.firebase.firestore.*)
4. Presentation LAYER — Android & iOS 📱
Both on Android and iOS we will have to listen changes on LiveData and draw the FirebaseUser list in a RecyclerView(Android) and a TableView(iOS):
Android - MainAcitivity.kt:
iOS — ViewController.swift:
Through this steps I hope you are able to create an app for both platform (Android & iOS) sharing all the business logic to read or write in Firebase. Besides, I hope it helps you to know a bit more about Kotlin Multiplatform and all the available possibilities.
That’s all folks! Feel free to reuse it in your projects and comment on what you consider appropriate. 👏🏼
🙋🏻♂️ Other Multiplatform Examples
By last if you want to try other Kotlin Multiplatform examples, you can take a look at these other projects: