Proguard 忽略库的配置文件 [英] Proguard ignores config file of library
问题描述
我好几天都面临着一个非常奇怪的问题......
I'm facing a really weird problem for days now...
我有一个包含两个模块的 Gradle 应用,一个主模块和一个库模块.
I have a Gradle app with two modules, one main module and one library module.
- 在两个模块中,我都声明了一个正确的 proguard-rules.pro 文件路径
- 在两个 .gradle 文件中,我都有 minifyEnabled true
这是第一个问题:即使库中的 minifyEnabled 为 false,它也会被混淆.它根本没有影响,似乎取决于主模块的设置.我使用 proguard 有一段时间了,我从来没有遇到过这样的行为.
here is the first problem: even if minifyEnabled is false in the library, it gets obfuscated. it has no effect at all and seems to depend on the main module's settings. I´m using proguard since a while now and I've never experienced such a behavior at all.
此外,库的 proguard-rules.pro 似乎完全被忽略了.我在那里声明什么并不重要,它没有被使用并且结果总是相同的(总是反编译以查看结果).它被默认设置混淆了.
Also, the proguard-rules.pro of the library seems to be completely ignored. It doesn't matter what I declare there, it is not used and the result is always the same (always decompiled to view result). It's obfuscated with the default settings.
我使用了无效的 proguard 文件名来查看该文件是否被触及,并且确实存在名称错误的错误,并且它还抱怨 proguard 文件中的语法错误...
I've used an invalid proguard file name to see if the file is even touched, and indeed there are errors with the wrong name and it also complaines about syntax errors in the proguard file...
我不知道它是否与 Android Studio 的更新有某种关系,因为它还建议我使用minifyEnabled"而不是runProguard".
I don't know if it is somehow related to an update of Android Studio, as it has also recommended me to use "minifyEnabled" instead of "runProguard".
如何管理 proguard 以使用库的 proguard-rules.pro?
How can I manage proguard to use the proguard-rules.pro of the library too?
我制作了一个示例项目来阐明我的问题
I've made an sample project to clarify my problem
我的库的 proguard 配置
The proguard config of my library
我的图书馆的gradle:
The gradle of my library:
最后是我总是得到的结果.我在 proguard 配置中排除/包含的内容无关紧要
And finally the result I always get. It doesn't matter what I exclude/include in the proguard config
如您所见,proguard 规则在主模块上运行良好.它做它应该做的.但它总是将我的图书馆完全混淆为 a.a....
As you can see, the proguard rules work quite well on the main module. It does what it should. But it always fully obfuscates my library to a.a....
它也完全删除了图书馆的Activity,这根本不应该发生
It also has completely deleted the Activity of the Library, which shouldn't happen at all
推荐答案
您通常不应在库项目中启用 ProGuard.ProGuard 在应用项目中将应用和库一起处理,这是最有效的方法.
You typically should not enable ProGuard in the library project. ProGuard processes the application and the library together in the application project, which is the most effective approach.
在库项目中,您可以在 build.gradle 中指定任何特定于库的 ProGuard 配置,例如:
In the library project, you can specify any library-specific ProGuard configuration in build.gradle, e.g.:
defaultConfig {
consumerProguardFiles 'proguard-rules.txt'
}
这个文件然后在库aar中打包为proguard.txt
并自动应用到应用项目中.
This file is then packaged in the library aar as proguard.txt
and automatically applied in the application project.
如果您确实在库项目中启用 ProGuard(可能是因为您想分发库),那么您还必须添加适当的配置来处理库.Android Gradle 构建似乎不会自动执行此操作.您可以:
If you do enable ProGuard in a library project (maybe because you want to distribute the library), then you also have to add the proper configuration for processing the library. The Android Gradle build doesn't seem to do this automatically. You can:
- 将
android-sdk/tools/proguard/examples/library.pro
复制到您的库项目中的proguard-project.txt
. - 删除示例输入/输出行
-injars
、-outjars
、-libraryjars
、-printmapping
文件.Gradle 构建过程会自动提供这些选项. - 从库项目的 build.gradle 中引用配置.
- Copy
android-sdk/tools/proguard/examples/library.pro
toproguard-project.txt
in your library project. - Remove the sample input/output lines
-injars
,-outjars
,-libraryjars
,-printmapping
from the file. The Gradle build process automatically provides these options. - Reference the configuration from the build.gradle of the library project.
为库项目和应用程序项目独立启用/禁用 ProGuard 对我来说很好.
Enabling/disabling ProGuard independently for the library project and for the application project works fine for me.
这篇关于Proguard 忽略库的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!