Skip to content

Gradle & Annotation processing

Nikhil Purushe edited this page Dec 10, 2018 · 19 revisions

Annotation processing takes a few steps to setup correctly in gradle.

Which configuration to use?

For Android projects use annotationProcessor configuration that is part of the Android Gradle plugin (2.2 and later) when specifying the requery-processor dependency.

annotationProcessor 'io.requery:requery-processor:1.1.0'

For Java projects use gradle-apt-plugin:

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

apply plugin: `net.ltgt.apt`
dependencies {
...
    apt 'io.requery:requery-processor:<version>'
}

For Kotlin projects be sure to add the kotlin-kapt plugin:

buildscript {
    ext.kotlin_version = '1.3.11'
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'

dependencies {
...
    kapt 'io.requery:requery-processor:<version>'
}

Note the need for plugins for proper annotation processing may get addressed in a future version of gradle, as described here

Why not use the provided scope?

You can use provided scope on an annotation processor dependency in gradle like so:

provided 'io.requery:requery-processor:1.5.1'

This works and the processor will run and generate source files. However there are problems:

  1. In IntelliJ the generated files are not visible to the IDE and appear red, even though the code compiles.

  2. The dependencies of the processor are now put on to your project classpath which is not desired.

You can’t perform that action at this time.