24

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.

Community
  • 1
  • 1
Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39

4 Answers4

41

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 { *; }
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • 1
    WARNING: Doing so will remove the wait() method of all objects, because *; includes all method of Log, including the wait() from Object class! This will surely cause weird side effects. – 3c71 May 28 '20 at 11:22
  • 1
    Do not ever do -assumenosideeffects class android.util.Log { *; } to avoid side effects. It will also be made an error soon in the new R8 – Sinapse Jul 02 '20 at 05:19
18

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

Gallal
  • 4,267
  • 6
  • 38
  • 61
12

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.

10

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(...);
}
Vincent Hiribarren
  • 5,254
  • 2
  • 41
  • 65
  • dontpreverify is not strictly related to optimization. "Only when eventually targeting Android, it is not necessary, so you can then switch it off to reduce the processing time a bit." https://www.guardsquare.com/en/products/proguard/manual/usage – Yazazzello Sep 25 '18 at 10:13