6

I want to use Dagger in IntelliJ but I can't use it. Dagger uses an annotation processor and I guess IntelliJ doesn't know about the annotation processor.

You can see the generated java file, it's generated by the Dagger2 compiler, but my java source can't find them. Even if I set build.gradle to connect between my java file and the generated java file.

This is my whole source project file.

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

def generatedSources = "$buildDir/generated/src"
def generatedOutputDir = file("$generatedSources")

compileJava {
    doFirst {
        generatedOutputDir.exists() || generatedOutputDir.mkdirs()
        options.compilerArgs = [
                '-s', "${generatedSources}"
        ]
    }
}

sourceSets {
    main {
        java {
            srcDirs += generatedOutputDir
        }
    }
}

dependencies {
    compile "com.google.dagger:dagger:2.0"
    compile "com.google.dagger:dagger-compiler:2.0"
    compile "com.squareup:otto:1.3.8"
    compile 'io.reactivex:rxjava:1.0.13'
    compile "org.glassfish:javax.annotation:10.0-b28"

    testCompile "junit:junit:4.12"
    testCompile "org.mockito:mockito-core:1.9.5"
}

compileJava.dependsOn clean
Aritz
  • 30,971
  • 16
  • 136
  • 217
Kh Jung
  • 313
  • 1
  • 9
  • May I suggest you give a try at my [`net.ltgt.apt`](https://plugins.gradle.org/plugin/net.ltgt.apt) for Gradle? `apply plugin: 'net.ltgt.apt`, `apply plugin: 'idea``, move the `dagger-compiler` dependency from `compile` to `compileOnly`, and remove all your `compileJava` and `sourceSets` customizations. The plugin should take care of everything. – Thomas Broyer Aug 19 '15 at 13:15
  • You've got the dagger-compiler as a `compile` dependency. It should be `apt` instead: `apt 'com.google.dagger:dagger-compiler:2.0'`. This way you tell gradle that it's an annotation processor tool: http://docs.oracle.com/javase/7/docs/technotes/guides/apt/ – Aritz Sep 01 '15 at 21:01
  • Thanks @ThomasBroyer. It can be compiled. But intellij can't find dependencies and display the compiled code. But the gradle compile and execute it. – Kh Jung Sep 01 '15 at 22:12
  • @XtremeBiker I can't understand the oracle's apt. How can I use it in my build.gradle? – Kh Jung Sep 01 '15 at 22:14
  • Replace the `compile` by `apt`. – Aritz Sep 02 '15 at 05:53
  • @ClarkJung Did you also `apply plugin: 'idea'`? (BTW, you should put `dagger-compiler` dependency to `apt` configuration, not `compileOnly`; my bad). – Thomas Broyer Sep 02 '15 at 08:25
  • 1
    @XtremeBiker You're right, but `apt` is not "native" to Gradle, it has to be added by a plugin (or by yourself, which is really not that hard, but requires a bit of configuration of the `JavaCompile` tasks too, and then of the IDE; which is exactly why I wrote the `net.ltgt.apt` plugin: to stop repeating myself from project to project) – Thomas Broyer Sep 02 '15 at 08:27
  • @XtremeBiker this is my whole build.gradle file. https://gist.github.com/moltak/7976aa2df943cf52c792 – Kh Jung Sep 02 '15 at 19:28

1 Answers1

5

I faced the same problem. I found this gradle plugin which does the job:

https://github.com/tbroyer/gradle-apt-plugin

In fact it has an example on how to use it with dagger2. Here's what I did:

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

repositories {
    mavenCentral()
    maven { url "https://plugins.gradle.org/m2/" }
    jcenter()
}

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

dependencies {
 compile "com.google.dagger:dagger:2.4"
 apt 'com.google.dagger:dagger-compiler:2.4'

}

All props should go to the original author of the plugin.

Sun
  • 2,658
  • 6
  • 28
  • 33