How do I configure IntelliJ/gradle to use dagger 2.0

后端 未结 8 2316
野的像风
野的像风 2020-12-28 16:01

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

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 16:18

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

提交回复
热议问题