简体   繁体   English

如何在java项目中使用IntelliJ中的Dagger2

[英]How can I use Dagger2 in IntelliJ on java projects

I want to use Dagger in IntelliJ but I can't use it. 我想在IntelliJ使用Dagger ,但我无法使用它。 Dagger uses an annotation processor and I guess IntelliJ doesn't know about the annotation processor. Dagger使用注释处理器,我猜IntelliJ不知道注释处理器。

You can see the generated java file, it's generated by the Dagger2 compiler, but my java source can't find them. 你可以看到生成的java文件,它是由Dagger2编译器生成的,但是我的java源代码找不到它们。 Even if I set build.gradle to connect between my java file and the generated java file. 即使我设置build.gradle来连接我的java文件和生成的java文件。

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

I faced the same problem. 我遇到了同样的问题。 I found this gradle plugin which does the job: 我发现这个gradle插件可以完成这项工作:

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

In fact it has an example on how to use it with dagger2. 事实上它有一个如何使用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. 所有道具都应该转到插件的原作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM