Objectbox CRUD operations using Kotlin – Set Up

Objectbox CRUD operations using Kotlin – Set Up

Hi guys, its been a while since i published my last article. Make sure you go through it first, to help you set up first. In this part of the small tutorial series, we will be developing a simple Note App, that will look similar to this example. We might change the User Interface, as we continue working on this tutorial series.

Objectbox setup using Kotlin is relatively easy. This tutorial will give you a rough idea on how to use Objectbox in your Android project. I will try as much as possible to cover almost all important aspects.

User Interface Design

We will build a simple app, with one activity to create new notes, update them, list them and even delete unwanted notes. You can name it a basic Evernote App. With proper understanding, you will be able to create your own complex applications with ease. Our User Interface will look like this

I will provide a step by step guide, and give a link to the final project code on Github. Lets get started.

Objectbox CRUD operations using Kotlin

Now, many might be wondering what the heck is CRUD. This is an acronym in computer programming that stand for :

C – Create

R – Read

U – Update

D – Delete

When dealing with databases, we need to perform database operations, CRUD in our example. We will be able to create a new note, view all added notes, update or delete the existing notes. To make this more easy, I will be covering them each per article.

Objectbox setup using Kotlin

Before using Objectbox, we need to create our data models and initialize objectbox. This article will explain how to do this easily, thanks to the expressive Kotlin syntax. Create a new project, and choose the Empty Activity as the template. I choose API 10 as the Minimum, but you can choose any level above this, but it is good to support as minimum as possible.

Create a new file App.kt, and add the following code: Remember to replace the package name with your own!

This class makes it easy to initialize Objectbox and make it accessible within our application easily. We  need to add it to our manifest file. Add the following  line of code to your Manifest file.

Your manifest file should look like this:

You will get an error, with MyObjectBox, but do not worry. We need to at least create one model, or rather a data class to allow objectbox automatically generate this and make it available for import. We will create a new package, and name it models. This is how you create a package in Android Studio: (This screenshot has Java classes not Kotlin, but explains how to create a new package)

A package is used to group say Java classes or Kotlin classes in this case neatly. This ensures our code is properly formatted and not scattered all over. We will group our data classes in one package and name it models. This will result in neatly arranged code. This is not mandatory but is as good android programming practice.

Kotlin Data Classes

Kotlin data class is a class that only contains state and does not perform any operation, as covered in our previous tutorial. We will use them to create our data models.

You can still use the normal Java Classes, but I will not be covering that in this tutorial. You can read how to use them later on. It will not result in any error as Java and Kotlin can be used together, but will result in more code.

We create a new Kotlin Class, and name it Note.kt, and add the following code:

The @Entity annotation identifies the Kotlin class Note as a persistable entity, or simply tell Objecbox that we want to save this class. This will trigger ObjectBox to generate persistence code tailored for this class.

ObjectBox is a database that persists objects. For a clear distinction, we sometimes call those persistable objects entities. To let ObjectBox know which classes are entities you add annotations to them. Then ObjectBox can do its magic with your entities.

In ObjectBox, every object has an ID of type long to efficiently get or reference objects. Use the @Id annotation on a  long property in your entity. You can then add other variables to your data class, depending on what you want to persist or rather save. We are adding variables note, comment and date. You can add more if you need them.

Rebuild your project, and you should not get any errors. We have now managed to set up Objectbox using Kotlin in our project. When using data classes, we use less code as opposed to Java classes which generate a lot of boiler plate codes, resulting to more readable code.


Setting up Objectbox using Kotlin saves a lot of boilerplate code that Java forces us to generate and time, so you end up with a code that is easier to understand and to maintain.

If you liked this article, you can continue learning Kotlin by subscribing to my emailing list to get instant notifications.

I’d love to see if / where you’re using Kotlin, so please drop me a comment or leave a response if you have anything to share (or even any questions to ask!)

Happy Coding!

More From Android Study:

Leave a Reply

Your email address will not be published. Required fields are marked *