Unresolved reference: Kotlin needs 2 builds after

2019-03-18 05:20发布

问题:

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.

回答1:

If you use kotlin and retrolambda this might fix it:

me.tatarka:gradle-retrolambda:3.4.0

github gist