How to invoke Annotation Processor from Gradle plugin

笑着哭i 提交于 2020-06-13 08:32:09

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/60886346/how-to-invoke-annotation-processor-from-gradle-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!