问题
I have noticed that my release build, which i made in eclipse via:
Android Tools -> Export signed application package ...
My proguard-project.txt contains:
-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(...);
}
Yet, if I run the app on a device und use CatLog to look at logs I see that there are all the Log.d(), Log.v() messages that shouldn't be there. Is there something else I should set? I have spent many hours googling but there are no clear instructions on that subject. Well most of the things i find are people with similar problems but no solutions.
回答1:
Make sure your proguard configuration does not have -dontoptimize
in it. It's the proguard optimizer that removes method calls that have "no side effects".
The SDK default proguard-android.txt
has -dontoptimize
in it. Use proguard-android-optimize.txt
instead, and don't add -dontoptimize
in your project-specific proguard configuration.
Could you please state some source where I could read up on the subject. If it exists of course.
Well, for starters -assumenosideeffects
is listed under optimization options in the documentation.
One more thing bothers is that, I have read that using proguard-android-optimize.txt can cause problems on some devices. So, it is a sort od catch 22 situation.
Proguard in general can have side effects. You need to test your proguard-processed application thoroughly.
If you want to remove logging from release builds without using proguard at all, you can e.g. add
if (BuildConfig.DEBUG)
before every logging call where BuildConfig
is the build configuration generated by the Gradle Android plugin. Since the DEBUG
there is a compile-time constant, the Java compiler sees the condition can never be true on a release configuration and won't emit the bytecode inside the conditional block.
回答2:
did you try this?
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
public static *** i(...);
public static *** w(...);
public static *** e(...);
}
回答3:
In my application, I wrapped the logger calls with my own class, which checks a Boolean- "debugMode" before logging. This way, changing the Boolean flag to false, results in not having logs (actually, this is just the concept since I want to allow different levels of logging as well, but the idea is the same). You can configure each level as you wish, so you still see severe logs but exclude the debug ones.
来源:https://stackoverflow.com/questions/33067142/how-to-remove-log-d-calls-in-release-build-of-an-android-app