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

com.xml.guard.tasks.DeleteTask.kt Maven / Gradle / Ivy

package com.xml.guard.tasks

import com.xml.guard.entensions.GuardExtension
import com.xml.guard.model.MappingParser
import com.xml.guard.utils.*
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File
import javax.inject.Inject

open class DeleteTask @Inject constructor(
    @Suppress("unused")
    private val guardExtension: GuardExtension,
    private val variantName: String,
) : DefaultTask() {

    init {
        group = "guard"
    }

    @TaskAction
    fun execute() {
        // 1、添加所需要删除的混淆类文件
        val obfuscateDirNames = mutableListOf()
        val obfuscateClassFiles = mutableListOf()
        MappingParser.parse(project.file(MappingParser.MAPPING_XML_CLASS)) { _, mappingType, key, value ->
            val rawName = key.trim()
            val rawValue = value.trim()

            when (mappingType) {
                MappingParser.DIR_MAPPING -> {
                    val rawDirs = mutableListOf()
                    rawName.split(".").forEach {
                        if (!rawDirs.contains(it)) {
                            rawDirs.add(it)
                        }
                    }
                    rawValue.split(".").forEach {
                        if (!rawDirs.contains(it) && !obfuscateDirNames.contains(it)) {
                            obfuscateDirNames.add(it)
                        }
                    }
                }

                MappingParser.CLASS_MAPPING -> {
                    obfuscateClassFiles.add(rawValue)
                }
            }
        }

        // 2、添加所需要删除的混淆资源文件
        val resFiles = mutableListOf()
        MappingParser.parse(project.file(MappingParser.MAPPING_RES)) { _, mappingType, _, value ->
            val rawValue = value.trim()
            when (mappingType) {
                MappingParser.RES_MAPPING -> resFiles.add(rawValue)
            }
        }

        val androidProjects = allDependencyAndroidProjects(project)
        androidProjects.forEach { project ->
            // 3、删除混淆的类文件及文件夹
            if (obfuscateClassFiles.isNotEmpty()) {
                val classDirs = mutableListOf()
                classDirs.addAll(project.aidlDirs(variantName))
                classDirs.addAll(project.javaDirs(variantName))

                project.files(classDirs).asFileTree.forEach { file ->
                    val obfuscateName =
                        getClassPackage(project, file) + "." + file.nameWithoutExtension
                    if (obfuscateClassFiles.any { it == obfuscateName }) {
                        file.delete()
                    }
                }
                classDirs.forEach { file ->
                    deleteEmptyDirectories(file) { directory ->
                        obfuscateDirNames.any { it == directory.name }
                    }
                }
            }

            // 4、删除混淆的资源文件
            if (resFiles.isNotEmpty()) {
                val resDirs = project.resDirs(variantName)
                project.files(resDirs).asFileTree.forEach { file ->
                    if (resFiles.any { it == file.nameWithoutExtension }) {
                        file.delete()
                    }
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy