34

Context

I have started a personal project in java with Gradle as the build system and I want to use Dagger 2 as a DI. The main reason of doing that is to get used to that library and be able to use it easily in bigger projects.

What have I tried

I've managed to make the Google sample runs on IntelliJ IDEA

Problem

IntelliJ IDEA keeps telling me that it cannot resolve the generated class (in this case DaggerCoffeeApp_Coffee). It's a bit annoying not to know if the written code is correct (specially when you are learning to use Dagger 2).

All java classes are the same as the Google sample. Here is my build.gradle file:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'com.google.dagger:dagger:2.0.1'
    compile 'com.google.dagger:dagger-compiler:2.0.1'
}

Question

Is there any way to make IntelliJ IDEA recognize DaggerCoffeeApp_Coffee as a generated class (and so make it possible to go to its implementation by `ctrl + left click)?

2
  • You should be able to after you have compiled the app successfully once. Just put your text cursor into the DaggerCoffeeApp_Coffee line on in this word and use Ctrl + B. Oct 30, 2015 at 20:53
  • Indeed, the app is compiled and I managed to run it. I can see the generated class in build/generated/src/coffee/ but IntelliJ keeps telling me that it cannot resolve DaggerCoffeeApp_Coffee
    – Alberto S.
    Oct 30, 2015 at 21:13

9 Answers 9

22

Simplest way I found:

  1. Add idea plugin and add Dagger2 dependency like below:

    plugins {
        id "net.ltgt.apt" version "0.10"
    }
    
    apply plugin: 'java'
    apply plugin: 'idea'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
    
        compile 'com.google.dagger:dagger:2.11'
        apt 'com.google.dagger:dagger-compiler:2.11'
    }
    
  2. Turn on Annotation Processing for IntelliJ: Go to Settings and search for Annotation Processors, check Enable annotation processing like below image:

enter image description here

3
  • Don't know why you're being downvoted this is the only thing that worked for me
    – nmu
    Jun 22, 2017 at 20:59
  • 2
    I'm using the above (plus setting sourceDirs, testSourceDirs) but have found File > Invalidate Caches / Restart necessary sometimes too (or delete .idea and reopen project [losing settings])
    – Ewan
    Sep 27, 2018 at 11:49
  • 1
    Something additional I needed to do in order for IntelliJ to recognize the class and deep link to it (so that we can go to source when we click the class name), I needed to set the corresponding generated sources directory as "generated sources" by right clicking <workspace>/out/production/<project_name>/generated directory and Mark Directory As->Generated Sources Root Oct 23, 2019 at 21:44
18

Finally I made it!

I had to add the apt and the idea plugin so right now my build.gradle file look like this:

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

apply plugin: "net.ltgt.apt"
apply plugin: 'java'
apply plugin: 'idea'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'com.google.dagger:dagger:2.0.1'
    apt 'com.google.dagger:dagger-compiler:2.0.1'
}
3
  • 4
    Adding the 'idea' plugin fixed it
    – Gallal
    Dec 9, 2015 at 18:01
  • thanks - i had an old net.ltgt.gradle:gradle-apt-plugin version which lead me here - setting it to the latest fixed it. cheers
    – bsautner
    Jan 30, 2018 at 17:55
  • 1
    Worked with me. Jazakallah.
    – Imran Khan
    Feb 12, 2019 at 18:42
8

you must manually enable the annotation processing in IntelliJ.

From: Settings --> Build, Execution, Deployment --> Compiler --> Annotation Processors --> Enable annotation processing and Obtain processors from project classpath

then rebuild the project and you will find the generated classes in the project.

Please note that I have used this solution in a (java) android project.

1
  • 5
    This probably helps Android projects but had no effect for server applications in our case. Nov 24, 2016 at 11:12
4

I'm using version 2017.3.3 of IntelliJ IDEA, version 0.14 of the net.ltgt.apt plugin and version 2.14.1 of Dagger and as well as applying the idea plugin in the build.gradle file (as in Pelocho's answer) I found I also had to tell IntelliJ where it can find the sources generated by Dagger, as follows:

apply plugin: 'idea'
idea {
    module {
        sourceDirs += file("$buildDir/generated/source/apt/main")
        testSourceDirs += file("$buildDir/generated/source/apt/test")
    }
}
3

This is what I had to do in order to get Idea to work with Dagger2 and gradle.

  1. Turn on annotation processing as shown in the answers above.
  2. Add the following to the build.gradle file in order for Idea to see the generated classes as sources.

    sourceDirs += file("$projectDir/out/production/classes/generated/")
    

Here's the full listing of my build.gradle

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

idea {
    module {
        sourceDirs += file("$projectDir/out/production/classes/generated/")
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.dagger:dagger:2.16'
    apt 'com.google.dagger:dagger-compiler:2.16'
}

sourceCompatibility = 1.8

Also, I had to add the following gradle task (to my build.gradle file) to clear out my out directory. When I moved some files around and Dagger2 regenerated the source files, the out directory wasn't being cleared out :(. I also included this task in my run configuration, so that it gets triggered before I rebuild my project.

task clearOutFolder(type: Delete) {
    delete 'out'
}
2
  • Thanks, using IntelliJ 2021.3.3 (UE) and this worked. Sep 25, 2022 at 20:06
  • Would like to specify that I did a slight omdification in your module paths: idea { module { sourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/main") testSourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/test") } } Sep 25, 2022 at 21:46
2

Here's the solution that worked for me:

File -> Project Structure -> (select your project under list of modules) -> Open 'Dependencies' tab

Then, click on green '+' sign, select 'JARs or directory' and select 'build/classes/main' folder.

Another solution would be to link folder with build class files using 'dependencies' block inside build.gradle:

https://stackoverflow.com/a/22769015/5761849

1

Using IntelliJ IDEA 2019.1 and Gradle 5.4.1, this seems to be enough:

plugins {
    id 'java'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.12'

    implementation 'com.google.dagger:dagger:2.23.1'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.23.1'
}

I don't know the minimal versions for which this solution works, though.

0

I had a similar problem, I could not find out the cause for a long time.

Just launched and the result surprised me. IDE displays an error Intellij Idea 2018.3.6 - build.gradle:

plugins {
    id "java"
}
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'com.google.dagger:dagger:2.11'
    apt 'com.google.dagger:dagger-compiler:2.11'
}
0

The following worked for me on IntelliJ 2021.3.3 (UE)

plugins {
    id 'java'
    id 'idea'
    id("com.github.johnrengelman.shadow") version "7.1.2"
}

idea {
    module {
        sourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/main")
        testSourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/test")
    }
}

group 'com.codigomorsa'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor 'com.google.dagger:dagger-compiler:2.44'
    implementation 'com.google.code.gson:gson:2.9.1'
    implementation 'com.google.dagger:dagger:2.44'
    testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.44'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}

test {
    useJUnitPlatform()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.