Realm Java and Realm Kotlin SDK: Local File Interoperability
0 reputation · 11 Jun 2021, 03:22 UTC
0 reputation · 11 Jun 2021, 03:22 UTC
When transitioning an Android application from the Realm Java SDK (io.realm) to the Realm Kotlin Multiplatform SDK (io.realm.kotlin), there is a critical boundary regarding the underlying storage engine. While both SDKs target the same general data model concepts, they bundle different versions of the Realm Core engine.
Current documentation indicates that the on-disk file formats are not interchangeable. This creates a challenge for applications with existing local databases that must migrate to the Kotlin SDK without losing user data. Because the SDKs handle schema locking and file headers differently, attempting to open a Realm Java file using the Kotlin SDK typically results in a format incompatibility.
Furthermore, running both SDKs within a single process to facilitate a side-by-side migration is unsupported and may lead to database corruption.
34775 reputation · 11 Jun 2021, 07:04 UTC
No. The Realm Kotlin SDK cannot safely open a Realm file written by the Realm Java SDK, even in read-only mode. There is no supported automated conversion tool and no documented read-only access path from Kotlin to a Java file.
Confirmed: the on-disk file formats and headers are not interchangeable between the two SDKs. Attempting to open a Java-written file with the Kotlin SDK raises a format incompatibility error. Running both SDKs in the same process to do a side-by-side migration is unsupported and can corrupt the database.
Likely explanation: the Java SDK and the Kotlin Multiplatform SDK bundle different Realm Core versions and handle schema locking and file headers differently. That difference makes the files mutually inaccessible.
Verification: run the export/import on a production-like dataset, compare record counts and sample values, and check logs for exceptions or corruption.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 11 Jun 2021, 06:52 UTC
A useful constraint to add to the export/import plan is that the Java-to-Kotlin move should not be attempted as a single in-app upgrade containing both SDKs. On Android, the safer sequence is to ship an export-only build that still depends on the Java SDK, writes a portable snapshot to app-private storage, and records schema/version metadata plus object counts. A later build removes the Java dependency and imports that snapshot into a new Kotlin Realm file.
Because Core file-format compatibility can change, confirm the exact io.realm and io.realm.kotlin versions in your build against the current Realm Kotlin changelog before finalizing the cutover.