Dagger 2 Annotation Processor Not 运行 for Java Gradle IntelliJ Project

Dagger 2 Annotation Processor Not Running for Java Gradle IntelliJ Project

我正在玩一个 Gradle java 项目,我很难让注释处理器达到 运行。出于某种原因,当我 运行 一个 intellij 配置(如下图所示)时,注释处理器没有 运行ning。我假设这是因为配置在启动前将 Make 命令配置为 运行。当调用 assemblebuild 时,注释处理器似乎 运行。

调用 ./gradlew clean make 时可重现该问题。我在调用 ./gradlew clean assemble./gradlew clean build 时没有这个问题。解决这个问题的最佳做法是什么?

您没有应用 APT 插件

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

apply plugin: 'com.neenbedankt.android-apt'

或者对于核心,其纯Java替代:

https://plugins.gradle.org/plugin/net.ltgt.apt

也可以尝试使用 apt 而不是 providedCompile

IntelliJ 需要为项目启用注释处理。这是一张图片,详细说明了您可以在何处为 IntelliJ 启用注释处理:

首选项 > 构建、执行、部署 > 编译器 > 注释处理器 > 检查 "Enable annotation processing"

  • 确保为您的项目启用注释处理(如@spierce7 所述)
  • 还要确保 apply plugin: 'idea' 在您的 build.gradle

示例build.gradle 片段:

plugins {
    id "net.ltgt.apt" version "0.5"
}

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

...

dependencies {
    compile 'com.google.dagger:dagger:2.10'
    apt 'com.google.dagger:dagger-compiler:2.10'
}

来自:https://github.com/tbroyer/gradle-apt-plugin(github 对于 net.ltgt.apt 插件)

IntelliJ IDEA

When the idea plugin is applied, the idea task will auto-configure the generated files to enable annotation processing in intelliJ IDEA.

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.

Note that starting with IntelliJ IDEA 2016.1, you'll have to uncheck Create separate module per source set when importing the project.

In any case, the idea plugin has to be applied to the project.

An alternative, starting with IntelliJ IDEA 2016.3, is to delegate the IDE build actions to Gradle itself: https://www.jetbrains.com/idea/whatsnew/#v2016-3-gradle