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.
Related
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
I am trying to remove logging using R8/Proguard, and I used
-assumenosideeffects class android.util.Log {
v(...);
d(...);
i(...);
w(...);
e(...);
}
which works fine. The issue arrised when trying to remove the custom logging in com.mypackage:
-assumenosideeffects class com.mypackage.MyCustomLogger {
v(...);
d(...);
i(...);
w(...);
e(...);
}
I have a rule that keeps all the public methods of the public classes from com.mypackage:
-keep, allowoptimization public class com.mypackage.** {
public *;
}
which I can't remove due to the scope of the project. This rule seemingly overrides the assumenosideeffects rule, and here is my question: how do I specify an exception to this keep rule? I know about the -if option but I haven't seen it anywhere used in the negative. I've tried writing -if class !com.mypackage.Logger before the -keep rule, but the build is taking a ridiculous amount of time and just never finishes.
I've found the answer, I need to add my Logger class within the keep rule like so:
-keep, allowoptimization public class !com.mypackage.MyCustomLogger, com.mypackage.** {
public *;
}
I need to strip down all my Timber logs in my production build. I have added the following in my proguard-project.txt :
-assumenosideeffects class timber.log.Timber* {
public static *** tag(...);
public static *** v(...);
public static *** i(...);
public static *** w(...);
public static *** d(...);
public static *** e(...);
-assumenosideeffects class timber.log.Timber.tag* {*;}
}
However only logs with the format Timber.e() or Timber.i() etc is being stripped. Logs with Timber.tag(..).i(...) are not getting stripped.
Is there any way to achieve this? Thank you
wouldn't it be easier to do something line
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
in your Application subclass? This way you will the logs only in debug
Stripping Timber.tag(...) cannot be done via Proguard. According to the usage guide,
In the optimization step, ProGuard can then remove calls to such methods, if it can determine that the return values aren't used.
Since Timber.tag(...) will return a Timber.Tree object, Proguard optimizer will not strip it off for you. Therefore, if you really want to strip off the log, you have to wrap it via a function e.g.
fun printDebugLog(tag: String, message: String) {
Timber.tag(tag).d(message)
}
Then you can use -assumenosideeffects to strip off printDebugLog call. Another thing you have to be aware of that when your class is in Kotlin, the following rules cannot be used to strip off Timber logs also
-assumenosideeffects class timber.log.Timber* {
public static *** d(...);
public static *** v(...);
public static *** i(...);
}
This is because when you decompile the Kotlin class to Java, logging will actually translated to
Timber.Forest.d(...)
Which will not be stripped off for the similar reason.
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(...);
}
I am using OpenCV in my android app. CvCameraViewListener2 is generating lots of logging. How do I systematically turn off OpenCV logging on android?
I ended up disabling android logs through proguard:
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** w(...);
public static *** v(...);
public static *** i(...);
}