Thanks to visit codestin.com
Credit goes to github.com

Skip to content

🔥🔥🔥对于使用 AGP8 的 toTransform 的api的各位小伙伴来说都应该接入本框架,或遵循本框架的设计,这将使打包速度显著加快

License

Notifications You must be signed in to change notification settings

FlyJingFish/FastTransform

Repository files navigation

English | 简体中文

Build Downloads Python Package Index Docker Pulls Sponsors

Brief description

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

Version restrictions

The version requires AGP 7.6 or above

Usage steps

Can you give the project a Star before starting? Thank you very much, your support is my only motivation. Stars and Issues are welcome!

1. Introduce in the class library of your plugin library

dependencies {
    implementation 'io.github.flyjingfish:fasttransform:1.0.8'
}

2. Use this library

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
    }

}

Directly accelerate existing projects

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

Method 1

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"

Method 2

In build.gradle in the app module Dependency plugins in

//Required items 👇 
plugins { 
    ... 
    id "io.github.flyjingfish.fasttransform" version "1.0.8" 

Finally, I recommend some other libraries I wrote

About

🔥🔥🔥对于使用 AGP8 的 toTransform 的api的各位小伙伴来说都应该接入本框架,或遵循本框架的设计,这将使打包速度显著加快

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •