Unresolved reference: DaoSession using greendao and kotlin

4.1k Views Asked by At

I am trying to convert a android project written in java to kotlin. My greendao class have been generated by anytime i build i get a Unresolved reference: DaoSession error message. I have
kapt { generateStubs = true } in my build gradle code.

3

There are 3 best solutions below

0
On BEST ANSWER

This is caused by that Greendao generate DaoSession and other Dao files at a default path:"app/build/generated/source/greendao/", which cannot be found by kotlin.

So you just need to change the Dao path by adding this code to your module Gradle file:

greendao {
    targetGenDir 'src/main/java' 
}

Then, you can find the Dao files like DaoSesson.java are generated in your project path 'src/main/java'. Now Kotlin can find the DaoSession.

Hope can help.

Reference: https://github.com/greenrobot/greenDAO/issues/352

0
On

Move your greendao plugin before the kotlin plugin in your app build.gradle like below:

apply plugin: 'org.greenrobot.greendao'

apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
0
On

Note: with your entity classes written in Java, to use the generated greenDAO files in your Kotlin classes you currently may have to manually add the generated source folder for Kotlin compilation to succeed:

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

src: https://github.com/greenrobot/greenDAO/issues/395

Also, see 0xAliHn answer: move

apply plugin: 'org.greenrobot.greendao'

above kotlin plugins