MyObjectBox is not generated in kotlin (objectbox library)
Asked Answered
G

4

6

I am trying to use Object Library.

I read the official documentation and follow the instructions. But still, it not working.

The problem is when I try to initialize boxStore object I don't find MyObjectBox class.

val boxStore = MyObjectBox.builder().androidContext(this).build()

Here is my app module. build.gradle (app module)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

apply plugin: 'io.objectbox'

android {
    compileSdkVersion 26
    defaultConfig {
        ....
    }
    buildTypes {
        release {
           ....
        }    
    }

    sourceSets {
        main.java.srcDirs += 'src/main/java'
    }
}

kapt {
    generateStubs = true

    arguments {
        arg("objectbox.debug", true)
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    ... other dependencies

    //object box
    implementation "io.objectbox:objectbox-android:$objectboxVersion"
    // some useful Kotlin extension functions
    implementation "io.objectbox:objectbox-kotlin:$objectboxVersion"
    kapt "io.objectbox:objectbox-processor:$objectboxVersion"

}

And Here is my project module: build.gradle (project)

buildscript {
ext.kotlin_version = '1.2.10'
//all version
ext.support_version = '26.1.0'
ext.objectboxVersion = '1.3.3'

repositories {
    google()
    jcenter()
    maven { url "http://objectbox.net/beta-repo/" }
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files

    //object box
    classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.google.com' }
        maven { url "http://objectbox.net/beta-repo/" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I am searching for the possible solution in several projects. I also follow the official demo app. But still, it not working for me?

Can anyone help me to fix this?

Guyton answered 30/12, 2017 at 17:59 Comment(0)
F
7

I think I finally found the solution: the important part is that you have to write the objectbox-gradle-plugin, above the kotlin-gradle-plugin in your project-level gradle file, the code looks like this :

classpath 'com.android.tools.build:gradle:3.2.1'
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

After that, add the following lines under the other apply plugin:... in app-level gradle

apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'

Most Importantly, you have to make at least one @Entity class and rebuild the project, and MyObjectBox will appear.

Feudality answered 10/1, 2019 at 8:1 Comment(2)
The key is the apply plugin: 'kotlin-kapt' call as it generates the required assets.Larcener
Reordering plugin declarations in gradle makes no sense.Lift
V
1

Your setup looks good. Note that you do not need to add the objectbox-android, objectbox-kotlin or objectbox-processor dependencies. The plugin will do it for you.

Do you have at least one @Entity class defined? For example:

@Entity
public class User { 
    @Id
    private long id;

    private String name;
}

Then Build > Make project. The annotation processor should pick up the entity and generate your MyObjectBox class.

Vltava answered 8/1, 2018 at 9:41 Comment(2)
Great! Just add @Entity at your model. It will automatically generate MyObjectBoxMaharani
That works, but is such an obtuse implementation/logic for a library to force upon developers.....Lift
A
0

I meet with the same question. My config is same with you.

Finally I found the reason.

@Entity
data class Person @JvmOverloads constructor(@Id var id:Long,
                                            var name:String,
                                            var age:Int,
                                            var high:Int)

Like this. You must define one bean first. Thus MyObjectBox will generate.

Autophyte answered 19/3, 2019 at 8:22 Comment(0)
A
0

Had also issues here. Had 3 entities, but the MyObjectBox class was not generated. I had to use the advanced kapt setting for Kotlin, which solved the problem:

kapt {
    arguments {
        arg("objectbox.modelPath", "$projectDir/schemas/objectbox.json")
    }
}

Source: https://docs.objectbox.io/advanced/advanced-setup

Axel answered 11/1, 2022 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.