Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?

前端 未结 8 933
抹茶落季
抹茶落季 2020-12-14 16:21

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 o

相关标签:
8条回答
  • 2020-12-14 16:25

    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'
    }
    
    0 讨论(0)
  • 2020-12-14 16:28

    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 讨论(0)
  • 2020-12-14 16:33

    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'
    }
    
    0 讨论(0)
  • 2020-12-14 16:34

    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:

    0 讨论(0)
  • 2020-12-14 16:37

    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.

    0 讨论(0)
  • 2020-12-14 16:39

    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")
        }
    }
    
    0 讨论(0)
提交回复
热议问题