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

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.tasks.CustomStringChangeTask
import com.xml.guard.tasks.DeclarationChangeTask
import com.xml.guard.tasks.DeleteTask
import com.xml.guard.tasks.DictionaryTask
import com.xml.guard.tasks.MoveDirTask
import com.xml.guard.tasks.PackageChangeTask
import com.xml.guard.tasks.ResGuardTask
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 3.0.5, 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("packageChange", PackageChangeTask::class, guardExt, variantName)
        createTask("moveDir", MoveDirTask::class, guardExt, variantName)
        createTask("delete", DeleteTask::class, guardExt, variantName)
        createTask("dictionary", DictionaryTask::class, guardExt, variantName)
        createTask("1-customStringChange", CustomStringChangeTask::class, guardExt, variantName)
        createTask("2-declarationChange", DeclarationChangeTask::class, guardExt, variantName)
        createTask("3-resGuard", ResGuardTask::class, guardExt, variantName)
        createTask("4-xmlClassGuard", XmlClassGuardTask::class, guardExt, variantName)
        createTask("5-stringFog", StringFogTask::class, guardExt, variantName)
    }

    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