Gradle - Proguard 配置是否继承?

Gradle - Are proguard configurations inherited?

这可能过于宽泛,但我想解释一下 Proguard 和缩小配置如何在项目及其依赖项之间传递,以了解这些操作在我项目的依赖项树中的深度。

我有 build.gradle 的 `themodule':

buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        }
dependencies {
    compile project(':someothermodule')
    compile 'some.maven.central.library'
}

通过配置,themodule 中的 类 似乎很明显将被缩小和混淆,但是 someothermodule 的 类 会发生什么?它们也会被缩小和混淆吗?即使 someothermoduleminifyEnabled true?

如果 'someothermodule' 只是一个 .jar 依赖会怎样?

some.maven.central.library 的配置会怎样?

正在构建的模块的 Proguard 配置是级联到它的依赖项还是每个依赖项都遵循自己的规则?

从技术上讲,它是这样的:

Library projects by themselves don't run ProGuard, so they don't use any configuration.

Application projects obfuscate the entire code base, including any referenced libraries, so they need proper configuration for the application code and for the library code.

我有一个小案例,我有一个 Facebook 库作为 gradle 依赖项,因为我们用 minifyEnabled:true 混淆了代码,所以我们必须防止它的所有代码被混淆,使用定期保留命令,例如:

-keep class com.facebook.** { *; }

此外,关于 .jar 混淆,您可以查看 this other post

此致,

如果模块本身被混淆 (minifyEnabled true),使用的配置不会自动被使用模块(在您的情况下是应用程序)继承。

Android gradle 插件中有一种机制可以启用此功能:

consumerProguardFiles 'proguard-rules.pro'

proguard-rules.pro中包含的规则将自动包含在应用程序项目中并与其他配置文件合并。

这仅适用于 Android 个库项目 (.aar)。例如,如果您对 maven central 上的 .jar 文件有依赖性,则没有这样的消费者规则可用,您必须自己将所需的配置添加到您的应用程序项目中。

请记住,混淆模块的配置和消费应用程序/模块使用的配置不需要相同。在大多数情况下,消费者规则只是一组 -keep 规则。