Removing Log call using proguard
Asked Answered
N

3

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?

Nutt answered 4/11, 2012 at 13:6 Comment(8)
The canonical answer for this uses slightly different syntax: #5553646Cooper
I tried that too. It doesnt work for some reason. Any ideas?Nutt
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.Console
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.Cooper
check that you are getting a mappings.txt file generated by pro guard, like that you will know that proguard is running.Schoenfeld
@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 thereNutt
i am out of suggestions, sorry... i am sure the solution below works, so ....Schoenfeld
Okay. No problem. I will just comment the logs out.Nutt
S
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'
    }
}   

Resources

Shumate answered 11/11, 2012 at 0:51 Comment(11)
Is there a reason for excluding the isLoggable method? I haven't used this anywhere in my project. Should I?Decussate
@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.Shumate
@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 .Arleanarlee
@Pankaj "... instead", so you need the shorter version.Shumate
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.Arleanarlee
totally missed proguard-android-optimize.txt when i first read thisChiliad
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.Financial
@user1951805: unfortunately not.Financial
@Financial You can create your own logging methods with multiple arguments, which you can then remove: #6009578Shumate
It should also be noted that it removes Object methods like Object#wait() from ALL classes, not just the one specified.Sennacherib
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?Forgetmenot
Y
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.

Yorktown answered 30/9, 2015 at 5:48 Comment(4)
FWIW, debuggable false is not necessaryDewie
@AlexCohn if you want someone to debug your app then not ;)Bernardinabernardine
i am getting error when using proguard-android-optimize.txt - java.lang.IllegalArgumentException: Form-encoded method must contain at least one @Field.Sheathe
When do you use isLoggable exactly? What is its purpose? I never used itForgetmenot
S
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.

Schoenfeld answered 4/11, 2012 at 13:10 Comment(4)
IMHO, This does not work anymore..One has to provide specific return type int instead of ***.Flood
@RiteshGune Fixed.Schoenfeld
i am getting error when using proguard-android-optimize.txt java.lang.IllegalArgumentException: Form-encoded method must contain at least one @Field.Sheathe
Please ask as a new question and attach config filesSchoenfeld

© 2022 - 2024 — McMap. All rights reserved.