缩小 android 应用但不混淆它
Minify android app but do not obfuscate it
当我不缩小我的应用程序时,我达到了最大方法数并且构建 dex 文件失败。这可以通过在 build.gradle
中启用 minify
来避免。然而,缺点是现在代码变得模糊了。这对于 Release 版本没问题,但对于 Debug 版本会有问题。
有没有办法告诉 gradle 缩小调试版本但不混淆它?
是的,您可以使用 ProGuard 来缩小调试版本。
关键是在 ProGuard 配置中使用 -dontobfuscate
选项进行调试构建。
在 build.gradle
中使用此设置:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro',
'proguard-rules-debug.pro'
}
}
将您的发布 ProGuard 配置写入 proguard-rules.pro
。
发布和调试使用相同的配置。这样您就可以确保没有必要的代码被删除。并且调试缩小不会破坏构建。
为调试构建添加额外的 ProGuard 配置文件 proguard-rules-debug.pro
。它应该包含仅用于调试的规则。在这种情况下仅添加:
-dontobfuscate
一个简单的解决方案是在构建配置中添加 minifyEnabled true
和 useProguard false
。但是不建议对来自官方文档的 debug 构建进行代码压缩 请注意,代码压缩会减慢构建时间,因此您应该尽可能避免在调试构建中使用它.
参考https://developer.android.com/studio/build/shrink-code.html
Tomik 的回答在技术上是正确的,但不支持使用 Instant Run for your builds. As pointed out in the official guide on code-shrinking:
Enable code shrinking with Instant Run If code shrinking is important
to you while incrementally building your app, try the experimental
code shrinker that's built into the Android plugin for Gradle. This
shrinker supports Instant Run, unlike ProGuard.
You can configure the Android plugin shrinker using the same
configuration files as ProGuard. However, the Android plugin shrinker
does not obfuscate or optimize your code—it only removes unused code.
So you should use it for your debug builds only, and enable ProGuard
for your release builds so your release APK's code is obfuscated and
optimized.
因此正确的解决方案是像这样设置调试版本:
android {
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
这样,调试版本中的代码不会得到优化或混淆,但会缩小。这也适用于使用 Instant Run.
时
minifyEnabled true
只是一个快捷方式:
postprocessing {
removeUnusedCode true
obfuscate true
optimizeCode true
}
因此,如果您想缩小而不混淆,请将 minifyEnabled true
替换为:
postprocessing {
removeUnusedCode true
obfuscate false // <--
optimizeCode true
}
此外,如果您有 shrinkResources true
,编译器会报错。等效的后处理字段是removeUnusedResources true
,即:
postprocessing {
removeUnusedCode true
removeUnusedResources true // <--
obfuscate false
optimizeCode true
}
与其他答案相反,useProguard false
不禁用混淆;它将混淆引擎从 ProGuard 更改为 R8。
当我不缩小我的应用程序时,我达到了最大方法数并且构建 dex 文件失败。这可以通过在 build.gradle
中启用 minify
来避免。然而,缺点是现在代码变得模糊了。这对于 Release 版本没问题,但对于 Debug 版本会有问题。
有没有办法告诉 gradle 缩小调试版本但不混淆它?
是的,您可以使用 ProGuard 来缩小调试版本。
关键是在 ProGuard 配置中使用 -dontobfuscate
选项进行调试构建。
在 build.gradle
中使用此设置:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro',
'proguard-rules-debug.pro'
}
}
将您的发布 ProGuard 配置写入 proguard-rules.pro
。
发布和调试使用相同的配置。这样您就可以确保没有必要的代码被删除。并且调试缩小不会破坏构建。
为调试构建添加额外的 ProGuard 配置文件 proguard-rules-debug.pro
。它应该包含仅用于调试的规则。在这种情况下仅添加:
-dontobfuscate
一个简单的解决方案是在构建配置中添加 minifyEnabled true
和 useProguard false
。但是不建议对来自官方文档的 debug 构建进行代码压缩 请注意,代码压缩会减慢构建时间,因此您应该尽可能避免在调试构建中使用它.
参考https://developer.android.com/studio/build/shrink-code.html
Tomik 的回答在技术上是正确的,但不支持使用 Instant Run for your builds. As pointed out in the official guide on code-shrinking:
Enable code shrinking with Instant Run If code shrinking is important to you while incrementally building your app, try the experimental code shrinker that's built into the Android plugin for Gradle. This shrinker supports Instant Run, unlike ProGuard.
You can configure the Android plugin shrinker using the same configuration files as ProGuard. However, the Android plugin shrinker does not obfuscate or optimize your code—it only removes unused code. So you should use it for your debug builds only, and enable ProGuard for your release builds so your release APK's code is obfuscated and optimized.
因此正确的解决方案是像这样设置调试版本:
android {
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
这样,调试版本中的代码不会得到优化或混淆,但会缩小。这也适用于使用 Instant Run.
时minifyEnabled true
只是一个快捷方式:
postprocessing {
removeUnusedCode true
obfuscate true
optimizeCode true
}
因此,如果您想缩小而不混淆,请将 minifyEnabled true
替换为:
postprocessing {
removeUnusedCode true
obfuscate false // <--
optimizeCode true
}
此外,如果您有 shrinkResources true
,编译器会报错。等效的后处理字段是removeUnusedResources true
,即:
postprocessing {
removeUnusedCode true
removeUnusedResources true // <--
obfuscate false
optimizeCode true
}
与其他答案相反,useProguard false
不禁用混淆;它将混淆引擎从 ProGuard 更改为 R8。