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?

Shudipto Trafder
  • 1,932
  • 1
  • 14
  • 27

4 Answers4

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.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
Mojtaba Razaghi
  • 319
  • 4
  • 10
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.

Uwe - ObjectBox
  • 1,012
  • 1
  • 7
  • 11
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.

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
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

Zar
  • 595
  • 4
  • 17