Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

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

The negator (exclamation mark) in proguard should allow me to keep anthing but the apache libraries:

-keep class !org.apache.**

According to those answers. That's the way to go:

However, it obfuscates all classes in my APK.

That's part of my build.gradle (I have 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'
}

After I added -printconfiguration to my proguard-rules.pro file I saw there are numerous -keep rules following my -keep class !org.apache.**

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

# Referenced at ***anonymized***appuildintermediatesmerged_manifests
eleaseAndroidManifest.xml:180
-keep class android.support.v4.app.CoreComponentFactory { <init>(); }
# Referenced at ***anonymized***appuildintermediatesmerged_manifests
eleaseAndroidManifest.xml:180
-keep class com.mycompany.MyApplication { <init>(); }
# Referenced at C:Users***anonymized***.gradlecachesransforms-2files-2.17f5f0b3369d8fa8a72a20e2278ec0accappcompat-v7-28.0.0
eslayoutabc_action_menu_item_layout.xml:17
-keep class android.support.v7.view.menu.ActionMenuItemView { <init>(...); }

That approach suggested by Ezekiel Baniaga also didn't work. Instead it keeps everything including the apache packages:

proguard-rules.pro

-printconfiguration

-dontshrink

-dontoptimize

-dontobfuscate

-keep,allowshrinking,allowoptimization,allowobfuscation class org.apache.**
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should file a bug report with the R8 project if this does not work anymore.

In order to keep using Proguard in the meantime, you can add this to your gradle.properties files:

android.enableR8=false

Further tests show that the implicit behavior of ProGuard is not implemented like that in R8.

So a rule like:

-keep class !org.apache.**

will implicitly keep all other classes when using ProGuard, but not when using R8. To achieve the same behavior with R8, change the rule to this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...