How do I configure IntelliJ/gradle to use dagger 2.0

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

    I've found a solution.

    https://github.com/tbroyer/gradle-apt-plugin

    buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.3"
      }
    }
    
    apply plugin: "net.ltgt.apt"
    
    dependecies {
      apt 'com.google.dagger:dagger-compiler:2.0.1'
      compile 'com.google.dagger:dagger:2.0.1'
    }
    

    Additionally if you are using Intellij a following configuration is recommended:

    When using the Gradle integration in IntelliJ IDEA however, rather than the idea task, you'll have to manually enable annotation processing: in Settings… → Build, Execution, Deployment → Compiler → Annotation Processors, check Enable annotation processing and Obtain processors from project classpath. To mimic the Gradle behavior and generated files behavior, you can configure the production and test sources directories to build/generated/source/apt/main and build/generated/source/apt/test respectively and choose to Store generated sources relative to: Module content root. I've also had to remove Exclude from whole build directory and mark generated/source/apt/main directory as source.

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

    I too couldn't get any of the plugins to work, so based on Stefan's response I did the following which works, but annoyingly IntelliJ seems to create group modules, which weren't there before. Be great if anyone has any idea what is causing this I would really like to get this fixed.

    apply plugin: 'java'
    apply plugin: 'idea'
    
    configurations {
        compileDagger
    }
    
    def genPath = new File(buildDir,"generated/source/apt/main" )
    
    task createGenPath << {
        if(!genPath.exists()){
            genPath.mkdirs()
        }
    }
    
    compileJava.dependsOn(createGenPath)
    
    compileJava {
        source += genPath
        classpath += configurations.compileDagger
        options.compilerArgs += ['-s', genPath]
    }
    
    idea.module {
        sourceDirs += genPath
    }
    
    dependencies {
        compileDagger "com.google.dagger:dagger-compiler:${dagger2Version}"
        compile "com.google.dagger:dagger:${dagger2Version}"
    }
    
    0 讨论(0)
提交回复
热议问题