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条回答
迷人小祖宗
2楼-- · 2019-02-02 04:16

I had problems with the existings plugins, so I added the following to my build.gradle:

def daggerVersion = "2.4"

// APT >>
def genPath = new File(buildDir,"generated/java/APT" )

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

compileJava {
     options.compilerArgs << '-s' << genPath
}
// APT <<


dependencies {
    compile "com.google.dagger:dagger:$daggerVersion"
    compile "com.google.dagger:dagger-compiler:$daggerVersion"
}

// APT IDEA >>
idea.module {
    sourceDirs += genPath
    // maybe add to generatedSourceDirs
    iml {
        withXml {
            File ideaCompilerXml = project.file('.idea/compiler.xml')
            if (ideaCompilerXml.isFile()) {
                Node parsedProjectXml = (new XmlParser()).parse(ideaCompilerXml)
                updateIdeaCompilerConfiguration(parsedProjectXml)
                ideaCompilerXml.withWriter { writer ->
                    XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(writer))
                    nodePrinter.setPreserveWhitespace(true)
                    nodePrinter.print(parsedProjectXml)
                }
            }
        }
    }
}

static void updateIdeaCompilerConfiguration( Node projectConfiguration) { //actually resets APT
    Object compilerConfiguration = projectConfiguration.component.find { it.@name == 'CompilerConfiguration' }
    compilerConfiguration.annotationProcessing.replaceNode{
        annotationProcessing() {
            profile(default: 'true', name: 'Default', enabled: 'true') {
                sourceOutputDir(name: '')
                sourceTestOutputDir(name: '')
                outputRelativeToContentRoot(value: 'true')
                processorPath(useClasspath: 'true')
            }
        }
    }
}
// APT IDEA <<
查看更多
手持菜刀,她持情操
3楼-- · 2019-02-02 04:19

I finished up with the following solution (and it seems to be the simplest one from all the sent answers):

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

def generatedMain = new File(buildDir, "generated/main")

compileJava {
    doFirst {
        generatedMain.mkdirs()
    }
    options.compilerArgs += ['-s', generatedMain]
}
idea.module.sourceDirs += generatedMain

dependencies {
    compileOnly 'com.google.dagger:dagger-compiler:2.8'
    compile 'com.google.dagger:dagger:2.8'
}
查看更多
萌系小妹纸
4楼-- · 2019-02-02 04:20

In my case the problem was IDEA creating a separate module for the dagger generated files. I had to go to File -> Project Structure -> Modules and remove the projectname_dagger module (by clicking the red minus), then add the generated source folder to my projectname_main module by clicking Add Content Root and selecting it.

The for some reason I had to delete Dagger's files and let IDEA regenerate them because I was getting errors about duplicate files in the project.

Now it works, event with Annotation Processors being turned off (I suspect they must mainly be important for Android projects).

查看更多
叼着烟拽天下
5楼-- · 2019-02-02 04:21

I've found a solution.

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

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

apply plugin: "net.ltgt.apt"

dependecies {
  apt 'com.google.dagger:dagger-compiler:2.0.1'
  compile 'com.google.dagger:dagger:2.0.1'
}

Additionally if you are using Intellij a following configuration is recommended:

When using the Gradle integration in IntelliJ IDEA however, rather than the idea task, you'll have to manually enable annotation processing: in Settings… → Build, Execution, Deployment → Compiler → Annotation Processors, check Enable annotation processing and Obtain processors from project classpath. To mimic the Gradle behavior and generated files behavior, you can configure the production and test sources directories to build/generated/source/apt/main and build/generated/source/apt/test respectively and choose to Store generated sources relative to: Module content root. I've also had to remove Exclude from whole build directory and mark generated/source/apt/main directory as source.

查看更多
贪生不怕死
6楼-- · 2019-02-02 04:22

The easiest way, I know of is to use the apt-idea plugin

Just activate the plugin in the build.gradle file:

plugins {
    id 'java'
    id 'net.ltgt.apt-idea' version "0.15"
}

and then add the annotation processors to the annotationProcessor configuration:

final DAGGER_VER = '2.16'
dependencies {
    implementation "com.google.dagger:dagger:${DAGGER_VER}"
    annotationProcessor"com.google.dagger:dagger-compiler:${DAGGER_VER}"
}

I've created a very simple test-project on GitHub: ex.dagger
(using IntelliJ 2018.1.4, Gradle 4.7)

查看更多
劫难
7楼-- · 2019-02-02 04:25

Since net.ltgt.apt version 0.11 (Feb 2018) you can just apply the plugin net.ltgt.apt-idea to build.gradle:

plugins {
    id "net.ltgt.apt-idea" version "0.18"
}

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

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