删除发布之前,所有调试日志记录来电:是否有工具来做到这一点? [英] Remove all debug logging calls before publishing: are there tools to do this?

查看:243
本文介绍了删除发布之前,所有调试日志记录来电:是否有工具来做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据谷歌,我一定要的停用任何呼叫日志中的源$ C ​​$ C方法的发布我的Andr​​oid应用程序之前。从出版物清单第5摘录:

  

请确保您禁用日志记录并禁用调试选项你建立你的应用程序发布之前。您可以通过删除调用登录方法在源文件中禁用日志记录。

我的开源项目大,这是一个痛苦的我每次释放做手工。此外,删除日志行可能是棘手的,例如:

 如果(条件)
  Log.d(LOG_TAG,东西);
data.load();
数据显示();
 

如果我评论日志行,则条件适用于下一行,机会是负载()没有被调用。在这种情况下足够罕见的,我可以决定它不应该存在吗?

这是官方的清单中,所以我想很多人都这样做定期。
那么,有没有删除所有日志行的工具?
preferably一个不骗了code象上面。

解决方案

我发现一个更容易的解决办法是忘记所有的如果检查所有的地方,只是使用 ProGuard的剥夺任何 Log.d() Log.v()当我们调用方法调用Ant的发布的目标。

这样一来,我们总是有调试信息是输出定期生成和不具备发布版本做任何code的变化。 ProGuard的也可以做多越过字节code删除其他不需要的语句,空块,并且能够自动内联短的方法在适当情况下。

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

  -dontskipnonpubliclibraryclasses
-dontobfuscate
-forceprocessing
-optimizationpasses 5

-keep类*扩展android.app.Activity
-assumenosideeffects类android.util.Log {
    公共静态*** D(...);
    公共静态*** V(...);
}
 

所以,你会保存到一个文件,然后从蚂蚁调用ProGuard的,传递您刚才编译JAR和你使用Android平台的JAR。

又见的范例在ProGuard的使用手册。


更新(4.5年后):现在我用木材 Android的记录。

它不仅是比默认值位更好登录实施 - 日志标签会自动设置,并且可以很容易地记录格式字符串和异常 - 但你也可以在运行时指定不同的日志记录行为。

在本实施例中,日志记录语句仅被写入在调试到的logcat构建我的应用程序的

木材是建立在我的应用程序 的onCreate()方法:

 如果(BuildConfig.DEBUG){
  Timber.plant(新Timber.DebugTree());
}
 

然后其他地方在我的code,我可以轻松登录:

  Timber.d(下载网址:%S,网址);
尝试 {
  // ...
}赶上(IOException异常IOE){
  Timber.e(IOE,坏的事情发生了!);
}
 

见<一href="https://github.com/JakeWharton/timber/blob/dd94a22/timber-sample/src/main/java/com/example/timber/ExampleApp.java">Timber示例应用程序一个更高级的例子,所有的日志语句发送开发过程中的logcat,并在生产,没有调试语句记录,但错误默默地报Crashlytics。

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

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();

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?

This is on the official checklist, so I guess many people do this on a regular basis.
So, is there a tool that removes all Log lines?
Preferably one that is not tricked by code like the above.

解决方案

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.

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.

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(...);
}

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.

See also the examples in the ProGuard manual.


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

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.

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

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.

这篇关于删除发布之前,所有调试日志记录来电:是否有工具来做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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