【问题标题】:Dagger 2 does not generate componentDagger 2 不生成组件
【发布时间】:2017-01-25 03:43:20
【问题描述】:

非 Android 应用(IDE 是 IntelliJ IDEA)中的 Dagger 2 让我筋疲力尽。

build.gradle

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.9"
    }
}

apply plugin: 'java'
apply plugin: 'net.ltgt.apt'

dependencies {
    compile 'com.google.dagger:dagger:2.8'
    apt 'com.google.dagger:dagger-compiler:2.8'
    compileOnly 'javax.annotation:jsr250-api:1.0'
}

组件

@Component(modules = {MyModule.class}) public interface MyComponent {}

模块

@Module class MyModule {}

主要

DaggerMyComponent component = DaggerMyComponent.builder().build();

编译器说

找不到符号 DaggerMyComponent

绝对没有更多的想法可能是错误的:(

顺便说一句,如果我运行gradlew clean build --refresh-dependencies,没有错误,一切正常!

但因为只有我在 IDE 中使用“重建项目” - 再次出错。可能是 IDEA 错误(是时候回到 Eclipse)?

是的,我添加了包含生成类的目录作为内容根目录,因此 IDE 可以看到 DaggerMyComponent。

【问题讨论】:

    标签: java intellij-idea dagger-2


    【解决方案1】:

    我已将问题发布到 JetBrains

    https://youtrack.jetbrains.com/issue/IDEA-169387

    解决方案在第一条评论中。

    如果您使用 IntelliJ 内部编译器构建您需要的项目 在文件|启用注释处理设置 |构建,执行, 部署 |编译器 |注释处理器

    作为替代方案,您可以将构建/运行 IDE 操作委托给 gradle。 您可以在 文件 | 中找到该选项。设置 |构建,执行, 部署 |构建工具 |摇篮 |跑步者

    在构建中 171.4073.35 默认设置Delegate IDE build/run actions to gradle 选项。

    【讨论】:

    • 啊哈,我已经启用了注释处理,正如对该问题其他风格的回答所暗示的那样(例如here),但是关于委派构建/运行选项的提示对我来说是新的.感谢您努力找出这一点:)
    • 你拯救了我的一天!非常感谢!
    【解决方案2】:

    Alexey 的回答很好,但是 intelliJ 不仅在不清理的情况下切换到注释处理器有问题,您还需要验证是否在中启用了注释处理器

    文件 -> 其他设置 -> 默认设置 -> 编译器 -> 注释处理器

    如果启用此功能,您还可以更改您的 compiler.xml

    [project root]/.idea/compiler.xml 并为您的配置文件启用 annotationPorcessing 为 true。 (检查下面从另一个stackoverflow帖子复制的图片)

      <component name="CompilerConfiguration">
        <annotationProcessing>
          <profile default="true" name="Default" enabled="true" />
        </annotationProcessing>
      </component>
    

    确保[File -> invalidate Cache and Restart] after

    其他人报告说他们不得不使用

    应用插件:'idea'

    在他们的 gradle 文件中让 intellij 识别 dagger 生成的类。

    【讨论】:

      【解决方案3】:

      除了启用 @spierce7 所示的注释处理之外,我的问题是我在没有设置 Gradle -> Runner 的情况下从 IntelliJ 的 Build 菜单运行 Build Project 将 IDE 构建/运行操作委托给 gradle。在终端运行./gradlew build 并看到 build/generated 文件夹已创建后,我意识到了这一点。

      让我感到困惑的另一个原因是,在我单击 ide 的那个部分之前,目录列表不会在 IntelliJ 中更新。

      我删除了apply plugin: 'idea',它仍然有效,所以没有必要。

      我不确定 File -> invalidate Cache and Restart 是否必要,但正如@Emanuel Seibold 在我注意到它开始工作之前指出的那样,我确实尝试过。

      【讨论】:

        【解决方案4】:

        我是 IntelliJ IDEA 的 2017.3.3 版本,net.ltgt.apt 插件的 0.14 版本和 Dagger 的 2.14.1 版本,对我有用的是将以下内容添加到我的 build.gradle 文件中,以告诉 IntelliJ 在哪里它可以找到 Dagger 生成的源代码:

        apply plugin: 'idea'
        idea {
            module {
                sourceDirs += file("$buildDir/generated/source/apt/main")
                testSourceDirs += file("$buildDir/generated/source/apt/test")
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2017-10-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-04
          • 1970-01-01
          • 2020-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多