How do I configure IntelliJ/gradle to use dagger 2

2019-02-02 04:07发布

I have a gradle project and I want to use dagger 2.0 in it. I don't know how to configure IntelliJ and gradle to generate files and let IntelliJ find them?

My build.gradle file looks like:

apply plugin: 'java'
apply plugin: 'idea'

version = '1.0'

repositories {
    mavenCentral()
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.12'
    compile 'org.slf4j:slf4j-simple:1.7.12'
    compile 'commons-configuration:commons-configuration:1.10'
    compile 'commons-collections:commons-collections:3.2.1'
    compile 'com.google.dagger:dagger:2.0'
    compile 'com.google.dagger:dagger-compiler:2.0:jar-with-dependencies'
    compile 'com.pi4j:pi4j-distribution:1.1-SNAPSHOT'
}

In the build directory of my application the file DaggerXmlConfigurationComponent exists, which is a component Dagger creates. But I can't use it in IntelliJ because it can't find the class.

This is not an Android application but an application for the Raspberry Pi.

8条回答
ら.Afraid
2楼-- · 2019-02-02 04:34

You'll have to manually enable annotation processing for IntelliJ: in Settings… → Build, Execution, Deployment → Compiler → Annotation Processors, check Enable annotation processing and Obtain processors from project classpath.

查看更多
你好瞎i
3楼-- · 2019-02-02 04:38

I too couldn't get any of the plugins to work, so based on Stefan's response I did the following which works, but annoyingly IntelliJ seems to create group modules, which weren't there before. Be great if anyone has any idea what is causing this I would really like to get this fixed.

apply plugin: 'java'
apply plugin: 'idea'

configurations {
    compileDagger
}

def genPath = new File(buildDir,"generated/source/apt/main" )

task createGenPath << {
    if(!genPath.exists()){
        genPath.mkdirs()
    }
}

compileJava.dependsOn(createGenPath)

compileJava {
    source += genPath
    classpath += configurations.compileDagger
    options.compilerArgs += ['-s', genPath]
}

idea.module {
    sourceDirs += genPath
}

dependencies {
    compileDagger "com.google.dagger:dagger-compiler:${dagger2Version}"
    compile "com.google.dagger:dagger:${dagger2Version}"
}
查看更多
登录 后发表回答