Proguard(R8)否定操作员不工作以保留某些包以外的任何东西

Proguard (R8) negate operator not working to keep anything except certain packages

proguard 中的否定符(感叹号)应该允许我保留 anthing 但 apache 库:

-keep class !org.apache.**

根据那些答案。这是要走的路:

但是,它混淆了我 APK 中的所有 类。

那是我的 build.gradle 的一部分(我有 Android Studio 3.5.3)

compileSdkVersion 29
buildToolsVersion "29.0.2"
//...
buildTypes {

    release {

        minifyEnabled true
        proguardFiles /*getDefaultProguardFile('proguard-android.txt'),*/  'proguard-rules.pro'

        // Enables resource shrinking, which is performed by the
        // Android Gradle plugin.
        shrinkResources false
    }
}

dependencies {
    //Utility libs
    implementation 'org.apache.commons:commons-collections4:4.1'
    implementation 'org.apache.commons:commons-lang3:3.4'
    implementation group: 'commons-io', name: 'commons-io', version: '2.5'
}

在我将 -printconfiguration 添加到我的 proguard-rules.pro 文件后,我看到有许多 -keep 规则遵循我的 -keep class !org.apache.**

-printconfiguration
-keep class !org.apache.**

# Referenced at ***anonymized***\app\build\intermediates\merged_manifests\release\AndroidManifest.xml:180
-keep class android.support.v4.app.CoreComponentFactory { <init>(); }
# Referenced at ***anonymized***\app\build\intermediates\merged_manifests\release\AndroidManifest.xml:180
-keep class com.mycompany.MyApplication { <init>(); }
# Referenced at C:\Users\***anonymized***\.gradle\caches\transforms-2\files-2.1f5f0b3369d8fa8a72a20e2278ec0acc\appcompat-v7-28.0.0\res\layout\abc_action_menu_item_layout.xml:17
-keep class android.support.v7.view.menu.ActionMenuItemView { <init>(...); }

Ezekiel Baniaga 建议的方法也没有用。相反,它保留了所有内容,包括 apache 包:

proguard-rules.pro

-printconfiguration

-dontshrink

-dontoptimize

-dontobfuscate

-keep,allowshrinking,allowoptimization,allowobfuscation class org.apache.**

如果这不再有效,您应该向 R8 项目提交错误报告。

为了在此期间继续使用 Proguard,您可以将其添加到您的 gradle.properties 文件中:

android.enableR8=false

进一步的测试表明 ProGuard 的隐式行为并未像 R8 中那样实现。

所以这样的规则:

-keep class !org.apache.**

在使用 ProGuard 时将隐式保留所有其他 类,但在使用 R8 时不会。要使用 R8 实现相同的行为,请将规则更改为:

-keep class !org.apache.**,**

我必须添加 ,** 才能使其正常工作。谢谢

-keep class !org.apache.**,**

前面的示例保留了 class 个名称,但仍然混淆了成员。所以我不得不添加 { *; }:

-keep class !org.apache.**,** { *; }

这就是我混淆多个包的方式(我必须在一个保留规则中使用它们!)

-keep class !org.apache.**, !org.zeroturnaround.**, !com.drew.**, ** { *; }

为了找出 -dontshrink -dontoptimize -dontobfuscate -keep,allowshrinking,allowoptimization,allowobfuscation class org.apache.** 的问题所在,我可以根据 https://www.guardsquare.com/en/products/proguard/manual/usage

添加 -whyareyoukeeping