com.xml.guard.tasks.DictionaryTask.kt Maven / Gradle / Ivy
package com.xml.guard.tasks
import com.xml.guard.entensions.GuardExtension
import com.xml.guard.utils.getRandomString
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File
import javax.inject.Inject
import kotlin.random.Random
@Suppress("unused")
open class DictionaryTask @Inject constructor(
private val guardExtension: GuardExtension,
private val variantName: String,
) : DefaultTask() {
init {
group = "guard"
}
private val dictionaryFile1 by lazy { project.file("proguard_dictionary1.txt") }
private val dictionaryFile2 by lazy { project.file("proguard_dictionary2.txt") }
private val dictionaryFile3 by lazy { project.file("proguard_dictionary3.txt") }
@TaskAction
fun execute() {
println()
println("Obfuscated dictionary generation --- start")
startTask(dictionaryFile1, isObfuscatePackage = false)
startTask(dictionaryFile2, isObfuscatePackage = false)
startTask(dictionaryFile3, isObfuscatePackage = true)
println()
println("Configure the following obfuscation rules:")
println("# ============================= Proguard Dictionary =============================")
println("-obfuscationdictionary ${dictionaryFile1.name}")
println("-classobfuscationdictionary ${dictionaryFile2.name}")
println("-packageobfuscationdictionary ${dictionaryFile3.name}")
println("# ============================= Proguard Dictionary =============================")
println()
println("Obfuscated dictionary generation --- end")
}
private fun startTask(dictionaryFile: File, isObfuscatePackage: Boolean) {
val dictionaries = mutableListOf()
val dictionaryLines = Random.nextInt(100000, 200000)
for (i in 0 until dictionaryLines) {
val dictionary = getRandomString(onlyLetter = isObfuscatePackage) {
if (isObfuscatePackage) it.lowercase() else it
}
dictionaries.add(dictionary)
}
val text = dictionaries.distinct().joinToString("\n")
dictionaryFile.writeText(text)
}
}