ObjectBox and Kotlin

ObjectBox comes with full Kotlin support for Android since version 0.9.13. This allows entities to be modeled in Kotlin classes (regular and data classes). With Kotlin support you can build faster apps even faster.

Gradle Build Set Up

It’s important to note that the Gradle build configuration differs from the “default” Java configuration. While Java uses a fully custom Gradle plugin, Kotlin applies a standard annotation processor and a small Gradle plugin.

Assuming you already set up your Kotlin Android project (check here and here), you need to add a few lines to your Gradle build files for ObjectBox. The Gradle “buildscript” configuration is typically done in your top level build.gradle  file (alternatively, you should be able to make those configurations in your app level Gradle file). Here, you define a variable for the ObjectBox version to use, and the dependency to the plugin:

Next, in your app level build.gradle  file, you need to apply the ObjectBox gradle plugin and define a couple of dependencies. Here, “app level” refers to the project, in which your entities are in.

Note, that the “io.objectbox:objectbox-kotlin” dependency is just a tiny library with a few Kotlin extension functions at the moment. We intend to grow it in the future (let us know what you want in it).

Once you got the build working it is time to look at entities.

Kotlin Entities

In Kotlin, you should define ID properties like this:

The ID must be a var (not a val) because ObjectBox assigns an ID after putting a new entity. Also, IDs should be of primitive type Long with the special value of zero for marking entities as new.

Example for a data class:

Constructors

ObjectBox needs to create objects and thus needs to call constructors. It will choose one of these two:

  • All-args constructor: If ObjectBox finds a constructor that matches all persisted properties (types and names are checked), it will call it preferably.
  • No-args constructor: fall back if it does not find an all-args constructor. ObjectBox will initialize fields after calling the no-args (default) constructor.

Note that Kotlin data classes usually provide a compatible all-args constructor. However, if you include transient properties or relations, ObjectBox will not use it. In that case you should provide a constructor yourself.

Defining Relations in Kotlin Entities

In general, you should already be familiar to ObjectBox relations, especially the ToOne and ToMany classes.

Let’s look at an example. Entities “Customer” and “Order” relate to each other. One Order is made by one Customer (to-one relation), and one customer may have placed several Orders (to-many relation).

Order.kt

Costumer.kt

Now, there is some magic happening here. Although the relation properties have a lateinit  modifier, you won’t initialize them yourself (you really should not). Instead, the ObjectBox Gradle plugin will transform your entity class to do the proper initialization in constructors. Thus, after object creation, you can just assume ToOne and ToMany (List) properties are ready for you to use.

Optional Configuration

In the Gradle build file (app level, where your entities are), you can add some optional configurations. For example, you can switch on debug mode for ObjectBox’s annotation processor (also run Gradle with the –info option to actually see the output):

Other kapt arguments are:

  • objectbox.modelPath: override the default location for the JSON model file (“objectbox-models/default.json”).
  • objectbox.daoCompat: set to true if you want to use DAO compat (greenDAO-like API)
Spread the love
Sign up for fresh ObjectBOX news here. No spam, just fresh developer news once in a while.
x