How do I configure IntelliJ/gradle to use dagger 2.0

后端 未结 8 2068
野的像风
野的像风 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:07

    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.

    0 讨论(0)
  • 2020-12-28 16:15

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

    0 讨论(0)
  • 2020-12-28 16:15

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

    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)

    0 讨论(0)
  • 2020-12-28 16:23

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