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

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

问题描述

根据 Google 的说法,在将我的 Android 应用发布到 Google Play 之前,我必须在源代码中停用对 Log 方法的任何调用".摘自发布清单:

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.

我的开源项目很大,每次发布时手动完成很痛苦.此外,删除 Log 行可能很棘手,例如:

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 语法来有效但安全地删除所有日志行?

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 检查并使用 ProGuard 去除任何 Log.d()Log.v()当我们调用 Ant release 目标时调用方法.

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 年后):现在我使用 Timber for 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:

Timber 在我的 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!");
}

参见 Timber 示例应用 更高级的示例,其中所有日志语句在开发期间都发送到 logcat,在生产中,不记录调试语句,但错误会静默报告给 Crashlytics.

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天全站免登陆