Martijn de Munnik
Martijn de Munnik

Reputation: 986

How do I configure IntelliJ/gradle to use dagger 2.0

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.

Upvotes: 27

Views: 8702

Answers (8)

Malt
Malt

Reputation: 30335

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"
}

Upvotes: 0

TmTron
TmTron

Reputation: 19441

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)

Upvotes: 5

Tomasz Gawel
Tomasz Gawel

Reputation: 8520

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'
}

Upvotes: 2

Gregor Petrin
Gregor Petrin

Reputation: 2931

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).

Upvotes: 1

John Howard
John Howard

Reputation: 31

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}"
}

Upvotes: 3

Stefan K.
Stefan K.

Reputation: 7851

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 <<

Upvotes: 1

Abdel
Abdel

Reputation: 580

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.

Upvotes: 8

robert_difalco
robert_difalco

Reputation: 4914

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.

Upvotes: 19

Related Questions