Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyObjectBox is not generated in kotlin (objectbox library)

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?

like image 522
Shudipto Trafder Avatar asked Dec 30 '17 17:12

Shudipto Trafder


2 Answers

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.

like image 85
Mojtaba Razaghi Avatar answered Sep 30 '22 16:09

Mojtaba Razaghi


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.

like image 37
Uwe - ObjectBox Avatar answered Sep 30 '22 16:09

Uwe - ObjectBox