Objectbox-java: Imports not working with Kotlin

Created on 23 May 2017  路  12Comments  路  Source: objectbox/objectbox-java

I started to convert all of my code to Kotlin and I noticed that whenever I try to invoke Objectbox related properties (generated files) it gives me compile time error Unresolved reference : ___. Even though the imports work but the project wont compile. As a workaround I use static java helper class. So is this going to be fixed any time soon?

Most helpful comment

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

All 12 comments

We are still working on Kotlin support (#9). Entities must be Java at the moment. Other classes should be fine to be in Kotlin.

So this should compile right?

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        Fabric.with(this, Crashlytics())
        DataManager.init(this)
        VkWrapper.init(this)
        val boxStore = MyObjectBox.builder().androidContext(applicationContext).build()
    }
}

But in my case it gives me error Error:(6, 33) Unresolved reference: MyObjectBox

Oh, I suspect the Gradle task "objectbox" is not triggered in time for Kotlin. Can you manually run the task before compiling?

I am not sure how to achieve this (

@arnis71 In the Gradle pane in Android Studio simply double-click the objectbox task:

image

Still not compiling...

Can reproduce when converting the App class of our example project. The code is generated, but not picked up by the compileDebugKotlin task. I suppose calling com.android.build.gradle.api.BaseVariant.registerJavaGeneratingTask() is not enough any longer (or maybe currently broken).

Maybe this will work with the upcoming 3.0 Android Gradle Plugin.
Update: nope, 3.0.0-alpha1 also not working. I suppose we actually have to use a different way to add the generated source folder to the compileDebugKotlin inputs.

Update: the tasks seem to correctly depend on each other (see task graph snippet below). So very likely the Kotlin tasks is missing the generated sources from its inputs.

:objectbox-example:compileDebugSources
+--- :objectbox-example:compileDebugJavaWithJavac
|    +--- :objectbox-example:compileDebugKotlin
|    |    +--- :objectbox-example:generateDebugSources
|    |    |    +--- ...
|    |    |    +--- :objectbox-example:objectbox
|    |    |    |    \--- :objectbox-example:objectboxPrepare

Update: The Android Kotlin plugin documentation does not hint at options to include generated source. The only supported option seem to be annotation processors that generate code.

Update: Android Plugin 3.0.0-alpha2 also not working.

-ut

A workaround is to manually include the objectbox generated source folder:

android {
    ...
    sourceSets {
        main.java.srcDirs += 'build/generated/source/objectbox'
    }
}

-ut

It works, thanks!

Won't fix. Full solution comes with #9

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

I guess you have to have at least 1 model class with @Entity annotation before you try to hit the "Make Project". Voila, MakeObject class will be generated. 2 hours wasted trying to figure out why it doesn't copmpile. lol

Was this page helpful?
0 / 5 - 0 ratings