使用自定义混淆配置文件启用 ProGuard

ProGuard 允许开发人员对代码进行模糊处理,缩小和优化。

#1 该过程的第一步是在构建上启用 proguard

这可以通过在所需的构建上将’minifyEnabled’命令设置为 true 来完成

#2 第二步是指定我们为给定的构建使用哪些 proguard 文件

这可以通过使用正确的文件名设置’proguardFiles’行来完成 ****

buildTypes {
    debug {
        minifyEnabled false
    }
    testRelease {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules-tests.pro'
    }
    productionRelease {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules-tests.pro', 'proguard-rules-release.pro'
    }
}

#3 开发人员可以根据自己的需要编辑他的 proguard 文件

这可以通过编辑文件(例如’proguard-rules-tests.pro’)并添加所需的约束来完成。以下文件用作示例 proguard 文件

// default & basic optimization configurations
-optimizationpasses 5
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-verbose

-dump obfuscation/class_files.txt
-printseeds obfuscation/seeds.txt
-printusage obfuscation/unused.txt // unused classes that are stripped out in the process
-printmapping obfuscation/mapping.txt // mapping file that shows the obfuscated names of the classes after proguad is applied

// the developer can specify keywords for the obfuscation (I myself use fruits for obfuscation names once in a while :-) )
-obfuscationdictionary obfuscation/keywords.txt
-classobfuscationdictionary obfuscation/keywords.txt
-packageobfuscationdictionary obfuscation/keywords.txt

最后,每当开发人员运行和/或生成新的 .APK 文件时,将应用自定义程序配置,从而满足要求。