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

前端 未结 8 941
抹茶落季
抹茶落季 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'
    }
    

提交回复
热议问题