I'm writing a gradle plugin to generate Java code. My plugin generates code that's based on the Android R class, so it's dependent on the
processVariantResources
Task. In my plugin, I'm using this code to do that:
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create<MyPluginExtension>(PLUGIN_NAME, MyPluginExtension::class.java)
project.tasks.whenTaskAdded { task ->
if (task.name.startsWith("process") && task.name.contains("Resources") && !task.name.contains("Test")) {
val index = task.name.indexOf("Resources")
val variantName = task.name.substring(7, index)
//Create my task and add it to the project, make it dependent on processResources
project.task("my${variantName}ResourceTask") {
it.doLast {
generateSomeCodeForVariant(project, extension, variantName)
}
taskList.add(it)
}.dependsOn(task)
}
}
project.afterEvaluate {
val appExtension = project.extensions.findByType(AppExtension::class.java)
appExtension?.applicationVariants?.all { variant ->
val myTask = project.tasks.getByName("my${variant.name.capitalize()}ResourcesTask")
val outputDir = "${project.buildDir}/generated/source/myplugin/${variant.name}"
//register my task as java generating
variant.registerJavaGeneratingTask(myTask, File(outputDir))
}
}
}
then, in the build.gradle of the project where I'm using this plugin, I've added
android {
sourceSets {
main {
java.srcDirs += ['build/generated/source/myplugin']
kotlin.srcDirs += ['build/generated/source/myplugin']
}
}
}
My plugin actually generates source code to the directory:
build/generated/source/myplugin/com/mygroup/myartifact
Anyway, the code gets generated correctly, and put in the correct place, but I can't get the compiler to recognize my generated code. Any help would be appreciated.