45

I am trying to use proguard to strip all my logs: I have entered the following line in my proguard-project.txt:

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

And my project.properties looks like this:

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

Inspite of this the logs continue to show in my application. What exactly am I doing wrong here?

Frank
  • 16,476
  • 7
  • 38
  • 51
user1667307
  • 2,041
  • 6
  • 27
  • 32
  • 2
    The canonical answer for this uses slightly different syntax: http://stackoverflow.com/questions/5553146/disable-logcat-output-completely-in-release-android-app/5553290#5553290 – CommonsWare Nov 04 '12 at 13:11
  • I tried that too. It doesnt work for some reason. Any ideas? – user1667307 Nov 04 '12 at 13:17
  • 1
    If the above didn't work for you, perhaps proguard isn't running? Check to make sure you're getting files generated in `proguard`. Note that when building or deploying your app from eclipse, the _only_ time proguard is run is when you generated a signed apk. – jbowes Nov 04 '12 at 13:22
  • Off the top of my head, no. The answer I linked to is a little old but should be OK AFAIK. You might consider adding `-verbose` and/or `-whyareyoukeeping class android.util.Log` to see if that tells you anything. – CommonsWare Nov 04 '12 at 13:22
  • check that you are getting a mappings.txt file generated by pro guard, like that you will know that proguard is running. – Frank Nov 04 '12 at 13:25
  • @jbowes- I did export, Android application(created a keystore and all that) and then used adb install to put it into my phone.@Frank-Yep . mappings.txt is there – user1667307 Nov 04 '12 at 13:36
  • i am out of suggestions, sorry... i am sure the solution below works, so .... – Frank Nov 04 '12 at 13:46
  • Okay. No problem. I will just comment the logs out. – user1667307 Nov 04 '12 at 13:53

3 Answers3

99

You shouldn't specify the '*' wildcard, because that includes methods like 'Object#wait()'. Better explicitly list the methods:

-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

This option is only relevant if optimization is not disabled, like in proguard-android.txt. You have to specify proguard-android-optimize.txt instead:

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

or with the contemporary Android Gradle plugin

buildTypes {
    releaseSomeBuildType {
        ...
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'your-proguard-file.pro'
    }
}   
Patrick
  • 33,984
  • 10
  • 106
  • 126
Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
  • 1
    Is there a reason for excluding the isLoggable method? I haven't used this anywhere in my project. Should I? – Roger Rapid Jan 24 '13 at 15:51
  • 2
    @Roger It tells ProGuard to remove the invocation of isLoggable if its return value isn't used, which is a useful optimization. If your project doesn't have any such invocations, that's fine. – Eric Lafortune Jan 29 '13 at 11:00
  • @EricLafortune Hi, I am enabling optimization to my Android project so do I need to write `proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt` both, or simply I have to add `proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt` in property file? Please help me . – Pankaj Kumar Feb 11 '13 at 09:46
  • 1
    @Pankaj "... instead", so you need the shorter version. – Eric Lafortune Feb 14 '13 at 09:49
  • Thank you... one more question is here if you don't mind... I am using ORMLite in my project, but ProGuard is not renaming the model classes. Is this normal or something bad in my ProGuard property. – Pankaj Kumar Feb 14 '13 at 09:57
  • totally missed proguard-android-optimize.txt when i first read this – scottyab Jun 21 '13 at 16:24
  • 3
    Hi Eric, please help me. I used your suggestion and in some cases log lines are not completely removed Case 1. If in my code I write: `Log.i(getClass().getSimpleName(), "maximize:: return true");` this is removed. Case 2. If in my code I write: `Log.i(getClass().getSimpleName(), "maximize:: index=" + index + ", ratio=" + ratio);` this become: `new StringBuilder("maximize:: index=").append(paramInt1).append(", ratio=").append(paramInt2).toString();` Please could you tell me how to obtain the same result for both cases? Thanks in advance. – Lisitso Sep 28 '14 at 22:16
  • @user1951805: unfortunately not. – Lisitso Jan 13 '15 at 09:13
  • 1
    @Lisitso You can create your own logging methods with multiple arguments, which you can then remove: http://stackoverflow.com/questions/6009078/removing-unused-strings-during-proguard-optimisation – Eric Lafortune Mar 28 '15 at 00:56
  • It should also be noted that it removes Object methods like `Object#wait()` from ALL classes, not just the one specified. – Tspoon Jun 05 '15 at 09:47
  • Say, if I have a class that's similar to Log, and I wish to disable calls to all of its functions (which are also static like in Log), how do I do it? – android developer Sep 14 '16 at 08:23
16

It's pretty late and I came across the same problem. I am using Android studio 1.3 and this is what I did.

  1. Add the log methods that you want to strip in your release build in proguard-android-optimize.txt:

    -assumenosideeffects class android.util.Log {
        public static boolean isLoggable(java.lang.String, int);
        public static int d(...);
        public static int w(...);
        public static int v(...);
        public static int i(...);
    }
    
  2. In your build.gradle (Module: app) set proguard-android-optimize.txt as default proguard file instead of proguard-android.txt:

    buildTypes {
        release {
            minifyEnabled true
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
        }
    }
    

This is because in proguard-android.txt optimization is turned off by default with flags

-dontoptimize
-dontpreverify 

This worked for me, hope it helps others.

Patrick
  • 33,984
  • 10
  • 106
  • 126
arshiya
  • 471
  • 3
  • 5
7

You need to do it like this:

-assumenosideeffects class android.util.Log {
public static int d(...);
public static int v(...);
public static int i(...);
public static int w(...);
public static int e(...);
public static int wtf(...);
    }

and expand for all the log methods you are using.

Frank
  • 16,476
  • 7
  • 38
  • 51