如何向被 Proguard 删除的 Android Java 应用程序添加日志记录?
我在应用程序中放置了大量日志记录以简化调试,但我想确保只有最少的日志记录进入生产应用程序。我的理解是编译器不会删除未使用的代码,Android 没有条件编译,但 Proguard 会删除未使用的代码。这是一个示例跟踪类:
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 final boolean info = logginglevel > 3;
public static final boolean debug = logginglevel > 4;
}
然后在应用程序本身中记录如下:
if (Trace.debug) Log.d(TAG,"problem")
这会成功地允许 Proguard 识别未使用的语句并删除它们吗?这个声明相当难看,但这是我能看到的最短的声明。
是否有更简洁的方法来包含 Android 应用程序中的登录,但是否会在生产版本中自动删除它?我怀疑可以配置 Proguard 本身来删除代码的特定方面,但不确定这是否是比这更好的解决方案。
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 final 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从另一个 StackOverflow 响应...
将其添加到您的 proguard 配置文件中:
From another StackOverflow response...
add this to your proguard config file:
此处
删除所有调试日志记录调用发布之前:有没有工具可以做到这一点?
Here
Remove all debug logging calls before publishing: are there tools to do this?