【发布时间】:2016-11-07 21:49:50
【问题描述】:
我正在 Android Studio 中开发一个应用程序,我想将 proguard 添加到我的应用程序中。但我不知道该怎么办?我也想了解它的背景。谁能给我看点东西?谢谢。
【问题讨论】:
标签: android gradle functional-programming android-gradle-plugin android-proguard
我正在 Android Studio 中开发一个应用程序,我想将 proguard 添加到我的应用程序中。但我不知道该怎么办?我也想了解它的背景。谁能给我看点东西?谢谢。
【问题讨论】:
标签: android gradle functional-programming android-gradle-plugin android-proguard
在你的 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 文件中的以下 sn-p 为发布构建启用代码收缩:
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(‘proguard-android.txt'),
'proguard-rules.pro'
}
}
...
}
【讨论】: