Jan
07/02/2024, 2:43 PMclass MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
val myPlugin = project.extensions.create("myPlugin", MyPluginExtension::class.java)
val kotlin = project.extensions.getByType(KotlinProjectExtension::class.java)
kotlin.sourceSets.forEach { sourceSet ->
val destination = project.layout.buildDirectory.dir("generated/myPlugin/${sourceSet.name}") // TODO: this feels wrong
sourceSet.kotlin.srcDir(destination)
project.tasks.register(
"generateMyCode${sourceSet.name}", // TODO: this feels wrong
GenerateCodeTask::class.java
) {
it.destination.set(destination)
}
}
}
}
The source set does get detected but registering all the tasks doesn't feel right. I thought of this because in Android you get all those tasks for each build variant, but like this many more tasks are registered (e.g. for *AndroidTest)
Follow up question: what's the correct way to attach this to e.g. the assemble task?Vampire
07/02/2024, 3:12 PMassemble task should not get these attached.
And you should not register the plain directory as source dir.
You should instead make sure that the GenerateCodeTask task properly declare its inputs and outputs as always, and then set the instances of this task as srcDir this will then automatically add task dependencies where needed automatically, so that every consumer of source files - be it a compile task, as static code analysis task, a sources jar task, ... - automatically gets the necessary task dependency and sees all sources.
At least that's how it works with "normal" projects. Not sure how to do it properly for Android as Android is always special and I'm not into android development. 🙂Jan
07/03/2024, 6:40 AMclass GenerateMyCodeTask : DefaultTask() {
@get:OutputDirectory val destination: DirectoryProperty = project.objects.directoryProperty()
}
And I changed the plugin to
val task = project.tasks.register(...) { it.destination.set(destination) }
sourceSet.kotlin.srcDir(task.get().outputs)
Is that the intended way?Vampire
07/03/2024, 8:58 AMsourceSet.kotlin.srcDir(task) should do if you do not have other outputs in it.
If you do, using get() is bad as you break task-configuration avoidance, but should use flatMap in that case.Martin
07/03/2024, 1:41 PMmain (or commonMain ) Kotlin source setMartin
07/03/2024, 1:42 PMmain /`commonMain` is a lot simplerMartin
07/03/2024, 1:44 PMmain source set)Jan
07/04/2024, 1:46 PMJan
07/04/2024, 1:50 PMtry { kotlin.sourceSets.getByName("commonMain").kotlin.srcDir(task)
} catch (e: UnknownDomainObjectException) { kotlin.sourceSets.getByName("main").kotlin.srcDir(task)
}
Now that seems to work in Android project, which is fine for now. Haven't tested it in KMP yet. I did find some extension for KMP source sets but they were internal. So is my way the intended way (or at least acceptable 😛 )?Martin
07/04/2024, 1:51 PMJan
07/04/2024, 1:52 PMMartin
07/04/2024, 1:52 PMpluginManager.withId("com.android.library"), .... instead of a big try/catch but I guess the try/catch should work tooMartin
07/04/2024, 1:54 PMkotlin.sourceSets.getByName("main").kotlin.srcDir(task) works on Android with some limitations around tooling (IDE and lint IIRC)Martin
07/04/2024, 1:56 PMsrcDir() is “good enough”, especially I’m not sure I’m getting by using the newer methodsJan
07/04/2024, 1:59 PMMartin
07/16/2024, 3:00 PMMartin
07/16/2024, 3:00 PMFredrick Eisele
07/16/2024, 3:03 PM