在发布的 Android 应用程序中完全禁用 LogCat 输出?

发布于 2024-10-30 15:53:53 字数 304 浏览 1 评论 0原文

在向市场发布应用程序之前关闭我自己的应用程序的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

橘香 2024-11-06 15:53:53

您可以使用 ProGuard 来完全删除任何不使用返回值的行,方法是告诉 ProGuard假设不会有任何问题。

以下 proguard.cfg 块指示删除 Log.d、Log.v 和 Log.i 调用。

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** w(...);
    public static *** v(...);
    public static *** 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.

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** w(...);
    public static *** v(...);
    public static *** i(...);
}

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.

迟月 2024-11-06 15:53:53

如果你不使用 proguard,你必须自己管理日志并在清单文件中将 dubuggable false 设置为 false

<application
    android:name="MyApplication"
    android:icon="@drawable/gift"
    android:label="@string/app_name" android:debuggable="@bool/build_log">

这是我的自定义日志类

public class Lol {

    public static final boolean ENABLE_LOG = true & MyApplication.sDebug;

    private static final boolean DEBUG = true & ENABLE_LOG;

    private static final boolean VERBOSE = true & ENABLE_LOG;

    private static final boolean TEMP = true & ENABLE_LOG;

    private static final boolean WARNING = true & ENABLE_LOG;

    private static final boolean INFO = true & ENABLE_LOG;

    private static final boolean ERROR = true & ENABLE_LOG;

    public static void obvious(String tag, String msg) {
        if (DEBUG) {
            msg = "*********************************\n" + msg
                    + "\n*********************************";
            Log.d(tag, msg);
        }
    }

    public static void d(String tag, String msg) {
        if (DEBUG)
            Log.d(tag, msg);
    }

    public static void d(boolean bool, String tag, String msg) {
        if (TEMP&bool)
            Log.d(tag, msg);
    }

    public static void i(String tag, String msg) {
        if (INFO)
            Log.i(tag, msg);
    }

    public static void e(String tag, String msg) {
        if (ERROR)
            Log.e(tag, msg);
    }

    public static void e(boolean bool, String tag, String msg) {
        if (TEMP&bool)
            Log.e(tag, msg);
    }

    public static void v(String tag, String msg) {
        if (VERBOSE)
            Log.v(tag, msg);
    }

    public static void w(String tag, String msg) {
        if (WARNING)
            Log.w(tag, msg);
    }

    public static String getStackTraceString(Exception e) {
        return Log.getStackTraceString(e);
    }

    public static void w(String tag, String msg, Exception e) {
        if (WARNING)
            Log.w(tag, msg,e);
    }
}

if you don't use proguard, you have to manage the log yourself and in the manifest file make dubuggable false

<application
    android:name="MyApplication"
    android:icon="@drawable/gift"
    android:label="@string/app_name" android:debuggable="@bool/build_log">

Here my custom log class

public class Lol {

    public static final boolean ENABLE_LOG = true & MyApplication.sDebug;

    private static final boolean DEBUG = true & ENABLE_LOG;

    private static final boolean VERBOSE = true & ENABLE_LOG;

    private static final boolean TEMP = true & ENABLE_LOG;

    private static final boolean WARNING = true & ENABLE_LOG;

    private static final boolean INFO = true & ENABLE_LOG;

    private static final boolean ERROR = true & ENABLE_LOG;

    public static void obvious(String tag, String msg) {
        if (DEBUG) {
            msg = "*********************************\n" + msg
                    + "\n*********************************";
            Log.d(tag, msg);
        }
    }

    public static void d(String tag, String msg) {
        if (DEBUG)
            Log.d(tag, msg);
    }

    public static void d(boolean bool, String tag, String msg) {
        if (TEMP&bool)
            Log.d(tag, msg);
    }

    public static void i(String tag, String msg) {
        if (INFO)
            Log.i(tag, msg);
    }

    public static void e(String tag, String msg) {
        if (ERROR)
            Log.e(tag, msg);
    }

    public static void e(boolean bool, String tag, String msg) {
        if (TEMP&bool)
            Log.e(tag, msg);
    }

    public static void v(String tag, String msg) {
        if (VERBOSE)
            Log.v(tag, msg);
    }

    public static void w(String tag, String msg) {
        if (WARNING)
            Log.w(tag, msg);
    }

    public static String getStackTraceString(Exception e) {
        return Log.getStackTraceString(e);
    }

    public static void w(String tag, String msg, Exception e) {
        if (WARNING)
            Log.w(tag, msg,e);
    }
}
待"谢繁草 2024-11-06 15:53:53

David Caunt 提供的出色答案似乎不适用于 proguard-android 中定义的规则-optimize.txt

当前版本的 ProGuard 似乎不使用通配符 ***,而是期望返回参数的类型限定符:

-assumenosideeffects class android.util.Log {
    public static int   d(...);
    public static int   w(...);
    public static int   v(...);
    public static int   i(...);
    public static int wtf(...);
}

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:

-assumenosideeffects class android.util.Log {
    public static int   d(...);
    public static int   w(...);
    public static int   v(...);
    public static int   i(...);
    public static int wtf(...);
}
蘑菇王子 2024-11-06 15:53:53

在应用程序 build.gradle 文件集中:

release {
    minifyEnabled true
     ……
}

在 proguard-rules.pro put 中:

-assumenosideeffects class android.util.Log {
  public static *** v(...);
  public static *** d(...);
  public static *** i(...);
  public static *** w(...);
  public static *** e(...);
}
-ignorewarnings

它对我有用。

In app build.gradle file set:

release {
    minifyEnabled true
     ……
}

In proguard-rules.pro put:

-assumenosideeffects class android.util.Log {
  public static *** v(...);
  public static *** d(...);
  public static *** i(...);
  public static *** w(...);
  public static *** e(...);
}
-ignorewarnings

It worked for me.

御弟哥哥 2024-11-06 15:53:53

我通常会这样做:

if (BuildConfig.DEBUG) Log.i(TAG, msg);

不过,如果你有很多依赖项(库)并且它们写得不好,那么就使用 https://stackoverflow.com/a/5553290/4548520

使行更短:

private final static boolean DEBUG = BuildConfig.DEBUG;

if (DEBUG) Log.i(TAG, msg_1);

if (DEBUG) Log.e(TAG, msg_error_2);

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:

private final static boolean DEBUG = BuildConfig.DEBUG;

if (DEBUG) Log.i(TAG, msg_1);

if (DEBUG) Log.e(TAG, msg_error_2);
灯下孤影 2024-11-06 15:53:53

除了将 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.

°如果伤别离去 2024-11-06 15:53:53

您不需要枚举类的所有方法,看起来您也可以简单地使用 简写:

-assumenosideeffects class a.b.c.DebugLogs {
    <methods>;
}

为我工作,使用 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 :

-assumenosideeffects class a.b.c.DebugLogs {
    <methods>;
}

Worked for me with com.android.tools.build:gradle version 4.0.0.

烈酒灼喉 2024-11-06 15:53:53

您可以将 debuggable false 放在 buildTypes 版本上。

buildTypes {

     release {
        debuggable false
        ...
     }

}

You can put debuggable false on buildTypes release.

buildTypes {

     release {
        debuggable false
        ...
     }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文