在发布的 Android 应用程序中完全禁用 LogCat 输出?
在向市场发布应用程序之前关闭我自己的应用程序的 LogCat 输出非常简单。我还知道如何通过标签和/或 id 有选择地过滤 LogCat 消息,以方便我自己的调试。
但现在我对一些可能更困难(也许不可能?)的事情感兴趣:禁用所有 LogCat 输出,包括 & 。特别是那些来自 3rd-party 服务的服务,如 TtsService、GoogleLoginService 等。
这可能吗?
进一步澄清:我对为自己过滤消息不感兴趣。我对从 Android 市场下载我的应用程序的人禁用第 3 方消息非常感兴趣。这可能吗?
Shutting off my own app's LogCat output before releasing an app to the market is straightforward. I also know how to selectively filter LogCat message by tag and/or id for my own debug convenience.
But now I am interested in something that may be much more difficult (perhaps impossible?): Disable all LogCat output, including & especially those coming from 3rd-party services like TtsService, GoogleLoginService, etc.
Is this possible?
To further clarify: I am not interested in filtering out messages for myself. I am rather interested in disabling 3rd-party messages for whoever downloads my app from the Android Market. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 ProGuard 来完全删除任何不使用返回值的行,方法是告诉 ProGuard假设不会有任何问题。
以下 proguard.cfg 块指示删除 Log.d、Log.v 和 Log.i 调用。
最终结果是这些日志行不在您的发布 apk 中,因此任何使用 logcat 的用户都不会看到 d/v/i 日志。
You can use ProGuard to remove completely any lines where a return value is not used, by telling ProGuard to assume that there will be no problems.
The following proguard.cfg chunk instructs to remove Log.d, Log.v and Log.i calls.
The end result is that these log lines are not in your release apk, and therefore any user with logcat won't see d/v/i logs.
如果你不使用 proguard,你必须自己管理日志并在清单文件中将 dubuggable false 设置为 false
这是我的自定义日志类
if you don't use proguard, you have to manage the log yourself and in the manifest file make dubuggable false
Here my custom log class
David Caunt 提供的出色答案似乎不适用于
proguard-android 中定义的规则-optimize.txt
。当前版本的 ProGuard 似乎不使用通配符
***
,而是期望返回参数的类型限定符:The great answer provided by David Caunt doesn't seem to work for the rules defined in
proguard-android-optimize.txt
.Instead of using the wildcard
***
, current versions of ProGuard seem to expect the return parameter's type qualifier:在应用程序 build.gradle 文件集中:
在 proguard-rules.pro put 中:
它对我有用。
In app build.gradle file set:
In proguard-rules.pro put:
It worked for me.
我通常会这样做:
if (BuildConfig.DEBUG) Log.i(TAG, msg);
不过,如果你有很多依赖项(库)并且它们写得不好,那么就使用 https://stackoverflow.com/a/5553290/4548520
使行更短:
I usually do next:
if (BuildConfig.DEBUG) Log.i(TAG, msg);
though if you have a lot of dependencies (libraries) and they written bad then yeah just use https://stackoverflow.com/a/5553290/4548520
making lines shorter:
除了将 Android SDK 中的默认 ProGuard 设置(“proguard-android.txt”文件)替换为“proguard-”之外,我还结合了 David Snabel-Caunt 接受的答案。 android-optimize.txt”优化文件。该文件也位于此 Android SDK 文件夹中,具有相同的规则,但启用了优化。
I combined David Snabel-Caunt's accepted answer in addition to swapping out the default ProGuard settings ("proguard-android.txt" file) from the Android SDK for the "proguard-android-optimize.txt" optimized file. The file is also available in this Android SDK folder with the same rules but with optimizations enabled.
您不需要枚举类的所有方法,看起来您也可以简单地使用
简写:为我工作,使用
com.android.tools.build:gradle
版本4.0.0
。Instead of enumerating all the class's methods it looks like you can also simply use the
<methods>
shorthand :Worked for me with
com.android.tools.build:gradle
version4.0.0
.您可以将 debuggable false 放在 buildTypes 版本上。
You can put debuggable false on buildTypes release.