ArtifactTransformationRequest

Added in 4.2.0

interface ArtifactTransformationRequest<TaskT : Task>


When a Directory contains more than one artifact (for example, consider SingleArtifact.APK with multiple APKs for different screen densities), this object will abstract away having to deal with BuiltArtifacts and manually load and write the metadata files.

Instead, users can focus on writing transformation code that transform one file at at time into a newer version. It will also provide the ability to use Gradle's WorkQueue to multi-thread the transformations.

Here is an example of a Task copying (unchanged) the APK files from one location to another.

// parameter interface to pass to work items.
interface WorkItemParameters: WorkParameters, Serializable {
val inputApkFile: RegularFileProperty
val outputApkFile: RegularFileProperty
}
// work item that copies one file at a time.
abstract class WorkItem @Inject constructor(
private val workItemParameters: WorkItemParameters
): WorkAction<WorkItemParameters> {

override fun execute() {
workItemParameters.inputApkFile.asFile.get().copyTo(
workItemParameters.outputApkFile.get().asFile)
}
}

And the task that wires things together:

abstract class CopyApksTask @Inject constructor(private val workers: WorkerExecutor): DefaultTask() {

@get:InputFiles
abstract val apkFolder: DirectoryProperty

@get:OutputDirectory
abstract val outFolder: DirectoryProperty

@get:Internal
abstract val transformationRequest: Property<ArtifactTransformationRequest<CopyApksTask>>

@TaskAction
fun taskAction() {
transformationRequest.get().submit(
this,
workers.noIsolation(),
WorkItem::class.java) {
builtArtifact: BuiltArtifact,
outputLocation: Directory,
param: WorkItemParameters ->
val inputFile = File(builtArtifact.outputFile)
param.inputApkFile.set(inputFile)
param.outputApkFile.set(File(outputLocation.asFile, inputFile.name))
param.outputApkFile.get().asFile
}
}
}

Summary

Public functions

Unit
submit(task: TaskT, transformer: (input: BuiltArtifact) -> File)

Submit a lambda to process each input BuiltArtifact object synchronously.

Supplier<BuiltArtifacts>
<ParamT : WorkParameters> submit(
    task: TaskT,
    workQueue: WorkQueue,
    actionType: Class<WorkAction<ParamT>>,
    parameterConfigurator: (builtArtifact: BuiltArtifact, outputLocation: Directory, parameters) -> File
)

Submit a org.gradle.workers style of WorkAction to process each input BuiltArtifact.

Public functions

submit

fun submit(task: TaskT, transformer: (input: BuiltArtifact) -> File): Unit

Submit a lambda to process each input BuiltArtifact object synchronously.

submit

fun <ParamT : WorkParameters> submit(
    task: TaskT,
    workQueue: WorkQueue,
    actionType: Class<WorkAction<ParamT>>,
    parameterConfigurator: (builtArtifact: BuiltArtifact, outputLocation: Directory, parameters) -> File
): Supplier<BuiltArtifacts>

Submit a org.gradle.workers style of WorkAction to process each input BuiltArtifact.

Parameters
task: TaskT

The Task initiating the WorkQueue requests.

workQueue: WorkQueue

The Gradle WorkQueue instance to use to spawn worker items with.

actionType: Class<WorkAction<ParamT>>

The type of the WorkAction subclass that process that input BuiltArtifact.

parameterConfigurator: (builtArtifact: BuiltArtifact, outputLocation: Directory, parameters) -> File

The lambda to configure instances of parameterType for each BuiltArtifact.