English | 简体中文
For all friends who use AGP8's toTransform API, they should connect to this framework or follow the design of this framework, which will significantly speed up the packaging speed
The version requires AGP 7.6 or above
Can you give the project a Star before starting? Thank you very much, your support is my only motivation. Stars and Issues are welcome!
dependencies {
implementation 'io.github.flyjingfish:fasttransform:1.0.8'
}class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
androidComponents.onVariants { variant ->
val task = project.tasks.register("${variant.name}XXX", MyClassesTask::class.java)
/*
variant.artifacts
.forScope(ScopedArtifacts.Scope.ALL)
.use(taskProvider)
.toTransform(
ScopedArtifact.CLASSES,
MyTask::allJars,
MyTask::allDirectories,
MyTask::outputFile
)
*/
variant.toTransformAll(task) //Equivalent to the above
// variant.toTransformAll(task,false) Passing false as the second parameter means using the original unaccelerated logic
}
}
}
//Inheriting DefaultTransformTask
abstract class MyClassesTask : DefaultTransformTask() {
// Equivalent to the method annotated by @TaskAction before
override fun startTask() {
/**
* singleClassesJar() Whether there is only one jar package, returning true means that there was a plug-in using toTransform before, and it did not use this plug-in or did not follow this design
*/
/**
* isFastDex is the second parameter passed in when calling toTransformAll
*/
allDirectories().forEach { directory ->
directory.walk().forEach { file ->
if (file.isFile) {
val relativePath = file.getRelativePath(directory)
val jarEntryName: String = relativePath.toClassPath()
FileInputStream(file).use { inputs ->
val cr = ClassReader(inputs)
val cw = ClassWriter(cr, 0)
cr.accept(
MyClassVisitor(cw),
ClassReader.EXPAND_FRAMES
)
cw.toByteArray().inputStream().use {
//write jar
directory.saveJarEntry(jarEntryName, it)
}
}
}
}
}
allJars().forEach { file ->
if (file.absolutePath in ignoreJar) {
return@forEach
}
val jarFile = JarFile(file)
val enumeration = jarFile.entries()
while (enumeration.hasMoreElements()) {
val jarEntry = enumeration.nextElement()
val entryName = jarEntry.name
if (jarEntry.isDirectory || entryName.isEmpty() || entryName.startsWith("META-INF/") || "module-info.class" == entryName || !entryName.endsWith(
".class"
)
) {
continue
}
jarFile.getInputStream(jarEntry).use { inputs ->
val cr = ClassReader(inputs)
val cw = ClassWriter(cr, 0)
cr.accept(
MyClassVisitor(cw),
ClassReader.EXPAND_FRAMES
)
cw.toByteArray().inputStream().use {
//write jar
file.saveJarEntry(jarEntryName, it)
}
}
} jarFile . close ()
}
}
override fun endTask() {
//Write some final work here
}
}
If your existing project has a plugin that uses toTransform, and it does not use this framework or follow the design of this framework, you can choose one of the following methods to accelerate your project
Depend on the plugin in build.gradle in the project root directory
-
New version
plugins { //Required items 👇 Note that the apply setting must be true id "io.github.flyjingfish.fasttransform" version "1.0.8" apply true }
-
Or old version
buildscript { dependencies { //Required items 👇 classpath 'io.github.flyjingfish:fasttransform:1.0.8' } } apply plugin: "fast.dex"
In build.gradle in the app module Dependency plugins in
//Required items 👇
plugins {
...
id "io.github.flyjingfish.fasttransform" version "1.0.8"