androidgradleannotation-processingjavacompiler

Gradle custom JavaCompile task for annotation processing


I am working on a gradle plugin that registers a JavaCompile task that should trigger an annotation processing process.

Here is what I currently have

private fun Project.runConfiguration(variants: DomainObjectSet<out BaseVariant>) {
        variants.all { variant ->
            // Assert Shuttle annotation processor is present or throw exception
            val processor = ShuttleProcessor::class.java.canonicalName ?: throw ShuttleCompilerNotFoundException()
            val packageName = variant.getPackageName()
            val task = tasks.register(
                    "generateShuttle${variant.name.capitalize()}Sources",
                    JavaCompile::class.java
            ) {
                it.group = TASK_GROUP
                it.source = variant.getSourceFolders(SourceKind.JAVA).first()
                it.options.annotationProcessorPath = variant.annotationProcessorConfiguration
                it.options.compilerArgs.addAll(listOf(
                        "-proc:only", "-implicit:none",
                        "-processor", processor
                ))
            }

            variant.registerJavaGeneratingTask(task.get())
        }
    }

In an android project when I apply my plugin and run the task with gradle, Nothing happens so far. I even tried to throw and exception on the first line of my processor but still no success.

Am I missing something or doing something wrong? Also how can I tell the task that it should use ShuttleProcessor as an annotation processor.

I am working with kotlin and not Groovy.

Thanks.


Solution

  • I have found a solution to what wrong was happening. JavaCompile task has to be aware of where is the processor we use. So the missing piece was these lines

    val classPath = variant.getCompileClasspath(null)
    

    and my task's configuration should incorporate these changes

    classpath = classPath
    options.annotationProcessorPath = classPath