spierce7
spierce7

Reputation: 15746

Dagger 2 Annotation Processor Not Running for Java Gradle IntelliJ Project

I'm playing around with a Gradle java project, and I'm having a difficult time getting annotation processor's to run. For some reason when I run an intellij configuration (pictured below), the annotation processors aren't running. I'm assuming this is because the configuration has the Make command configured to run before launch. The annotation processors seem to run when assemble or build is called.

The issue is reproducible when calling ./gradlew clean make. I don't have that issue when calling ./gradlew clean assemble, or ./gradlew clean build. What's the best practice for getting around this?

enter image description here

Upvotes: 1

Views: 4971

Answers (3)

I_AM_PANDA
I_AM_PANDA

Reputation: 542

  • make sure that Annotation Processing is enabled for your project (as described by @spierce7)
  • also make sure that apply plugin: 'idea' is in your build.gradle

sample build.gradle snippet:

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

from: https://github.com/tbroyer/gradle-apt-plugin (github for net.ltgt.apt plugin)

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

Upvotes: 2

spierce7
spierce7

Reputation: 15746

IntelliJ needs Annotation Processing enabled for the project. Here is an image that details where you can enable Annotation Processing for IntelliJ:

Preferences > Build, Exection, Deployment > Compiler > Annotation Processors > Check "Enable annotation processing"

enter image description here

Upvotes: 5

EpicPandaForce
EpicPandaForce

Reputation: 81539

You are not applying the APT plugin

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

And

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

Or for the core, its pure-Java alternative:

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

Also try using apt instead of providedCompile

Upvotes: -3

Related Questions