TransformationAction not executing

Hello everyone,

I am developing a plugin for android applications in which I was using the old transform API.
Upgrading to AGP7+, I’m porting it to the new Transformation API but the TransformationAction is not running.
To test the plugin, I am compiling it within an Android application project, in the BuildSrc module.
Debugging, the TransformationAction is registering correctly, but the @TaskAction has not been executed
Does anyone have any idea what could be happening?

Thanks in advance

class TestPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        val artifactType = Attribute.of("artifactType", String::class.java)
        project.dependencies.registerTransform(TestTransformAction::class.java) { spec ->
            spec.from.attribute(artifactType, "jar")
            spec.to.attribute(artifactType, "processed-jar")
        }
    }
}
abstract class TestTransformAction : TransformAction<TransformParameters.None> {
    @get:InputArtifact
    @get:PathSensitive(PathSensitivity.RELATIVE)
    abstract val artifact: Provider<FileSystemLocation>


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

The @TaskAction annotation actually is useless.
You are not writing a task, but a transform.
For the transform it is sufficient to overwrite the transform function.
For the attribute and value, I recommend you use the built-in constants org.gradle.api.artifacts.type.ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE and org.gradle.api.artifacts.type.ArtifactTypeDefinition.JAR_TYPE.
But this does not change anything, just a recommendation.

The question is, whether you also request the artifact type to be "processed-jar".
Just registering the transform will not execute it anywhere.
You just say, that if someone requests "processed-jar" but you currently only have "jar", then this transform can provide this transformation.

Hi,

Thanks for the reply. i’ll try to test your suggestions.
But, I don’t understand when you say " someone requests "processed-jar" but you currently only have "jar" , then this transform can provide this transformation."
Can you show me some code so I can understand it?

Thanks again!

val runtimeClasspath by configurations.existing {
    attributes {
        attribute(ARTIFACT_TYPE_ATTRIBUTE, "processed-jar")
    }
}

Thanks again, but after evaluating this approach, I think it is not the best solution.
In old AGP, I used a Transformation to modify (via ASM) some classes. With AGP7+ I wanted to change it to TransformationAction, but I think I’m not in the right way. I think TransformationAction is not for this purpose.
I think is better to use the instrumentation API or ScopedArtifactsOperation toTransform to perform this operation…

Thanks again

I have no idea about Android builds.
But the built-in transform action framework is exactly to transform artifacts.
You can use it to instrument jars, to unpack things, to sign things, to leave out class files from jars, to practically do anything on-the-fly with artifacts.

Whether with AGP there are more appropriate AGP-specific ways, I don’t know.

I’ll give the TransformationAction API another chance, but I can’t understand how it works… maybe that’s the problem! :man_facepalming:
I only need to receive all the classes and libraries of the project to be able to analyze and modify them with ASM

Thanks!