Hi,
I’m developing a Gradle plugin that runs a compiler to generate Java code and need to add the generated files to the configured source set.
For Java, I extend each source set with a custom extension for configuration, then link the task output to the Java source set. This works as expected:
fun configure(
project: Project,
toolsPath: Provider<String>,
includeSearchPath: Provider<List<String>>,
extension: SliceExtension,
compileSlice: TaskProvider<Task>,
) {
val javaExtension = project.extensions.findByType(JavaPluginExtension::class.java)
?: throw GradleException("JavaPluginExtension is missing. Ensure the Java plugin is applied before configuring Slice Tools.")
javaExtension.sourceSets.configureEach { sourceSet ->
val sliceSourceSet = SliceToolsUtil.createSliceSourceSet(extension, sourceSet.name)
sourceSet.extensions.add("slice", sliceSourceSet)
val compileTask = SliceToolsUtil.configureSliceTaskForSourceSet(
project,
toolsPath,
includeSearchPath,
extension,
sliceSourceSet,
compileSlice,
)
sourceSet.java.srcDir(compileTask.flatMap { it.output })
}
}
When I run compileJava, the corresponding compileSliceMain task is triggered, and everything works smoothly:
:ice:compileSliceMain SKIPPED
:ice:compileJava SKIPPED
However, I cannot get the Android version to work:
fun configure(
project: Project,
toolsPath: Provider<String>,
includeSearchPath: Provider<List<String>>,
extension: SliceExtension,
compileSlice: TaskProvider<Task>,
) {
val androidExtension = project.extensions.findByType(BaseAppModuleExtension::class.java)
?: project.extensions.findByType(LibraryExtension::class.java)
?: throw GradleException(
"Android extension is missing. Ensure either the Android application or library plugin is applied.\n" +
"Ensure the Android plugin is applied before configuring Slice Tools.",
)
// Register Slice source set as "slice" extension of each Android source set
androidExtension.sourceSets.configureEach { sourceSet ->
project.logger.lifecycle("Configuring Slice source set for Android source set: ${sourceSet.name}")
val sliceSourceSet = SliceToolsUtil.createSliceSourceSet(extension, sourceSet.name)
// Register it as an extension in the Android source set
(sourceSet as ExtensionAware).extensions.add("slice", sliceSourceSet)
// Create a compile task for this Slice source set
val compileTask = SliceToolsUtil.configureSliceTaskForSourceSet(
project,
toolsPath,
includeSearchPath,
extension,
sliceSourceSet,
compileSlice,
)
// Link the generated output from Slice compilation to the Android Java sources
sourceSet.java.srcDir(compileTask.flatMap { it.output })
}
}
The issue: When I run the build, my compileSlice tasks are not triggered.
The output directory in the task is declared as:
@get:OutputDirectory
abstract val output: DirectoryProperty
And set during initialization:
init {
// Ensure the task always runs, preventing Gradle from marking it as UP-TO-DATE. This allow us to check for
// changes in the Slice files and dependencies.
outputs.upToDateWhen { false }
// Set default output directory to `build/generated/source/slice/<sourceSetName>`
output.convention(
sourceSetName.flatMap { project.layout.buildDirectory.dir("generated/source/slice/$it") },
)
}
Does anyone know what might be preventing the Android source sets from picking up the generated sources?
Any help would be greatly appreciated.
Thanks,
José