7

I am developing my Android app.

Then I enable & configure proguard by:

Step 1. Enable proguard:

In project.properties I have:

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

I also tried the following:

# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
proguard.config=proguard.cfg

Step 2. Configure proguard:

In proguard.cfg I have:

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

I think the above configuration should remove all logs.

But when I install the APK under target/ folder & run my app, I can still see all my Log messages in logcat console. Why?

Joe
  • 14,039
  • 2
  • 39
  • 49
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

3 Answers3

11

You should use the first line in your project.properties:

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

You should then add these lines to your proguard-project.txt (not the deprecated proguard.cfg):

-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(...);
}

These options only have any effect if the file does not contain -dontoptimize.

Ant and Eclipse pick up the settings from project.properties. Gradle and Maven require equivalent settings that specify the configuration files, in build.gradle and in pom.xml respectively.

Similar questions and answers:

Community
  • 1
  • 1
Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
  • Since when the proguard.cfg is deprecated? – Leem.fin Apr 01 '14 at 10:34
  • @Leem.fin It's not, at all. Simply add `proguard.config=proguard.cfg` to project.properties. – Paul Burke Apr 03 '14 at 11:14
  • The SDK Tools Revision 20 started combining proguard-project.txt with a central proguard-android.txt, in favour of proguard.cfg, at least when you create a new project. The new approach is definitely preferable, since it separates the project-specific configuration (maintained by you) from the generic configuration (maintained by Google's Android team). It appears the documentation hasn't been updated yet. – Eric Lafortune Apr 04 '14 at 21:57
  • There are some risk of using proguard-android-optimize. Checkout the comment in https://android.googlesource.com/platform/sdk/+/master/files/proguard-android-optimize.txt. – Elye Jul 07 '16 at 06:33
  • There are other approach of removing debug message. Checkout https://medium.com/@elye.project/debug-messages-your-responsible-to-clear-it-before-release-1a0f872d66f#.6io15cj29 – Elye Jul 07 '16 at 06:34
  • @Leem.fin `proguard.cfg` is just a different name for `proguard-rules.txt`, which was combined in `proguard-android.txt` in newer SDKs. Now, even [proguard-android.txt is no longer maintained](https://stackoverflow.com/q/76701909/3154666)... Since any name can be specified in `build.gradle`, I am therefore back to the nostalgic `proguard.cfg` name. – Introspective Jul 21 '23 at 05:02
1

I can't remember where I found the reference to this method, but I've always used this:

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

in my configuration. It does remove debug and verbose logging that I wrote

NickT
  • 23,844
  • 11
  • 78
  • 121
0

You can create your own Log utilit class and use it,

What I do when I release my apps is: I create my own Log class with methods i, d, e, w etc and use this Log class instead of the Android one, because then I can use a simple switch like boolean debug = true according to which I write to the LogCat or don't. That way I can leave all my log statements in the app. When you've written your own Log class, to use it all over your app, you can simply replace all,

Remove:

import android.util.Log; 

Add

import your.package.Log; 

Like this:

    public class Log {

        public static void i(String logTag, String logString) {

            if (isLogsEnabled) {
                Log.i(logTag, logString);
             }
        }

        public static void v(String logTag, String logString) {

            if (isLogsEnabled) {
                Log.v(logTag, logString);
             }
        }

          // you can add method for w,d,wtf also...
    }

Logging is a very handy debugging and diagnostic technique used by developers. Use the logging class provided as part of the Android SDK to log important information about your application to LogCat, but make sure you review your application’s logging implementation prior to publication, as logging has performance drawbacks.

Before releasing your application Review your log carefully so its doesn't leek any confidential data,

Log is very important whenever your application in testing mode, Logs will provide you current state and scenario of your application on current device. So its very helpful whenever you will update your application.

Sometime Google play reject your application if they was found your Logging mechanism violate the rules.

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
  • 1
    the question was specific to trim the log messages with proguard – Even Cheng Sep 03 '15 at 09:54
  • Using proguard, you'll need proguard-android-optimize, which is risky as per stated by the comment in https://android.googlesource.com/platform/sdk/+/master/files/proguard-android-optimize.txt. – Elye Jul 07 '16 at 06:34
  • Hence the above is a good option. There are other options as stated in https://medium.com/@elye.project/debug-messages-your-responsible-to-clear-it-before-release-1a0f872d66f#.6io15cj29 – Elye Jul 07 '16 at 06:35