-
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所需的新版Transform API
test: 测试AGP 8.0到及更新版本
- Loading branch information
Showing
7 changed files
with
161 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
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
86 changes: 86 additions & 0 deletions
86
...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,86 @@ | ||
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.getInputClass() | ||
}.flatMap { | ||
it.getOutputClassNames() | ||
} | ||
|
||
outputClassNames.forEach { className -> | ||
val entryName = className.replace('.', '/') + ".class" | ||
jarOutput.putNextEntry(ZipEntry(entryName)) | ||
classTransform.onOutputClass(entryName, className, jarOutput) | ||
} | ||
} | ||
} | ||
|
||
private class TransformInputImpl( | ||
val file: File, | ||
override val kind: Kind | ||
) : TransformInput() { | ||
|
||
override fun asFile(): File = file | ||
|
||
override fun toOutput(): File { | ||
return file//fixme 不应该要求TransformInput实现toOutput方法 | ||
} | ||
|
||
} | ||
} | ||
|
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