smeeb
smeeb

Reputation: 29507

Gradle commands/configs to process annotations

I have a Java project that uses annotation processors to generate source code that must then, subsequently, be added to the compile classpath and compiled/packaged.

This project is built by Gradle, and so I am wondering how to invoke these annotation processors under the hood so that:

  1. First the annotations are processed and the required source code is generated under src/main/java; and then
  2. When Gradle gets to the compilation phase, that source code already exists and can be compiled like all the other sources

When I run gradle clean build the code does not get generated, and other sources (which depend on the generated classes being there) fail compilation.

I am trying to understand what needs to be done so that Gradle will run the necessary annotation processors and generate the source code. The specific processors at hand are those required by the Immutables project (that is, my app uses Immutables to generate immutable objects), however I would think the answer to this question is general and project-agnostic.


Update

Something like this (?):

dependencies {
    // ...
    apt 'com.squareup.dagger:dagger-compiler:1.1.0' 
    apt "org.immutables:value:2.0.21"

    compile 'com.squareup.dagger:dagger:1.1.0'         

    provided "org.immutables:value:2.0.21:annotations"
    provided "org.immutables:builder:2.0.21"
    provided "org.immutables:gson:2.0.21:annotations"
}

Upvotes: 3

Views: 1852

Answers (1)

Viktor Yakunin
Viktor Yakunin

Reputation: 3266

This one should help you: https://bitbucket.org/hvisser/android-apt. Example from the following link:

dependencies {
   apt 'com.squareup.dagger:dagger-compiler:1.1.0' 
   compile 'com.squareup.dagger:dagger:1.1.0' 
}

EDIT

It seems that the library You want to use is already have manual http://immutables.github.io/getstarted.html

EDIT2

compile directive gives your project access to annotations, but apt directive would start apt(annotated processing tool).So the full build.gradle would be:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
    }
}

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

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.yourpackage"//FIXME
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1//FIXME
        versionName "test"//FIXME
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    apt "org.immutables:value:2.0.21" // for annotation processor
    provided "org.immutables:value:2.0.21:annotations" // annotation-only artifact
    provided "org.immutables:builder:2.0.21" // there are only annotations anyway
    provided "org.immutables:gson:2.0.21:annotations" // annotation-only artifact
}
apt {
    arguments {
        resourcePackageName android.defaultConfig.applicationId
        androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}

Upvotes: 2

Related Questions