Objectbox using Kotlin – UPDATED!

Objectbox using Kotlin – UPDATED!

Hello guys, today we are going to learn about something different. The database. When developing applications, we might be faced with various challenges like building applications with offline capabilities, or even building hybrid apps that can run either online or offline, or even being able to save our users data and make it available to them without active internet connection like many applications.

Introduction

Android provides several ways to store user and app data. SQLITE is one way of storing user data. Android comes in with built in SQLite database implementation. However, there is a major drawback with it, it is not easy to use, especially for beginners.

In this article, we will be learning how to use Objectbox. ObjectBox is a new mobile object database optimized for performance. ObjectBox brings technology from NoSQL server databases to mobile. Objectbox was created with performance in mind. ObjectBox is a fully transactional database satisfying ACID properties. This article has been updated to show you how to set up version 1 of Objectbox, an updated from the previous beta version.

ObjectBox top 5 features

Superfast: The motivation to build ObjectBox was to deliver the best possible performance. It outperforms all embedded databases we ever tested (which are quite a few), typically by a factor 5 to 15. Details will follow.

Object API: No more rows, columns and SQL – ObjectBox is a database built for objects from ground up (no ORM, no SQLite). The concise API is easy to learn and just takes a fraction of the code you would need to work with SQLite.

Instant unit testing: With multi-platform approach, you can run plain unit tests on the desktop (no Robolectric, no instrumentation tests) with a real database in an instant.

Simple threading: Objects returned by ObjectBox work in all threads with no strings attached.

No manual schema migrations: ObjectBox takes care of new object versions with added, removed, and renamed entities and properties.

Objectbox using Kotlin

In order to use ObjectBox in your Android project, you need to create a new project and then add its Gradle plugin and Android library. But remember, we are using Kotlin not Java. So if you created an empty project, with Android Studio less than version 3.0, you will need to convert your existing Java code to Kotlin. This is relatively simple, as shown in this tutorial  I wrote a while back.

Press shift twice in Android Studio Editor. You should be able to see finder, then you should type convert Kotlin

Then select Configure Kotlin in Project

Click the OK Button and Sync the project.

In order to convert this Java Template to Kotlin, Press CTRL + ALT + SHIFT + K (or) click Code -> Convert Java File to Kotlin. Doing this will convert Java files to Kotlin.

Now lets add our Objectbox to our new project.

build.gradle(Project)

Add the following code to your gradle file:

build.gradle(module:app)

Add the following code to your gradle file:

If you are doing Android instrumentation (especially with Espresso), you may get a warning like this:
Error: Conflict with dependency ‘com.google.code.findbugs:jsr305’ in project ‘:app’. Resolved versions for app (3.0.2) and test app (2.0.1) differ.
You can easily resolve the version conflict adding this Gradle dependency:

It is added in the above example, but it was worth mentioning it as you have to write it to avoid getting any conflicts.

Now hit sync now and wait for gradle build to finish. We should not get any errors, and the build process should be a success.

Conclusion

We have managed to add Objectbox to our application. This article was meant to show you how to add it to your project first, and the contents will keep changing as more changes are made by the developers. I came to find it the best alternative to use when developing applications due to the ease of use and the powerful features it gives you as a developer.

In the next article, I will guide you through CRUD operations using Objectbox and you will be dying to use it in your next project. If you encounter any errors while trying to set it up, feel free to ask it as a question below and I will gladly help you.’

This article explains how to set up the Version 1 of Objectbox, I will keep on updating with newer releases of Objectbox in the future.

Happy Coding!

 

 

 

 

More From Android Study:

Leave a Reply

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