Setting up ObjectBox Database in Mobile Application
ObjectBox positions itself as the fastest embedded database for mobile devices. The company's benchmarks show 10x advantage over Room/SQLite on write operations. In practice, the gap depends on access patterns, but ObjectBox is indeed faster on mass object insertions due to its native C++ engine without SQL parsing.
When ObjectBox Makes Sense
ObjectBox is an object database, not relational. No JOIN, no GROUP BY, no aggregate functions in the traditional SQL sense. If your application works with object graphs (entities with relationships) rather than analytical queries, ObjectBox wins. For IoT applications, trackers, game saves, catalogs with filtering — it's well-suited.
Connection and Models
// build.gradle (app)
plugins {
id("io.objectbox")
}
dependencies {
implementation("io.objectbox:objectbox-kotlin:3.8.0")
}
@Entity
data class Task(
@Id var id: Long = 0,
var title: String = "",
var description: String = "",
var priority: Int = 0,
var dueDate: Long = 0,
var isCompleted: Boolean = false
) {
// ToMany relationships configured via RelationInfo
val tags: ToMany<Tag> = toMany()
}
@Entity
data class Tag(
@Id var id: Long = 0,
var name: String = "",
@Index var color: String = ""
)
@Id is mandatory Long for ObjectBox internal ID. This is not UUID and not your business identifier — use a separate string field for server synchronization.
Initialization and BoxStore
// Application class
class MyApp : Application() {
companion object {
lateinit var boxStore: BoxStore
private set
}
override fun onCreate() {
super.onCreate()
boxStore = MyObjectBox.builder()
.androidContext(this)
.name("tasks-db")
.build()
}
}
// Usage in ViewModel
class TaskViewModel : ViewModel() {
private val taskBox: Box<Task> = MyApp.boxStore.boxFor(Task::class.java)
val tasks: LiveData<List<Task>> = liveData(Dispatchers.IO) {
val query = taskBox.query(Task_.isCompleted.equal(false))
.order(Task_.priority, QueryBuilder.DESCENDING)
.build()
emitSource(query.subscribe().toLiveData())
}
}
query.subscribe().toLiveData() — ObjectBox DataObserver automatically notifies when data in the box changes. This is reactive subscription without extra code.
Queries via QueryBuilder
ObjectBox does not use string SQL. Queries use type-safe QueryBuilder with Properties — generated meta classes (Task_, Tag_):
// Filtering with multiple conditions
val urgentTasks = taskBox.query(
Task_.isCompleted.equal(false)
.and(Task_.priority.greater(2))
.and(Task_.dueDate.less(System.currentTimeMillis() + 86_400_000L))
)
.order(Task_.dueDate)
.build()
.find()
// Full-text search requires @Index(type = IndexType.VALUE) + FTS
val searchResults = taskBox.query(
Task_.title.contains("meeting", StringOrder.CASE_INSENSITIVE)
)
.build()
.find()
No JOIN — relationships via ToOne/ToMany. ObjectBox loads related objects lazily by default:
// ToMany loaded on first access
val taskTags = task.tags // lazy load, database access
ObjectBox Sync
Like Realm, ObjectBox offers a commercial synchronization server — ObjectBox Sync. Bidirectional sync, conflict resolution, delta updates. This is a separate product with licensing.
Typical Problems
ID is not UUID. ObjectBox assigns Long IDs automatically — they are unique only locally. For server synchronization, you need a separate UUID field with @Index.
Codegen on each build. The plugin generates MyObjectBox.java and _ classes. If the schema changed but gradle cache wasn't cleared, the compiler complains about mismatch. ./gradlew clean fixes it.
Open BoxStore — one per application. Attempting to open a second BoxStore on the same file throws an exception. Singleton via Application class is mandatory.
ObjectBox setup, models, reactive queries for Android: 3–5 days. Cost calculated individually.







