All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.xml.guard.AndroidGuardPlugin.kt Maven / Gradle / Ivy

@file:Suppress("DEPRECATION")

package com.xml.guard

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.xml.guard.entensions.GuardExtension
import com.xml.guard.model.aabResGuard
import com.xml.guard.model.andResGuard
import com.xml.guard.tasks.CustomStringChangeTask
import com.xml.guard.tasks.DeleteTask
import com.xml.guard.tasks.DictionaryTask
import com.xml.guard.tasks.FindConstraintReferencedIdsTask
import com.xml.guard.tasks.MoveDirTask
import com.xml.guard.tasks.PackageChangeTask
import com.xml.guard.tasks.ResGuardTask
import com.xml.guard.tasks.DeclarationChangeTask
import com.xml.guard.tasks.StringFogTask
import com.xml.guard.tasks.XmlClassGuardTask
import com.xml.guard.utils.AgpVersion
import com.xml.guard.utils.uppercaseFirstChar
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import kotlin.reflect.KClass

/**
 * User: ljx
 * Date: 2022/2/25
 * Time: 19:03
 */
class AndroidGuardPlugin : Plugin {

    override fun apply(project: Project) {
        checkApplicationPlugin(project)
        println("AndroidGuard version is 2.5.2, agpVersion=${AgpVersion.agpVersion}")

        val guardExt = project.extensions.create("androidGuard", GuardExtension::class.java)
        val android = project.extensions.getByName("android") as AppExtension
        project.afterEvaluate {
            android.applicationVariants.all { variant ->
                it.createTasks(guardExt, variant)
            }
        }
    }

    private fun Project.createTasks(guardExt: GuardExtension, variant: ApplicationVariant) {
        val variantName = variant.name.uppercaseFirstChar()
        createTask("xmlClassGuard$variantName", XmlClassGuardTask::class, guardExt, variantName)
        createTask("packageChange$variantName", PackageChangeTask::class, guardExt, variantName)
        createTask("moveDir$variantName", MoveDirTask::class, guardExt, variantName)
        createTask("resGuard$variantName", ResGuardTask::class, guardExt, variantName)
        createTask("delete$variantName", DeleteTask::class, guardExt, variantName)
        createTask("dictionary$variantName", DictionaryTask::class, guardExt, variantName)
        createTask("stringFog$variantName", StringFogTask::class, guardExt, variantName)
        createTask(
            "declarationChange$variantName",
            DeclarationChangeTask::class,
            guardExt,
            variantName
        )
        createTask(
            "customStringChange$variantName",
            CustomStringChangeTask::class,
            guardExt,
            variantName
        )

        if (guardExt.findAndConstraintReferencedIds) {
            createAndFindConstraintReferencedIdsTask(variantName)
        }
        if (guardExt.findAabConstraintReferencedIds) {
            createAabFindConstraintReferencedIdsTask(variantName)
        }
    }

    private fun Project.createAndFindConstraintReferencedIdsTask(variantName: String) {
        val andResGuardTaskName = "resguard$variantName"
        val andResGuardTask = project.tasks.findByName(andResGuardTaskName)
            ?: throw GradleException("AndResGuard plugin required")
        val taskName = "andFindConstraintReferencedIds$variantName"
        val task =
            createTask(taskName, FindConstraintReferencedIdsTask::class, andResGuard, variantName)
        andResGuardTask.dependsOn(task)
    }

    private fun Project.createAabFindConstraintReferencedIdsTask(variantName: String) {
        val aabResGuardTaskName = "aabresguard$variantName"
        val aabResGuardTask = project.tasks.findByName(aabResGuardTaskName)
            ?: throw GradleException("AabResGuard plugin required")
        val taskName = "aabFindConstraintReferencedIds$variantName"
        val task =
            createTask(taskName, FindConstraintReferencedIdsTask::class, aabResGuard, variantName)
        aabResGuardTask.dependsOn(task)
    }

    private fun checkApplicationPlugin(project: Project) {
        if (!project.plugins.hasPlugin("com.android.application")) {
            throw GradleException("Android Application plugin required")
        }
    }

    private fun  Project.createTask(
        taskName: String,
        taskClass: KClass,
        vararg params: Any
    ): Task = tasks.findByName(taskName) ?: tasks.create(taskName, taskClass.java, *params)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy