Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: Kotlin needs 2 builds after clean to pick up code when using kapt

In our Android app we're using DBFlow to access the SQLite database. We're referencing the classes generated by DBFlow via Kotlin. We are already aware of the fact that we have the model classes as well as the database class in Java as code generation won't work when writing these classes in Kotlin.

However we still have to build the code twice after every project clean. Executing the first build on a device results in ClassNotFoundExceptions for seemingly random Kotlin classes (even if they don't access code generated by DBFlow or any of the model classes defined by us). The same is reported already at compile time when enabling Proguard which of course fails the build. The second build always succeeds.

The funny thing is, that code generation already works at the first run - the classes are there and are also picked up by the IDE. But the compiler somehow can't find them making me think that the code generation happens too late in the build process. On the other hand as explained above there are also classes not found which don't have to do anything with code generation and/or annotation processing.

So is there a better solution to this problem than a second build?

For reference, the relevant parts of our app's build.gradle looks like this - just like the DBFlow documentation is suggesting:

def dbflow_version = "3.0.0-beta4"

dependencies {
    kapt "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"
}

kapt {
   generateStubs = true
}

Edit: I found that I also have to rebuild twice for every change I make to the code. It compiles correctly in the first build if I didn't clean before, but the changes simply aren't picked up.

like image 783
ubuntudroid Avatar asked Mar 04 '16 14:03

ubuntudroid


People also ask

What does unresolved reference mean in Kotlin?

To conclude, the unresolved reference error happens when Kotlin has no idea what the keyword you're typing in the file points to. It may be a function, a variable, or another construct of the language that you've yet to declare in the code.


1 Answers

If you use kotlin and retrolambda this might fix it:

me.tatarka:gradle-retrolambda:3.4.0

github gist

like image 144
Jan Rabe Avatar answered Oct 10 '22 21:10

Jan Rabe