-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): 支持AGP 8.0+所需的新版Transform API
GradleTransformWrapper对接新版API实现的是最基本的全量输入和输出功能。 没有实现增量编辑的能力,也没有对齐旧版API中在getSecondaryFiles中将自身代码加入, 以便开发中更新transform代码可触发重新执行transform。 因此使用GradleTransformWrapper开发transform时可能需要手动clean。 ShadowPlugin加入了hasDeprecatedTransformApi检测, 只在判断出AGP主版本号大于等于8时才会应用GradleTransformWrapper。 增加了projects/test/gradle-plugin-agp-compat-test中对AGP 8.0+已知版本的自动化测试。 resolve #1212
- Loading branch information
Showing
6 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ore/transform/src/main/kotlin/com/tencent/shadow/core/transform/GradleTransformWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.tencent.shadow.core.transform | ||
|
||
import com.tencent.shadow.core.transform_kit.ClassTransform | ||
import com.tencent.shadow.core.transform_kit.TransformInput | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.Directory | ||
import org.gradle.api.file.RegularFile | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.tasks.InputFiles | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.BufferedOutputStream | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.util.jar.JarOutputStream | ||
import java.util.zip.ZipEntry | ||
import javax.inject.Inject | ||
|
||
/** | ||
* 适配AGP 7.2引入的新的Transform API,AGP没给这个API特别命名,但我们知道它是Gradle直接提供的Transform | ||
* 接口。与之对应的是DeprecatedTransformWrapper适配旧的Transform API接口。 | ||
*/ | ||
abstract class GradleTransformWrapper @Inject constructor(@Internal val classTransform: ClassTransform) : | ||
DefaultTask() { | ||
// This property will be set to all Jar files available in scope | ||
@get:InputFiles | ||
abstract val allJars: ListProperty<RegularFile> | ||
|
||
// Gradle will set this property with all class directories that available in scope | ||
@get:InputFiles | ||
abstract val allDirectories: ListProperty<Directory> | ||
|
||
// Task will put all classes from directories and jars after optional modification into single jar | ||
@get:OutputFile | ||
abstract val output: RegularFileProperty | ||
|
||
@TaskAction | ||
fun taskAction() { | ||
val inputs: List<TransformInput> = allDirectories.get().map { | ||
TransformInputImpl(it.asFile, TransformInput.Kind.DIRECTORY) | ||
} + allJars.get().map { | ||
TransformInputImpl(it.asFile, TransformInput.Kind.JAR) | ||
} | ||
|
||
classTransform.beforeTransform() | ||
classTransform.input(inputs) | ||
classTransform.onTransform() | ||
output(inputs) | ||
classTransform.afterTransform() | ||
} | ||
|
||
private fun output(inputs: Iterable<TransformInput>) { | ||
val jarOutput = JarOutputStream( | ||
BufferedOutputStream(FileOutputStream(output.get().asFile)) | ||
) | ||
jarOutput.use { | ||
val outputClassNames = inputs.flatMap { | ||
it.inputClassNames | ||
} | ||
|
||
outputClassNames.forEach { className -> | ||
val entryName = className.replace('.', '/') + ".class" | ||
jarOutput.putNextEntry(ZipEntry(entryName)) | ||
classTransform.onOutputClass(className, jarOutput) | ||
} | ||
} | ||
} | ||
|
||
private class TransformInputImpl( | ||
val file: File, | ||
override val kind: Kind | ||
) : TransformInput() { | ||
|
||
override fun asFile(): File = file | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters