开发者

How to add logging to Android Java app that is stripped out by Proguard?

开发者 https://www.devze.com 2023-02-09 19:18 出处:网络
I put extensive logging in apps to ease debugging, but I want to ensure only the minimum goes into the production application. My understanding is that the compiler doesn\'t remove unused code, there\

I put extensive logging in apps to ease debugging, but I want to ensure only the minimum goes into the production application. My understanding is that the compiler doesn't remove unused code, there's no conditional compilation with Android, but Proguard will remove unused code. Here's a sample tracing class:

public class Trace
{
  public static final int logginglevel = 5;

  public static final boolean errors = logginglevel > 1;
  public static final boolean warnings = logginglevel > 2;
  public static fin开发者_StackOverflow社区al boolean info = logginglevel > 3;
  public static final boolean debug = logginglevel > 4;
}

Then in the application itself log like this:

if (Trace.debug) Log.d(TAG,"problem")

Will this succeed in allowing Proguard to identify the unused statements and remove them? This statement is pretty ugly, but it's the shortest I can see that it can be.

Is there a more succinct way to include logging in an Android app, but have it automatically stripped out for the production build? I suspect it's possible to configure Proguard itself to remove specific aspects of the code, but not sure if that's a better solution than this.


From another StackOverflow response...

add this to your proguard config file:

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


Here

Remove all debug logging calls before publishing: are there tools to do this?

0

精彩评论

暂无评论...
验证码 换一张
取 消