1

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

spierce7
  • 14,797
  • 13
  • 65
  • 106
  • Does changing `providedCompile` to `compile` work? And can you define 'getting the compiler to run', e.g. doesn't it generate the sources or is your IDE unable to find the sources? – nhaarman Aug 09 '15 at 20:55
  • I've tried `compile` as well. I thought it might be IntelliJ not being able to find the sources, so I created a separate project. I can't manually find the generated sources in any of the build folders either. It seems to me that it's simply not running. – spierce7 Aug 09 '15 at 21:15
  • Have you tried adding a task dependency? e.g. `make.dependsOn(build)` – Ben Manes Aug 09 '15 at 22:19
  • @BenManes Good idea. Just tried it and it didn't work. `Error:(13, 0) Could not find property 'make' on root project 'game'.` – spierce7 Aug 09 '15 at 22:26
  • Maybe you gain the help this links. https://stackoverflow.com/questions/25239243/annotation-processor-in-gradle-outputs-source-files-to-build-classes-making-java/25242121#25242121 – Kh Jung Aug 13 '15 at 17:45

3 Answers3

5

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

spierce7
  • 14,797
  • 13
  • 65
  • 106
  • Hm, I didn't know you need to explicitly enable annotation processing in IntelliJ too. It worked out of the box in Android once you included and applied the apt plugin. Nice find. – EpicPandaForce Aug 09 '15 at 22:54
  • This still wasn't enough for me, I had to choose Module content root and then manually mark the generated source folders as such – Novaterata May 06 '16 at 18:24
  • That is not even an option. This answer appears to be outdated and no longer of value. – Rockin4Life33 Dec 28 '16 at 20:03
  • 1
    @Anovative Just confirmed the answer is still valid on the latest IntelliJ – spierce7 Dec 29 '16 at 05:52
  • Thanks for the confirmation @spierce7. This appears to be something Google did not include in Android Studio (v2.2.3) as there are no subcategories of the 'Compiler' category. – Rockin4Life33 Dec 29 '16 at 18:54
  • @Anovative I just found the exact same setting in the exact same place on Android Studio. After I let one of my projects load though, the setting disappeared. It's likely because my project (and likely yours) is configured as an Android project through Gradle, and the Android plugin handles the annotation processors differently. The above steps were specifically for java projects. If you are using Android, I'd recommend you use the apt plugin, which is standard for Android: https://bitbucket.org/hvisser/android-apt – spierce7 Dec 30 '16 at 07:54
  • @spierce7 Thanks! If I'm not mistaken as of Gradle 2.2.x and above the apt plugin is no longer necessary as Gradle will natively handle annotation processing. Just swap `apt` for `annotationProcessor`. But I'm falling out of the scope of this post since I'm working with an Android project in Android Studio. – Rockin4Life33 Dec 30 '16 at 18:24
2
  • 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

I_AM_PANDA
  • 532
  • 1
  • 4
  • 8
-3

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

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428