Removing Log call using proguard - android

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?

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

It's pretty late and I came across the same problem. I am using Android studio 1.3 and this is what I did.
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(...);
}
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.

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.

Related

Is there any way to disable Proguard configuration output?

I use this in each module of my multi-module library project
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), '../proguard-rules-library.pro'
Is there any way I can disable configuration outputs to console on build stage? This is an example:
The proguard configuration file for the following section is C:\Users\pc\Development\project\build\app\intermediates\default_proguard_files\global\proguard-android.txt-7.2.2
This is a configuration file for ProGuard.
http://proguard.sourceforge.net/index.html#manual/usage.html
This is just a part of a log output. It goes on further with printing the contents of my proguard-rules-library.pro files
P.S. there is no -printconfiguration flag in the file, but default proguard-android-optimize has the -verbose flag. But I don't think it could be the cause of such behavior
You can remove logging calls with this option in proguard-project.txt:
-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 relevant if optimization is not disabled, like in proguard-android.txt. You have to specify proguard-android-optimize.txt instead, in project.properties:
proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt

Using R8 and proguard to remove logging, but turn off everything else

I am trying to use R8 and proguard to remove logging from the release build. The catch is that I need to this be minimally invasive at the moment, so I would like to enable R8/proguard to remove logs, but turn off everything else. IE minifcation, obfuscation, etc.
build.gradle:
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
proguard-rules.pro:
-dontobfuscate
-dontoptimize
-dontshrink
-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static int v(...);
public static int w(...);
public static int e(...);
public static int d(...);
public static int i(...);
}
However when building and deploying a release build the logs are not removed. I imagine that this is because assumenosideeffects runs as part of one of the options that I turned off.
I have also tried this:
-keep class ** { *; }
-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static int v(...);
public static int w(...);
public static int e(...);
public static int d(...);
public static int i(...);
}
However that also still leaves logging.
Without moving to a different logging library or modifying code, is is possible to to remove logging with R8/proguard and not run anything else?
EDIT:
I an effort to figure out why proguard/r8 is not removing logs I created a brand new project. I added one line of logging with the following config:
release {
debuggable true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
However when I build the release version and install the APK I am still seeing logging, which means my log statement was not removed.
EDIT:
debuggable true
Does skip r8 optimization. So this is not a good way to verify logs have been removed by proguard. Using a dex to jar application to verify is the way to go. dex2jar worked for me.
Please remove debuggable true line from release block that's why you are seeing logs in build.
I'm also using R8 to remove logs from release builds for some of my apps. The only difference I can see between your and my config are three asterisks before the v(), d(), ... functions.
So this is what's working for my release builds:
-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static *** v(...);
public static *** d(...);
public static *** i(...);
public static *** w(...);
public static *** e(...);
}
Edit: If this doesn't help, maybe this is cause by a behavior change in AGP (related question: Android Gradle Plugin 4.2.x changed behavior for assumenosideeffects)

Proguard not stripping down Timber logs

I am using Timber logs for logging in my Android application only in the Debug environment, for that purpose, I added this line in my Application class:-
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
I, subsequently, used this throughout my application flawlessly. Also, I obfuscated the Timber logs for release build variant, by adding these lines to my proguard-rules.pro file:-
-assumenosideeffects class timber.log.Timber* {
public static *** v(...);
public static *** d(...);
public static *** i(...);
public static *** e(...);
public static *** w(...);
}
In my app's build.gradle, I added this:
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
Despite, all of this in my production app, I got a crash through Crashlytics caused for a NullPointerException inside one of the Timber log statement, the Timber log, first of all, shouldn't have been there in the production app, as I've stripped it down, moreover, I've planted Timber log only in the debug environment, so I don't understand why it'snot stripped down.
I think on Timber has use Log. You can try it add Timber with Log on proguard-rules.pro
# Remove log
-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(...);
public static int e(...);
}
-assumenosideeffects class timber.log.Timber* {
public static *** d(...);
public static *** w(...);
public static *** v(...);
public static *** i(...);
public static *** e(...);
}

Use ProGuard to exclude Log in Android

Is it possible to exclude the implementation of Log class from the apk using ProGuard during the build generation? So when we generate an apk, we do not have the development Logs in the class files.
Yes, you can remove logs.
-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(...);
}
more information: Removing Log call using proguard

Proguard - removing logs roboguice.util.Ln

In my app we are using logs from roboguice.util.Ln. I read how to remove logging by using proguard, but it seems to not working on roboguice logging. The last thing i tried was to add in my proguard configuration file something like this:
-assumenosideeffects class roboguice.util.Ln {
public static *** v(...);
public static *** i(...);
public static *** w(...);
public static *** d(...);
public static *** e(...);
public static boolean isDebugEnabled();
public static boolean isVerboseEnabled();
}
-assumenosideeffects class roboguice.util.Ln$Print {
public int println(int, java.lang.String);
protected java.lang.String processMessage(java.lang.String);
protected static java.lang.String getScope(int);
}
I also tried to do this without adding Print inner class, methods is***Enabled() etc. Any ideas how to remove roboguice.util.Ln logs?
Your configuration for roboguice.util.Ln looks correct. However, the option -assumenosideeffects only has effect with optimization enabled. In Android builds this means: in release mode and with the shared configuration proguard-android-optimize.txt in project.properties for Ant, or build.gradle for Gradle.

Categories

Resources