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

时间:2021-01-09 14:02:06

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.

在向市场发布应用程序之前,先关掉我自己的应用程序的LogCat输出,这很简单。我还知道如何通过标记和/或id选择性地过滤LogCat消息,以便自己调试方便。

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.

但是现在我感兴趣的是一件可能要困难得多的事情(也许不可能?):禁用所有LogCat输出,包括&尤其是那些来自第三方服务的,比如TtsService、GoogleLoginService等等。

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?

进一步澄清:我对自己过滤信息不感兴趣。我很有兴趣为任何从Android市场下载我的应用的人禁用第三方消息。这是可能的吗?

6 个解决方案

#1


180  

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.

您可以使用ProGuard完全删除不使用返回值的任何行,通过告诉ProGuard假定不会出现问题。

The following proguard.cfg chunk instructs to remove Log.d, Log.v and Log.i calls.

以下混淆器。cfg块指示删除日志。d,日志。v和日志。我的电话。

-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.

最终的结果是这些日志行不在您的发行版apk中,因此任何使用logcat的用户都不会看到d/v/i日志。

#2


10  

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

如果不使用proguard,则必须自己管理日志,并在manifest文件中创建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);
    }
}

#3


1  

The great answer provided by David Caunt doesn't seem to work for the rules defined in proguard-android-optimize.txt.

David Caunt提供的伟大答案似乎并不适用于proguard-android- optimization .txt中定义的规则。

Instead of using the wildcard ***, current versions of ProGuard seem to expect the return parameter's type qualifier:

与使用通配符*** *不同,当前版本的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(...);
}

#4


0  

You can put debuggable false on buildTypes release.

您可以在buildtype发布中放置可调试的false。

buildTypes {

     release {
        debuggable false
        ...
     }

}

#5


0  

In app build.gradle file set:

在应用程序构建。gradle文件设置:

release {
    minifyEnabled true
     ……
}

In proguard-rules.pro put:

在proguard-rules。亲说:

-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.

它为我工作。

#6


0  

I usually do next:

我通常做下:

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

如果BuildConfig.DEBUG日志。我(标签,味精);

though if you have a lot of dependencies (libraries) and they written bad then yeah just use https://*.com/a/5553290/4548520

虽然如果您有很多依赖项(库)并且它们写得很糟糕,那么请使用https://*.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);

#1


180  

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.

您可以使用ProGuard完全删除不使用返回值的任何行,通过告诉ProGuard假定不会出现问题。

The following proguard.cfg chunk instructs to remove Log.d, Log.v and Log.i calls.

以下混淆器。cfg块指示删除日志。d,日志。v和日志。我的电话。

-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.

最终的结果是这些日志行不在您的发行版apk中,因此任何使用logcat的用户都不会看到d/v/i日志。

#2


10  

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

如果不使用proguard,则必须自己管理日志,并在manifest文件中创建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);
    }
}

#3


1  

The great answer provided by David Caunt doesn't seem to work for the rules defined in proguard-android-optimize.txt.

David Caunt提供的伟大答案似乎并不适用于proguard-android- optimization .txt中定义的规则。

Instead of using the wildcard ***, current versions of ProGuard seem to expect the return parameter's type qualifier:

与使用通配符*** *不同,当前版本的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(...);
}

#4


0  

You can put debuggable false on buildTypes release.

您可以在buildtype发布中放置可调试的false。

buildTypes {

     release {
        debuggable false
        ...
     }

}

#5


0  

In app build.gradle file set:

在应用程序构建。gradle文件设置:

release {
    minifyEnabled true
     ……
}

In proguard-rules.pro put:

在proguard-rules。亲说:

-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.

它为我工作。

#6


0  

I usually do next:

我通常做下:

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

如果BuildConfig.DEBUG日志。我(标签,味精);

though if you have a lot of dependencies (libraries) and they written bad then yeah just use https://*.com/a/5553290/4548520

虽然如果您有很多依赖项(库)并且它们写得很糟糕,那么请使用https://*.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);