android-jetpack-composekotlin-multiplatformobjectboxcompose-multiplatform

How to use ObjectBox database into compose multiplatform project?


The object-box database offers a very easy way of implementation for both android and jvm projects. It has clear-text documentation for these purposes. But problem arises when I try to use it into a multiplatform project. There are no problems with the android part. But problem arises when I try to apply the io.objectbox plugin into the desktop counterpart.

Since, I have to use the kotlin("mutliplatform") plugin, it fails to sync saying:

Caused by: org.gradle.api.plugins.InvalidPluginException: 'io.objectbox' can only be applied to a project if one of the following is applied before:
    * an Android plugin
    * the Kotlin Android or JVM plugin
    * the Java Library, Java Application or Java plugin

Neither of these options seems possible to use. How can I use then?


Solution

  • Library have to be built for KMM, and so it should be written in Kotlin - at least mostly, as you can use Java in androidMain/jvmMain.

    objectbox-java is Java library, so it can't be simply used from KMM. There's a feature request, but I wouldn't expect it to be implemented any soon, as it means rewriting whole library from Java to Kotlin.

    Consider switching to databases that support KMM - the list can be found here.

    Also, you can do one of the two things you usually do in the absence of the KMM library:

    1. Create expect classes to be used from common code and define actual classes for androidMain that will use the Java ObjectBox library / for iosMain will use the iOS ObjectBox library.
    2. Create an interface or abstract class in common code and define them on the platform side. This may be preferable in cases where the iOS library is written in Swift and is not adopted for use from ObjC, so Kotlin interop is also much limited.