如何在每个版本中随机化ProGuard词典? [英] How to randomize ProGuard dictionaries on each build?

查看:73
本文介绍了如何在每个版本中随机化ProGuard词典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了文章,其中引入了将字典条目随机化的概念在混淆时,不要为类名和变量使用标准的"a,b,c,...".他解释说,可以在构建时在gradle中运行任务以生成随机文本文件,该文件可以替换默认提供的文件:

I found an article which introduces the concept of randomizing the dictionary entries instead of using the standard "a, b, c, ..." for class names and variables when obfuscating. He explains that tasks can be run in gradle on build to generate a randomized text file which can replace the one provided by default:

tasks.whenTaskAdded { currentTask ->
    //Android Gradle plugin may change this task name in the future
    def prefix = 'transformClassesAndResourcesWithProguardFor'
    if (currentTask.name.startsWith(prefix)) {
        def taskName = currentTask.name.replace(prefix,
                       'createProguardDictionariesFor')
        task "$taskName" {
            doLast {
                createRandomizedDictonaries()
            }
        }

        //append scramble task to proguard task
        currentTask.dependsOn "$taskName"
    }
}

但是,我不知道他如何在gradle中运行 createRandomizedDictonaries(),以及他如何创建一个包含随机字母/符号/符号列表的文本文件.逻辑可能类似于:

However, I don't know how he runs createRandomizedDictonaries() in gradle and how he creates a text file which contains a random list of letters/integers/symbols. The logic could be something like:

  • 从gradle的类中运行函数.
  • 读取主文本文件(在应用程序内)中的字符串,并将它们一个接一个地添加到带有循环的字符串Arraylist中吗?潜在地结合一些随机逻辑,仅添加来自文本文件?
  • 如果所有字符串都已添加到Arraylist中,请使用一些随机化逻辑将X个字符串添加到另一个字符串中用于ProGuard混淆的Arraylist.
  • 使用Arraylist中的随机字符串创建一个新的文本文件.
  • 指示ProGuard使用新生成的文本文件进行混淆.

欢迎同时使用kotlin和Java解决方案.

Solutions for both kotlin and java are welcome.

推荐答案

我找到了一个有关使用gradle构建混淆字典的网站. https://yrom.net/blog/2019/06/19/simple-codes-to-generate-obfuscation-dictionary/

I found a web which is talk about use gradle to build a obfuscationdictionary. https://yrom.net/blog/2019/06/19/simple-codes-to-generate-obfuscation-dictionary/

我在gradle任务中运行了代码,但需要进行一些修改.以下是我的gradle代码:

I run the code in my gradle task, but it needs to modify a little bit. The following is my gradle code:

task genDict{
outputs.file('D:/ProjectName/build/tmp/builddict.txt')
doLast{
    def r =new Random()
    println(r)
    def begin = r.nextInt(1000) + 0x0100
    def end = begin + 0x40000
    println("end: "+end)
    def chars = (begin..end)
            .findAll { Character.isValidCodePoint(it) && Character.isJavaIdentifierPart(it) }
            .collect { String.valueOf(Character.toChars(it)) }
    println("chars: "+chars)
    int max = chars.size()
    println(max)
    def start = []
    def dict = []
    for (int i = 0; i < max; i++) {
        char c = chars.get(i).charAt(0)
        if (Character.isJavaIdentifierStart(c)) {
            start << String.valueOf(c)
        }
    }
    println(start.size())
    def f = outputs.files.getSingleFile()
    f.getParentFile().mkdirs()
    f.withWriter("UTF-8") {
        it.write(start.join(System.lineSeparator()))
        it.write()
    }
}

我认为您可以设置字典规则.这是一个例子.不要忘记在您的proguard-rules.pro文件中添加混淆规则

I think you can set your dictionary rule. This is an example. Don't forget to add the obfuscation rule in your proguard-rules.pro file

-混淆词典'D:\ ProjectName \ build \ tmp \ builddict.txt'

-obfuscationdictionary 'D:\ProjectName\build\tmp\builddict.txt'

-classobfuscationdictionary'D:\ ProjectName \ build \ tmp \ builddict.txt'

-classobfuscationdictionary 'D:\ProjectName\build\tmp\builddict.txt'

-packageobfuscationdictionary'D:\ ProjectName \ build \ tmp \ builddict.txt'

-packageobfuscationdictionary 'D:\ProjectName\build\tmp\builddict.txt'

这篇关于如何在每个版本中随机化ProGuard词典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆