用于注释处理的渐变自定义JavaCompile任务

问题描述 投票:0回答:1

我正在开发一个gradle插件,该插件注册了一个JavaCompile任务,该任务应触发注释处理过程。

这是我目前拥有的

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())
        }
    }

在一个Android项目中,当我应用插件并使用gradle运行任务时,到目前为止没有任何反应。我什至试图在处理器的第一行抛出异常,但仍然没有成功。

我错过了什么还是做错了什么?我又如何告诉任务它应该使用ShuttleProcessor作为注释处理器。

我正在使用kotlin,而不是Groovy

谢谢。

android gradle annotation-processing javacompiler
1个回答
0
投票

我已经找到了解决问题的方法。 JavaCompile任务必须知道我们使用的处理器在哪里。所以缺少的部分是这些行

val classPath = variant.getCompileClasspath(null)

并且我任务的配置应包含这些更改

classpath = classPath
options.annotationProcessorPath = classPath
© www.soinside.com 2019 - 2024. All rights reserved.