Android Proguard not removing all log messages

2020-02-17 05:37发布

问题:

I want to create an obfuscated android application. I use ProGuard for that. I would like to automatically remove all the Log.* messages. How can I do that? I found this post but I still get them. (I use a decompiler to check the obfuscation).
The proguard-project.txt is the following:

-injars       libs/In.jar
-outjars      libs/Out.jar
#-libraryjars  <java.home>/lib/rt.jar
-libraryjars C:/Users/thomas/android-sdks/platforms/android-7/android.jar

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep public class * {
    public protected *;
}

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** e(...);
}

Any help would be appreciated.
Thanks.

回答1:

This only remove all debug Log.d(TAG, "..."); and error Log.e(TAG, "...") calls:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** e(...);
}

To remove all log calls, simply use this:

-assumenosideeffects class android.util.Log { *; }


回答2:

The default android Proguard configuration disables optimisation. To enable it, in your project's project.properties file use proguard-android-optimize.txt instead of proguard-android.txt



回答3:

For anyone that can't seem to wrap their head around pro-guard, you have to make sure you do two things.

1: From @yorkw

-assumenosideeffects class android.util.Log { *; }

2: From @Gallal

In your project's project.properties file use:

proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt

instead of

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

This is because you have two proguard options "out of the box" since they are included in the sdk

android-adk > tools > proguard

Which contains two files:

proguard-android.txt
proguard-android-optimize.txt

Hope that helps someone else down the line.



回答4:

If you have Android Studio, you have to modify the build.gradle of your main application.

In your gradle file, you have to specify the usage of proguard-android-optimize.txt as the default file.

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        // With the file below, it does not work!
        //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Actually, in the default proguard-android.txt file, optimization is disabled with the two flags:

-dontoptimize
-dontpreverify

The proguard-android-optimize.txt file does not add those lines, so now assumenosideeffects can work.

Then, as said in other answers for Eclipse or else, you just have to add to your main proguard file the line:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** e(...);
}