How can I get Kotlin working Dagger 2 and Intellij for non-android projects?

I haven’t been able to get it working. I’m using the kapt plugin and calling “gradle build” will generate Dagger classes and compile as expected. The problem is that intellij doesn’t recognize the generated classes and can’t build the project. Has anyone gotten this working?

1 Like

You probably found the solution long ago, but for future searchers, just add this to your build:

  sourceSets {
    main {
      java {
        srcDir "${buildDir}/generated/source/kapt2/main"
      }
    }
  }

(Change kapt2 to kapt if you are not using the new experimental kapt2)

2 Likes

Almost 1 year later and it’s still valid… This shouldn’t be done manually…

I’ve made a basic Dagger 2 example in Kotlin that you should be able to import, compile and run in IntelliJ IDEA. Just see the readme, it’s easy.

Thank you, Erik! It’s really useful. We can also add

apply plugin: 'application'
...
mainClassName = 'io.github.erikhuizinga.kotfee.CoffeeApp'

to be able to run the app like this
./gradlew run
Probably in general case we also need to set ‘sourceSets’.