I am trying to upgrade a Gradle plugin under Gradle 8 and AGP 8. This plugin was originally compiled with AGP 3.5.4, and its registerTransform API can no longer be used in the new environment. While attempting to adapt the plugin for AGP 8, I discovered that the GAV information of a JAR package could be obtained using the following approach in the previous version.
inputs.each {
it.jarInputs.each {
def jarName = it.name
def src = it.getFile()
def dest = outputProvider.getContentLocation(jarName, it.contentTypes, it.scopes, Format.JAR);
//...
}
}
Via def jarName = it.name, the plugin get a String like "com.androidx.constriantlayout:constraintlayout:2.1.4" (just for example of the style). It represent the JAR's GAV info.
However, in the AGP 8 environment, this method can no longer be used to obtain the GAV information of the JAR package. The 'input' no longer has a 'name' attribute. This is due to the deprecation of the entire transform API, which has been replaced by the 'toTransform' API of the 'androidExtensionComponent'. The new 'toTransform' API does not provide a way to obtain the GAV information from the input JAR package.
Someone suggested that I use TransformAction during the dependency resolution phase in Gradle to save the GAV information of the JAR packages in the artifacts, and write it to a file for easy querying by subsequent plugins.
Does anyone met a problem like this? Plz help me, thanks a lot!
I indeed followed this approach. After reading the official documentation , gradle official site's doc, I added the following two core pieces of code in the build.gradle(:app) file.
abstract class VersionInfoEncompassTransformAction implements TransformAction<TransformParameters.None> {
@InputArtifact
abstract Provider<FileSystemLocation> getInputArtifact()
@Override
void transform(TransformOutputs outputs) {
def input = inputArtifact.get().asFile
// todo nameWithoutExtension extension属性缺失
def output = outputs.file(input.nameWithoutExtension + " -versioned" + input.extension)
addVersionInformation(input, output)
}
private void addVersionInformation(File input, File output) {
println("addVersionInfomation:" + output.absoluteFile)
Files.copy(input, output)
def jarFileOs = new JarOutputStream(new FileOutputStream(output))
def versionFile = new File("version-info.prop")
writeFirstTestLine(versionFile)
def versionEntry = new JarEntry(versionFile.name)
versionFile.withInputStream {
BufferedInputStream bufferedInputStream = new BufferedInputStream(it)
try {
jarFileOs.putNextEntry(versionEntry)
byte[] buffer = new byte[1024]
int length
while ((length = bufferedInputStream.read(buffer)) != -1) {
jarFileOs.write(buffer, 0, length);
}
jarFileOs.closeEntry()
} catch (IOException e) {
e.printStackTrace()
} finally {
bufferedInputStream.close()
jarFileOs.close()
}
}
}
private void writeFirstTestLine(File outputFile) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))
writer.write("1.0")
writer.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
dependencies {
def artifactType = Attribute.of('artifactType', String)
registerTransform(VersionInfoEncompassTransformAction.class) {
from.attribute(artifactType, "jar")
to.attribute(artifactType, "jar")
}
}
However, in practice, when I tried running the build command, the script with the above code didn't seem to execute the transform task. My command is:
./gradlew assembleDebug --refresh-dependencies
and in the output I cannot find my log from my cod here println("addVersionInfomation:" + output.absoluteFile)
I don't know if there is anything wrong. The attributes filter? or anything else? If you need my source code of plugin or the demo application, plz tell me.