12

I totally exhausted with Dagger 2 in non-Android app (IDE is IntelliJ IDEA).

build.gradle

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.9"
    }
}

apply plugin: 'java'
apply plugin: 'net.ltgt.apt'

dependencies {
    compile 'com.google.dagger:dagger:2.8'
    apt 'com.google.dagger:dagger-compiler:2.8'
    compileOnly 'javax.annotation:jsr250-api:1.0'
}

Component

@Component(modules = {MyModule.class}) public interface MyComponent {}

Module

@Module class MyModule {}

Main

DaggerMyComponent component = DaggerMyComponent.builder().build();

Compiler says

cannot find symbol DaggerMyComponent

Absolutely have no more ideas what could be wrong :(

By the way, if I run gradlew clean build --refresh-dependencies, there is no error, everything fine!

But as only I use 'Rebuild project' in IDE - error again. Possibly IDEA bug (time to go back to Eclipse)?

And yes, I have added directory with generated classes as a content root, so IDE can see DaggerMyComponent.

Alexey
  • 2,980
  • 4
  • 30
  • 53

4 Answers4

11

I had post the issue to JetBrains

https://youtrack.jetbrains.com/issue/IDEA-169387

and the solution is in first comment.

If you use IntelliJ internal compiler for the project build you need to enable annotation processing at File | Settings | Build, Execution, Deployment | Compiler | Annotation Processors

As an alternative you can delegate build/run IDE actions to gradle. You can find the option at File | Settings | Build, Execution, Deployment | Build Tools | Gradle | Runner

In build 171.4073.35 Delegate IDE build/run actions to gradle option is set by default.

Alexey
  • 2,980
  • 4
  • 30
  • 53
  • Aha, I had already enabled annotation processing, as answers to other flavors of this question suggests it (for instance [here](http://stackoverflow.com/questions/33444356/is-there-any-way-of-making-intellij-idea-recognizing-dagger-2-generated-classes)), but the tip about the option about delegating build/run was new to me. Thanks for the effort to find out this :) – mous Apr 30 '17 at 20:31
  • You saved my day! Thanks a lot! – matteoh May 19 '18 at 21:03
2

Alexey answer is good, but intelliJ doesnt only have problems switching to annotation processors without cleaning, you need also validate if annotation processors are enabled in

File -> Other Settings -> Default Settings -> Compiler -> Annotation Processors

If this is enabled you may also change your compiler.xml in

[project root]/.idea/compiler.xml and enable annotationPorcessing to true for your profile. (Check Image below copied from another stackoverflow post)

  <component name="CompilerConfiguration">
    <annotationProcessing>
      <profile default="true" name="Default" enabled="true" />
    </annotationProcessing>
  </component>

Make sure to [File -> invalidate Cache and Restart] after

Others reported that they had to use

apply plugin: 'idea'

in their gradle file to let intellij recognize dagger generated classes.

enter image description here

Emanuel
  • 8,027
  • 2
  • 37
  • 56
0

In addition to enabling annotation processing as shown by @spierce7, my problem was that I was running Build Project from IntelliJ's Build menu without having set the Gradle -> Runner settings to Delegate IDE build/run actions to gradle. I realized this after running ./gradlew build from the terminal and seeing that the build/generated folder was created.

Another source of confusion for me was that the list of directories would not update in IntelliJ until I clicked into that section of the ide.

I removed apply plugin: 'idea' and it still works so that is unnecessary.

I'm not sure if File -> invalidate Cache and Restart was necessary but I did try that as @Emanuel Seibold pointed out before I noticed it started working.

Abtin Gramian
  • 1,630
  • 14
  • 13
-1

I'm version 2017.3.3 of IntelliJ IDEA, version 0.14 of the net.ltgt.apt plugin and version 2.14.1 of Dagger and what worked for me was adding the following to my build.gradle file to tell IntelliJ where it can find the sources generated by Dagger:

apply plugin: 'idea'
idea {
    module {
        sourceDirs += file("$buildDir/generated/source/apt/main")
        testSourceDirs += file("$buildDir/generated/source/apt/test")
    }
}
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147