ArtifactTransformationRequest

interface ArtifactTransformationRequest<TaskT : Task>
com.android.build.api.artifact.ArtifactTransformationRequest

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 Supplier<BuiltArtifacts>
submit(task: TaskT, workQueue: WorkQueue, actionType: Class<out WorkAction<ParamT>>, parameterConfigurator: (builtArtifact: BuiltArtifact, outputLocation: Directory, parameters: ParamT) -> File)

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

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

Submit a lambda to process each input BuiltArtifact object synchronously.

Public methods

submit

abstract fun <ParamT : WorkParameters> submit(
    task: TaskT,
    workQueue: WorkQueue,
    actionType: Class<out WorkAction<ParamT>>,
    parameterConfigurator: (builtArtifact: BuiltArtifact, outputLocation: Directory, parameters: ParamT) -> 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<out WorkAction<ParamT>> The type of the WorkAction subclass that process that input BuiltArtifact.
parameterConfigurator: (builtArtifact: BuiltArtifact, outputLocation: Directory, parameters: ParamT) -> File The lambda to configure instances of parameterType for each BuiltArtifact.

submit

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

Submit a lambda to process each input BuiltArtifact object synchronously.