Use ProGuard to exclude Log in Android - 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

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

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

stream was reset: null

I had a strange bug which was hard to find :-(
In my Android app I use Retrofit2 with Moshi do access an api. I also use proguard-android-optimize.txt to make my app clean and small. And in my proguard-rules.pro I have:
-assumenosideeffects class timber.log.Timber { *; }
-assumenosideeffects class android.util.Log { *; }
With this combination I get this error stream was reset: null when I try to access the api.
To fix it I have to remove this assumenosideeffects lines from my proguard config. But why? Has anyone an idea why I get this network problem when proguard removes logging methods?
The wildcard * matches all methods, including methods in super classes, like Object#wait(). You don't really want to remove those invocations, so you should explicitly list the methods for which you want to remove the invocations:
-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(...);
}
ProGuard already prints out a warning if you specify the wildcard.

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.

Removing Log call using proguard

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.

Categories

Resources