Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Dagger2 in IntelliJ on java projects

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
like image 722
Kh Jung Avatar asked Aug 13 '15 18:08

Kh Jung


1 Answers

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.

like image 72
Sun Avatar answered Nov 11 '22 07:11

Sun