AouledIssa
AouledIssa

Reputation: 2824

How to invoke Annotation Processor from Gradle plugin

I am currently working on a Gradle custon plugin that should analyse my root project for specific configs in every subproject and then generate some kotlin source code in the build dir. I can't figure out a way to invoke my annotation processor from my gradle plugin which has a custom task for this matter.

Any ideas how to achieve this? Any resource/tutorial/documentation is also highly welcomed.

Thanks in advance and be safe.

Upvotes: 4

Views: 3678

Answers (1)

AouledIssa
AouledIssa

Reputation: 2824

After a long time of googling and mostly trying and failing, I finally figured out the solution to my question. Here is my task configuration.

Basically we have to provide the annotation processor's classpath as a project configuration. In my case I added this block to the project's build.gradle

allprojects {
    configurations {
        myProcessor //pick any name!!!
    }
}

and then as a dependency in app build.gradle

dependencies {
    myProcessor "PATH_TO_MY_PROCESSOR_JAR" //or maven dependency if it's uploaded to maven central
}
tasks.register(
"myTaskName",
JavaCompile::class.java
) {
    compiler ->
    with(compiler.options) {
        isFork = true
        isIncremental = true
    }
    with(compiler) {
        group = shuttle.plugin.ShuttlePlugin.TASK_GROUP
        destinationDir = outputDir
        classpath = variant.getCompileClasspath(null)
        options.annotationProcessorPath = configurations.getByName("myProcessor") //this is the missing piece!!
        source = files(projectDir.resolve("src/main/java")).asFileTree
    }
}

However, this task will only compile Java classes Only and not kotlin. Any Idea how to fix this behaviour knowing that my plugin targets only android apps so I don't have direct access to kotlinCompile gradle default task?

Upvotes: 3

Related Questions