Getting Started Guide for Kotlin Multiplatform Mobile (KMM) with Flexible Sync
Rate this tutorial
This is an introductory article on how to build your first Kotlin Multiplatform Mobile using Atlas Device Sync.
Mobile development has evolved a lot in recent years and in this tutorial, we are going discuss Kotlin Multiplatform Mobile (KMM), one such platform which disrupted the development communities by its approach and thoughts on how to build mobile apps.
Traditional mobile apps, either built with a native or hybrid approach, have their tradeoffs from development time to performance. But with the Kotlin Multiplatform approach, we can have the best of both worlds.
Kotlin Multiplatform is all about code sharing within apps for different environments (iOS, Android). Some common use cases for shared code are getting data from the network, saving it into the device, filtering or manipulating data, etc. This is different from other cross-development frameworks as this enforces developers to share only business logic code rather than complete code which often makes things complicated, especially when it comes to building different complex custom UI for each platform.
If you are an Android developer, then you don't need to do much. The primary development of KMM apps is done using Android Studio. The only additional step for you is to install the KMM plugin via IDE plugin manager. One of the key benefits of this is it allows to you build and run the iOS app as well from Android Studio.
To enable iOS building and running via Android Studio, your system should have Xcode installed, which is development IDE for iOS development.
To verify all dependencies are installed correctly, we can use
kdoctor, which can be installed using brew.
With our setup complete, it's time to get our hands dirty and build our first Hello World application.
Creating a KMM application is very easy. Open Android Studio and then select Kotlin Multiplatform App from the New Project template. Hit Next.
On the next screen, add the basic application details like the name of the application, location of the project, etc.
Finally, select the dependency manager for the iOS app, which is recommended for
Regular framework, and then hit finish.
Once gradle sync is complete, we can run both iOS and Android app using the run button from the toolbar.
That will start the Android emulator or iOS simulator, where our app will run.
Now it's time to understand what's happening under the hood to grasp the basic concepts of KMM.
Any KMM project can be split into three logic folders — i.e.,
shared— and each of these folders has a specific purpose.
Since KMM is all about sharing business-/logic-related code, all the shared code is written under
sharedthe folder. This code is then exposed as libs to
iosAppfolders, allowing us to use shared logic by calling classes or functions and building a user interface on top of it.
There can be a few use cases where you like to use platform-specific APIs for writing business logic like in the
Hello World!app where we wanted to know the platform type and version. To handle such use cases, KMM has introduced the concept of
expect, which can be thought of as KMM's way of
In this concept, we define
expectfor the functionality to be exposed, and then we write its implementation
actualfor the different environments. Something like this:
In the above example, you'll notice that we are using platform-specific APIs like
sharedfolder. To keep this organised and readable, KMM has divided the
sharedfolder into three subfolders:
With this, we covered the basics of KMM (and that small learning curve for KMM is especially for people coming from an
androidbackground) needed before building a complex and full-fledged real app.
The tech stack for our app will be:
We will be following a top to bottom approach in building the app, so let's start building the UI using Jetpack compose with
In our viewModel, we have a method
saveQueryto capture the user queries and share them with the speaker. This information is then passed on to our logic layer,
RealmRepo, which is built using Kotlin Multiplatform for Mobile (KMM) as we would like to reuse this for code when building an iOS app.
Now, to save and share this information, we need to integrate it with Atlas Device Sync, which will automatically save and share it with our clients in real time. To connect with Device Sync, we need to add
RealmSDK first to our project, which provides us integration with Device Sync out of the box.
Realm is not just SDK for integration with Atlas Device Sync, but it's a very powerful object-oriented mobile database built using KMM. One of the key advantages of using this is it makes our app work offline without any effort.
This step is broken down further for ease of understanding.
build.gradlefile under project root and add the
Now let's enable the Realm plugin for our project. We should make corresponding changes to the
build.gradlefile under the
With the last step done, we are just one step away from completing the Realm setup. In this step, we add the Realm dependency to our project.
Realmdatabase will be shared across all platforms, we will be adding the Realm dependency to the common source
shared. In the same
build.gradlefile, locate the
sourceSettag and update it to:
With this, we have completed the
Realmsetup for our KMM project. If you would like to use any part of the SDK inside the Android module, you can add the dependency in Android Module
Since Realm is an object-oriented database, we can save objects directly without getting into the hassle of converting them into different formats. To save any object into the
Realmdatabase, it should be derived from
Now let's save our query into the local database, which will then be synced using Atlas Device Sync and saved into our cloud database, Atlas.
The next step is to create a
Realminstance, which we use to save the information. To create a
Realm, an instance of
Configurationis needed which in turn needs a list of classes that can be saved into the database.
Realminstance is sufficient for saving data into the device but in our case, we need to integrate this with Atlas Device Sync to save and share our data into the cloud. To do this, we take four more steps:
- Enable Atlas Device Sync using Flexible Sync. Select the App services tab and enable sync, as shown below.
Now let's connect our Realm and Atlas Device Sync. To do this, we need to modify our
Realminstance creation. Instead of using
RealmConfiguration, we need to use
SyncConfigurationinstance can be created using its builder, which needs a user instance and
initialSubscriptionsas additional information. Since our application doesn't have a user registration form, we can use anonymous sign-in provided by Atlas App Services to identify as user session. So our updated code looks like this:
Now, the last step to complete our application is to write a read function to get all the queries and show it on UI.
Also, you can view or modify the data received via the
saveInfofunction using the
With this done, our application is ready to send and receive data in real time. Yes, in real time. No additional implementation is required.