在构建Android应用的发行版之前,如何删除所有调试日志记录调用? [英] How to remove all debug logging calls before building the release version of an Android app?

查看:102
本文介绍了在构建Android应用的发行版之前,如何删除所有调试日志记录调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Google的说法,在将我的Android应用发布到Google Play之前,我必须"在源代码中停用对Log方法的所有调用".从发布清单的第3节中提取:

According to Google, I must "deactivate any calls to Log methods in the source code" before publishing my Android app to Google Play. Extract from section 3 of the publication checklist:

在构建要发布的应用程序之前,请确保禁用日志记录并禁用调试选项.您可以通过删除对源文件中Log方法的调用来停用日志记录.

Make sure you deactivate logging and disable the debugging option before you build your application for release. You can deactivate logging by removing calls to Log methods in your source files.

我的开源项目很大,每次发布时都很难手动进行.此外,删除日志行可能很棘手,例如:

My open-source project is large and it is a pain to do it manually every time I release. Additionally, removing a Log line is potentially tricky, for instance:

if(condition)
  Log.d(LOG_TAG, "Something");
data.load();
data.show();

如果我在Log行中注释,则该条件适用于下一行,并且可能不会调用load().这样的情况难道足以我决定不存在吗?

If I comment the Log line, then the condition applies to the next line, and chances are load() is not called. Are such situations rare enough that I can decide it should not exist?

那么,有没有更好的源代码级方法呢?还是一些聪明的ProGuard语法可以有效但安全地删除所有Log行?

So, is there a better source code-level way to do that? Or maybe some clever ProGuard syntax to efficiently but safely remove all Log lines?

推荐答案

我发现一个更简单的解决方案是忘记所有if检查,而只需使用

I find a far easier solution is to forget all the if checks all over the place and just use ProGuard to strip out any Log.d() or Log.v() method calls when we call our Ant release target.

这样,我们始终可以为常规版本输出调试信息,而不必为发行版本进行任何代码更改. ProGuard还可以对字节码进行多次传递,以删除其他不需要的语句,空块,并可以在适当的情况下自动内联短方法.

That way, we always have the debug info being output for regular builds and don't have to make any code changes for release builds. ProGuard can also do multiple passes over the bytecode to remove other undesired statements, empty blocks and can automatically inline short methods where appropriate.

例如,这是一个非常基本的Android ProGuard配置:

For example, here's a very basic ProGuard config for Android:

-dontskipnonpubliclibraryclasses
-dontobfuscate
-forceprocessing
-optimizationpasses 5

-keep class * extends android.app.Activity
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

因此,您将其保存到文件中,然后从Ant调用ProGuard,并传入刚编译的JAR和您正在使用的Android平台JAR.

So you would save that to a file, then call ProGuard from Ant, passing in your just-compiled JAR and the Android platform JAR you're using.

另请参阅ProGuard手册中的示例.

See also the examples in the ProGuard manual.

更新(4.5年后)::如今,我在Android上使用了木材记录.

Update (4.5 years later): Nowadays I used Timber for Android logging.

它不仅比默认的Log实现好一点-日志标签是自动设置的,而且很容易记录格式化的字符串和异常-而且您还可以在运行时指定不同的日志记录行为.

Not only is it a bit nicer than the default Log implementation — the log tag is set automatically, and it's easy to log formatted strings and exceptions — but you can also specify different logging behaviours at runtime.

在此示例中,日志记录语句将仅在我的应用程序的调试版本中写入logcat:

In this example, logging statements will only be written to logcat in debug builds of my app:

在我的Application onCreate()方法中设置了木材:

Timber is set up in my Application onCreate() method:

if (BuildConfig.DEBUG) {
  Timber.plant(new Timber.DebugTree());
}

然后在我的代码中的其他任何地方,我都可以轻松登录:

Then anywhere else in my code I can log easily:

Timber.d("Downloading URL: %s", url);
try {
  // ...
} catch (IOException ioe) {
  Timber.e(ioe, "Bad things happened!");
}

请参见

See the Timber sample app for a more advanced example, where all log statements are sent to logcat during development and, in production, no debug statements are logged, but errors are silently reported to Crashlytics.

这篇关于在构建Android应用的发行版之前,如何删除所有调试日志记录调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆