ArtifactTransformationRequest

public interface ArtifactTransformationRequest<TaskT extends 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 methods

abstract void
submit(
    @NonNull TaskT task,
    @NonNull Function1<@NonNull BuiltArtifact, @NonNull File> transformer
)

Submit a lambda to process each input BuiltArtifact object synchronously.

abstract @NonNull Supplier<@NonNull BuiltArtifacts>
<ParamT extends WorkParameters> submit(
    @NonNull TaskT task,
    @NonNull WorkQueue workQueue,
    @NonNull Class<@NonNull WorkAction<@NonNull ParamT>> actionType,
    @NonNull Function3<@NonNull BuiltArtifact, @NonNull Directory, @NonNull parameters, @NonNull File> parameterConfigurator
)

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

Public methods

submit

abstract void submit(
    @NonNull TaskT task,
    @NonNull Function1<@NonNull BuiltArtifact, @NonNull File> transformer
)

Submit a lambda to process each input BuiltArtifact object synchronously.

submit

abstract @NonNull Supplier<@NonNull BuiltArtifacts> <ParamT extends WorkParameters> submit(
    @NonNull TaskT task,
    @NonNull WorkQueue workQueue,
    @NonNull Class<@NonNull WorkAction<@NonNull ParamT>> actionType,
    @NonNull Function3<@NonNull BuiltArtifact, @NonNull Directory, @NonNull parameters, @NonNull File> parameterConfigurator
)

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

Parameters
@NonNull TaskT task

The Task initiating the WorkQueue requests.

@NonNull WorkQueue workQueue

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

@NonNull Class<@NonNull WorkAction<@NonNull ParamT>> actionType

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

@NonNull Function3<@NonNull BuiltArtifact, @NonNull Directory, @NonNull parameters, @NonNull File> parameterConfigurator

The lambda to configure instances of parameterType for each BuiltArtifact.