使用自定義混淆配置檔案啟用 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 檔案時,將應用自定義程式配置,從而滿足要求。