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?

8
  • 2
    The canonical answer for this uses slightly different syntax: stackoverflow.com/questions/5553146/… Commented Nov 4, 2012 at 13:11
  • I tried that too. It doesnt work for some reason. Any ideas? Commented Nov 4, 2012 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. Commented Nov 4, 2012 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. Commented Nov 4, 2012 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. Commented Nov 4, 2012 at 13:25

3 Answers 3

100

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' } } 

Resources

Sign up to request clarification or add additional context in comments.

11 Comments

Is there a reason for excluding the isLoggable method? I haven't used this anywhere in my project. Should I?
@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.
@Pankaj "... instead", so you need the shorter version.
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 You can create your own logging methods with multiple arguments, which you can then remove: stackoverflow.com/questions/6009078/…
|
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.

4 Comments

FWIW, debuggable false is not necessary
@AlexCohn if you want someone to debug your app then not ;)
i am getting error when using proguard-android-optimize.txt - java.lang.IllegalArgumentException: Form-encoded method must contain at least one @Field.
When do you use isLoggable exactly? What is its purpose? I never used it
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.

4 Comments

IMHO, This does not work anymore..One has to provide specific return type int instead of ***.
@RiteshGune Fixed.
i am getting error when using proguard-android-optimize.txt java.lang.IllegalArgumentException: Form-encoded method must contain at least one @Field.
Please ask as a new question and attach config files

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.