如何准备混淆文件以及其中包含的内容?

How to prepare a proguard file and whats included in it?

我正在 Android Studio 中开发一个应用程序,我想将混淆器添加到我的应用程序中。但我不知道该怎么办?我也想了解它的背景。谁能告诉我一些东西?谢谢。

在您的 gradle 文件中将 true 设置为 minifyEnabled

您可以定义是否在调试、发布或两者中启用 proguard

buildTypes {
        release {
            minifyEnabled true
            proguardFiles 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            proguardFiles 'proguard-rules.pro'
        }
    }

你也可以设置 proguardFiles 来配置他,检查这个 site 来查看关于它的文档,看这个例子:

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/balysv/Documents/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

-optimizationpasses 5
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose

如果您想使用自定义词典进行代码混淆,请使用您的词典文件设置此配置:

-obfuscationdictionary proguard-dic.txt
-classobfuscationdictionary proguard-dic.txt
-packageobfuscationdictionary proguard-dic.txt

字典文件是一个简单的文本文件,其中包含要用于混淆代码的标签,每行 1 个标签。

为了使您的 APK 文件尽可能小,您应该启用收缩以删除发布版本中未使用的代码和资源。

代码压缩可用于 ProGuard,它可以检测并删除打包应用程序中未使用的 类、字段、方法和属性,包括包含的代码库中的那些(使其成为一个有价值的工具用于解决 64k 参考限制)。

ProGuard 还优化了字节码,删除了未使用的代码指令,并使用短名称混淆了剩余的 类、字段和方法。混淆代码使您的 APK 难以进行逆向工程,这在您的应用使用安全敏感功能(例如许可验证)时尤为重要。

例如,build.gradle 文件中的以下代码片段启用了发布版本的代码缩减:

android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile(‘proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

Example of use from Proguard, from Android Studio