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

**URL:** <https://discuss.kotlinlang.org/t/how-can-i-get-kotlin-working-dagger-2-and-intellij-for-non-android-projects/1572>\
**Category:** Uncategorized\
**Created:** [March 31, 2016, 4:15pm UTC](https://discuss.kotlinlang.org/t/how-can-i-get-kotlin-working-dagger-2-and-intellij-for-non-android-projects/1572 "2016-03-31T16:15:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanPallas](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@DanPallas](https://discuss.kotlinlang.org/u/DanPallas)\
**Post date:** [March 31, 2016, 4:15pm UTC](https://discuss.kotlinlang.org/t/how-can-i-get-kotlin-working-dagger-2-and-intellij-for-non-android-projects/1572/1 "2016-03-31T16:15:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Raman.Gupta](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/raman.gupta/32/1834_2.png) [@Raman.Gupta](https://discuss.kotlinlang.org/u/Raman.Gupta)\
**Post date:** [October 3, 2016, 6:29pm UTC](https://discuss.kotlinlang.org/t/how-can-i-get-kotlin-working-dagger-2-and-intellij-for-non-android-projects/1572/2 "2016-10-03T18:29:57Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![andreiverdes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/andreiverdes/32/3760_2.png) [@andreiverdes](https://discuss.kotlinlang.org/u/andreiverdes)\
**Post date:** [August 29, 2017, 11:59am UTC](https://discuss.kotlinlang.org/t/how-can-i-get-kotlin-working-dagger-2-and-intellij-for-non-android-projects/1572/3 "2017-08-29T11:59:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![erik](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/erik/32/4556_2.png) [@erik](https://discuss.kotlinlang.org/u/erik)\
**Post date:** [December 18, 2017, 10:49am UTC](https://discuss.kotlinlang.org/t/how-can-i-get-kotlin-working-dagger-2-and-intellij-for-non-android-projects/1572/4 "2017-12-18T10:49:27Z")

</div>

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.

> **[kotfee ☕️](https://erikhuizinga.github.io/kotfee/)**
>
> Google’s Dagger 2 coffee maker example in Kotlin

---

<div class="post-metadata">

**Author:** ![ievgen\_o](https://avatars.discourse-cdn.com/v4/letter/i/6bbea6/32.png) [@ievgen\_o](https://discuss.kotlinlang.org/u/ievgen_o)\
**Post date:** [February 5, 2019, 10:04am UTC](https://discuss.kotlinlang.org/t/how-can-i-get-kotlin-working-dagger-2-and-intellij-for-non-android-projects/1572/5 "2019-02-05T10:04:50Z")

</div>

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’.
