TransformAs an Android Gradle plugin, this API is used by developers to insert files such as class bytecode, resources and so on during apK construction.

For some performance testing plug-ins, if you change the bytecode every time you compile, it may slow down the compile time. We can turn on and off whether to perform staking depending on the compile environment.

Method 1: BuildType name with release does not register Transform

class DoubleClickPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        val flavorBuildType = getFlavorBuildType(project)
        if(! flavorBuildType.toLowerCase(Locale.ROOT).contains("release")) {return
        }
        val config = DoubleClickConfig()
        val appExtension: AppExtension = project.extensions.getByType(AppExtension::class.java)
        appExtension.registerTransform(DoubleClickTransform(config))
    }

    // Get FlavorBuildType from startParameter
    fun getFlavorBuildType(appProject: Project): String {
        var flavorBuildType = ""
        val arg = appProject.gradle.startParameter.taskRequests.getOrNull(0)? .args? .getOrNull(0)
        if(! arg.isNullOrEmpty()) {var index = arg.indexOf("assemble")
            index = if (index > -1) index + "assemble".length else 0
            flavorBuildType = arg.substring(index, arg.length)
        }
        if (flavorBuildType.isNotEmpty()) {
            flavorBuildType = flavorBuildType.substring(0.1).toLowerCase(Locale.ROOT) + flavorBuildType.substring(1)}return flavorBuildType
    }
}
Copy the code

Method 2: Rewrite the applyToVariant method of Transform

class TestTransform : Transform(){override fun applyToVariant(variant: VariantInfo): Transform(){override fun applyToVariant(variant: VariantInfo) Boolean { return variant.buildTypeName.contains("release") } }Copy the code

Reference article:

Toutiao Android ‘second’ level compilation speed optimization

Android Black tech | Gradle Plugin usage scenarios

RocketXPlugin