ObjectBox doesn't generate getters and constructors within entities

1.5k views Asked by At

I'm trying to get started with OB by using this page http://objectbox.io/documentation/introduction/

I create a new project in Android Studio 2.3.3

My Gradle files:

ROOT:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven { url "http://objectbox.net/beta-repo/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath "io.objectbox:objectbox-gradle-plugin:1.0.1"
    }
}

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

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

APP:

apply plugin: 'com.android.application'
apply plugin: 'io.objectbox'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.obox"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'


    compile "io.objectbox:objectbox-android:1.0.1"
    annotationProcessor "io.objectbox:objectbox-processor:1.0.1"
}

It doesn't work with and without 2 lower lines in the APP-gradle file.

My entity class:

package com.example.obox;

import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;

@Entity
public class MyModel {
    @Id
    private long id;
    private String content;
}

No generated code added. So I get errors while trying to make the project.

.../MyModelCursor.java
Error:(45, 32) error: cannot find symbol method getContent()
Error:(48, 57) error: cannot find symbol method getId()
Error:(56, 15) error: cannot find symbol method setId(long)

.../MyModel_.java
Error:(91, 26) error: cannot find symbol method getId()
3

There are 3 answers

2
Markus Junginger On BEST ANSWER

ObjectBox does not generate code into your source files (unlike greenDAO). So you have two options:

  1. Make the fields package private by removing private
  2. Provide standard getters (your IDE can generate them easily)
0
Mr. B. On

I had a similar error message, probably caused by starting a Date field with is, which might be a keyword or something and allowed only by Booleans (not sure about that, just my guess).

After changing isProblemSolvedUpdatedAt: Date? = Date() to problemSolvedUpdatedAt: Date? = Date, the error was gone.

Maybe it helps someone.

Edit: I'm using ObjectBox with Kotlin.

0
mumayank On

If using Kotlin, then simply declare your data/entity class members as var instead of val.

Example:

@Entity
data class MyObject(var string: String, @Id var id: Long = 0)

Use of kotlin makes this super easy as it generates getters/ setters by default.