How to migrate from Realm Java SDK to Realm Kotlin SDK
Rate this tutorial
This article is targeted to existing Realm developers who want to understand how to migrate to Realm Kotlin SDK.
Android has changed a lot in recent years notably after the Kotlin language became a first-class citizen, so does the Realm SDK. Realm has recently moved its much-awaited Kotlin SDK to beta enabling developers to use Realm more fluently with Kotlin and opening doors to the world of Kotlin Multiplatform.
Let's understand the changes required when you migrate from Java to Kotlin SDK starting from setup till its usage.
The new Realm Kotlin SDK is based on Kotlin Multiplatform architecture which enables you to have one common module for all your data needs for all platforms. But this doesn't mean to use the new SDK you would have to convert your existing Android app to KMM app right away, you can do that later.
Let's understand the changes needed in the gradle file to use Realm Kotlin SDK, by comparing the previous implementation with the new one.
In project level
Earlier with Java SDK
With Kotlin SDK, we can delete the Realm plugin from
In the module-level
With Java SDK, we
- Enabled Realm Plugin
- Enabled Sync, if applicable
With Kotlin SDK,
id("io.realm.kotlin") version "0.10.0"
- Remove the Realm block under android tag
- Add Realm dependency under
If you are using only Realm local SDK, then you can add
With these changes, our Android app is ready to use Kotlin SDK.
Traditionally before using Realm for querying information in our project, we had to initialize and set up few basic properties like name, version with sync config for database, let's update them as well.
Steps with JAVA SDK :
- Setup Realm DB properties like name, version, migration rules etc using
- Setup logging
- Configure Realm Sync
With Kotlin SDK :
Realm.init()Is not needed anymore.
- Setup Realm DB properties like db name, version, migration rules etc. using
RealmConfiguration- This remains the same apart from a few minor changes.
- Setup logging - This is moved to
- Configure Realm Sync - No changes
No changes are required in model classes, except you might have to remove a few currently unsupported annotations like
@RealmClasswhich is used for the embedded object.
Note: You can remove
classwhich was mandatory for using Java SDK in Kotlin.
The most exciting part starts from here 😎(IMO).
Traditionally Realm SDK has been on the top of the latest programming trends like Reactive programming (Rx), LiveData and many more but with the technological shift in Android programming language from Java to Kotlin, developers were not able to fully utilize the power of the language with Realm as underlying SDK was still in Java, few of the notable were support for the Coroutines, Kotlin Flow, etc.
But with the Kotlin SDK that all has changed and further led to the reduction of boiler code. Let's understand these by examples.
Example 1: As a user, I would like to register my visit as soon as I open the app or screen.
Steps to complete this operation would be
- Authenticate with Realm SDK.
- Based on the user information, create a sync config with the partition key.
- Open Realm instance.
- Start a Realm Transaction.
- Query for current user visit count and based on that add/update count.
With JAVA SDK:
With Kotlin SDK:
Upon quick comparing, you would notice that lines of code have decreased by 30%, and we are using coroutines for doing the async call, which is the natural way of doing asynchronous programming in Kotlin. Let's check this with one more example.
Example 2: As user, I should be notified immediately about any change in user visit info. This is more like observing the change to visit count.
With Java SDK:
With Kotlin SDK :
Again upon quick comparing you would notice that lines of code have decreased drastically, by more than 60%, and apart coroutines for doing async call, we are using Kotlin Flow to observe the value changes.
Apart from Realm Kotlin SDK being written in Kotlin language, it is fundamentally little different from the JAVA SDK in a few ways:
- Frozen by default: All objects are now frozen. Unlike live objects, frozen objects do not automatically update after the database writes. You can still access live objects within a write transaction, but passing a live object out of a write transaction freezes the object.
- Thread-safety: All realm instances, objects, query results, and collections can now be transferred across threads.
- Singleton: You now only need one instance of each realm.
There is no straight answer to question, it really depends on usage, complexity of the app and time. But I think so this the perfect time to evaluate the efforts and changes required to migrate as Realm Kotlin SDK would be the future.