question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Objectbox entities in shared library module

See original GitHub issue

Issue Basics

  • ObjectBox version: 1.2.0-RC
  • Reproducibility: always

Reproducing the bug

Description

I have a project set up with the following modules: -mobile (application) -shared (library) -wear (application)

I have a single entity taken directly from the kotlin example “Note.kt” Originally I placed this in the shared module so that I could use the same entities in the mobile and wear application, but after building the project it complains -> “Unresolved reference: MyObjectBox”

When I moved Note.kt into the mobile app and build, it recognizes MyObjectBox fine.

Code

build.gradle (mobile)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'
apply plugin: 'kotlin-android-extensions

android {
    compileSdkVersion 27
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.grant.gradechaser"
        minSdkVersion 23
        targetSdkVersion 27
        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'
    })
    wearApp project(':wear')
    compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
    compile "com.android.support:support-v4:$rootProject.ext.supportVersion"

    compile "com.android.support:design:$rootProject.ext.supportVersion"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

}

repositories {
    mavenCentral()
}

build.gradle (shared)

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.objectbox'


// Until the ObjectBox Gradle plugin has better support for Kotlin, we must help it a bit:
//task('compileKotlin').dependsOn objectbox
android.sourceSets.main.java.srcDirs += 'build/generated/source/objectbox'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    compile "io.objectbox:objectbox-android:$objectboxVersion"
    compile "io.objectbox:objectbox-kotlin:$objectboxVersion" // some useful Kotlin extension functions
    kapt "io.objectbox:objectbox-processor:$objectboxVersion" // When using Kotlin use kapt instead:
}
repositories {
    mavenCentral()
}

The wear build.gradle looks pretty similar to the mobile one, so I didn’t include it.
I’ve tried putting the lines that add the generated code to the sourceSet in the mobile module too but that didn’t fix the problem.

Is this module setup not supported? I saw that you mentioned here

As a rule of thumb, they should reside in a single module. You could have multiple modules with entities, but only if they are totally separated (also different DB file).

But if they are all in the shared module, should that still work?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gmkadocommented, Nov 10, 2017

Ha, I’m an idiot. Forgot to add the dependency on the shared module. Added compile project(':shared') to my app module and it imports fine.

0reactions
aarajputcommented, Jan 21, 2018

Add this line. It will solve your problem. apply plugin: ‘kotlin-kapt’

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting started - ObjectBox Docs
ObjectBox is a super fast mobile database that persists objects. It lets you avoid many repetitive tasks and offers a simple interface to...
Read more >
FAQ - ObjectBox Docs
The ObjectBox plugin only looks for entities in the current module, it does not search library modules. However, you can have a separate...
Read more >
Advanced Setup - ObjectBox Docs
Manually Add Libraries ... The ObjectBox Gradle plugin adds required libraries and the annotation processor to your projects dependencies automatically, but you ...
Read more >
ObjectBox Java
This is the ObjectBox documentation for our Java API. We strive to provide you with the easiest and fastest solution to store and...
Read more >
ObjectBox Admin
Apply the plugin after the dependencies block so it picks up ... because the plugin fails to detect and adds the objectbox-android library....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found