【问题标题】:How do I configure IntelliJ/gradle to use dagger 2.0如何配置 IntelliJ/gradle 以使用 dagger 2.0
【发布时间】:2015-04-10 13:11:35
【问题描述】:

我有一个 gradle 项目,我想在其中使用 dagger 2.0。不知道如何配置IntelliJ和gradle生成文件让IntelliJ找到?

我的 build.gradle 文件如下所示:

apply plugin: 'java'
apply plugin: 'idea'

version = '1.0'

repositories {
    mavenCentral()
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.12'
    compile 'org.slf4j:slf4j-simple:1.7.12'
    compile 'commons-configuration:commons-configuration:1.10'
    compile 'commons-collections:commons-collections:3.2.1'
    compile 'com.google.dagger:dagger:2.0'
    compile 'com.google.dagger:dagger-compiler:2.0:jar-with-dependencies'
    compile 'com.pi4j:pi4j-distribution:1.1-SNAPSHOT'
}

在我的应用程序的构建目录中,存在DaggerXmlConfigurationComponent 文件,这是 Dagger 创建的组件。但我不能在 IntelliJ 中使用它,因为它找不到类。

这不是 Android 应用程序,而是 Raspberry Pi 的应用程序。

【问题讨论】:

  • 您看到了吗:stackoverflow.com/questions/20107182/…?也许它可以提供帮助。
  • 您找到解决方案了吗?我遇到了同样的问题,文件是生成的,如果我从 cmd 行运行我的构建就可以了,但是 intellij 认为它仍然缺少生成的文件。

标签: java gradle dagger-2


【解决方案1】:

据我所知,最简单的方法是使用apt-idea plugin

只需激活build.gradle文件中的插件:

plugins {
    id 'java'
    id 'net.ltgt.apt-idea' version "0.15"
}

然后将注解处理器添加到annotationProcessor 配置中:

final DAGGER_VER = '2.16'
dependencies {
    implementation "com.google.dagger:dagger:${DAGGER_VER}"
    annotationProcessor"com.google.dagger:dagger-compiler:${DAGGER_VER}"
}

我在 GitHub 上创建了一个非常简单的测试项目:ex.dagger
(使用 IntelliJ 2018.1.4、Gradle 4.7)

【讨论】:

    【解决方案2】:

    您必须手动为 IntelliJ 启用注释处理:在 Settings... → Build, Execution, Deployment → Compiler → Annotation Processors,选中 Enable annotation processing 并从项目类路径获取处理器。

    【讨论】:

      【解决方案3】:

      我也无法让任何插件工作,因此根据 Stefan 的回复,我做了以下工作,但令人讨厌的是 IntelliJ 似乎创建了以前不存在的组模块。如果有人知道是什么原因造成的,那就太好了,我真的很想解决这个问题。

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

      【讨论】:

        【解决方案4】:

        我找到了解决办法。

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

        此外,如果您使用 Intellij,建议使用以下配置:

        然而,当在 IntelliJ IDEA 中使用 Gradle 集成时,您必须手动启用注释处理,而不是使用 IDEA 任务:在 Settings... → Build, Execution, Deployment → Compiler → Annotation Processors 中,选中 Enable annotation processing 并获取来自项目类路径的处理器。为了模仿 Gradle 行为和生成文件行为,您可以将生产和测试源目录分别配置为 build/generated/source/apt/main 和 build/generated/source/apt/test 并选择将生成的源相对于:模块内容根。 我还必须从整个构建目录中删除 Exclude 并将生成的/source/apt/main 目录标记为源。

        【讨论】:

        • 在我添加“idea”插件之前,它对我没有用。即使没有启用注释处理器,它也可以工作。 apply plugin:'idea'
        • +1 表示以“我还必须删除”开头的最后一句“我还必须从整个构建目录中删除排除并将生成/源/apt/主目录标记为源”
        【解决方案5】:

        我完成了以下解决方案(它似乎是所有已发送答案中最简单的一个):

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

        【讨论】:

          【解决方案6】:

          我在使用现有插件时遇到问题,因此我将以下内容添加到我的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 <<
          

          【讨论】:

            【解决方案7】:

            在我的情况下,问题是 IDEA 为 dagger 生成的文件创建了一个单独的模块。我必须转到File -&gt; Project Structure -&gt; Modules 并删除projectname_dagger 模块(通过单击红色减号),然后通过单击Add Content Root 并选择它,将生成的源文件夹添加到我的projectname_main 模块。

            出于某种原因,我不得不删除 Dagger 的文件并让 IDEA 重新生成它们,因为我收到有关项目中重复文件的错误。

            现在它可以工作了,注解处理器被关闭的事件(我怀疑它们必须主要对 Android 项目很重要)。

            【讨论】:

              【解决方案8】:

              自 net.ltgt.apt 版本 0.11(2018 年 2 月)起,您只需将插件 net.ltgt.apt-idea 应用到 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"
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-03-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-01-17
                相关资源
                最近更新 更多