Configure multiple TransformActions to process artifact in chain

Basically I have the following code:

class SamplePlugin : Plugin<Project> {
  override fun apply(project: Project) {
    val artifactType = Attribute.of("artifactType", String::class.java)

    project.dependencies.registerTransform(FooTransformAction::class.java) { spec ->
      spec.from.attribute(artifactType, "jar")
      spec.to.attribute(artifactType, "processed-jar")
    }

    project.dependencies.registerTransform(BarTransformAction::class.java) { spec ->
      spec.from.attribute(artifactType, "jar")
      spec.to.attribute(artifactType, "processed-jar")
    }
  }
}

abstract class FooTransformAction : TransformAction<TransformParameters.None> {
  @get:InputArtifact
  @get:PathSensitive(PathSensitivity.RELATIVE)
  abstract val artifact: File

  override fun transform(outputs: TransformOutputs) {
    outputs.file(artifact)
  }
}

abstract class BarTransformAction : TransformAction<TransformParameters.None> {
  @get:InputArtifact
  @get:PathSensitive(PathSensitivity.RELATIVE)
  abstract val artifact: File

  override fun transform(outputs: TransformOutputs) {
    outputs.file(artifact)
  }
}

What I want to achieve is to process some artifact.jar in chain using multiple TransformActions:

  1. FooTransformAction takes artifact.jar as an input and produces artifact-foo.jar as an output.
  2. BarTransformAction takes artifact-foo.jar and produces artifact-foo-bar.jar.
  3. After the chain is finished the final jar should be accessible under processed-jar artifact type.

The problem with the code above is that FooTransformAction isn’t invoked at all and it looks like registerTransform(BarTransformAction::class.java) overrides the previous registerTransform call.

Is is possible to configure FooTransformAction and BarTransformAction so they are executed in a way I described above?

Please note I don’t control FooTransformAction as it’s actually a JetifyTransform from android gradle plugin, so there is no way for me to change how it’s created or configured.

Hi Vladimir,

the attributes in your registration calls are the same, so both transforms would try to transform artifact.jar. You want the second transform to work on the output of the first, so you need to add some to attributes to the first registration. Those attributes also need to be added as from attributes of the second registration.

Your transform does not run, since it only affects the artifactType, and that attribute is not available at resolution. Check out the documentation on artifact transforms to see how you need to register your transforms so they run on resolution.

Cheers,
Stefan

Hi Stefan!

Thanks for the documentation link, it looks like a quite comprehensive example of how to use TransformAction. I’ll definitely check it out.