根据谷歌,我必须在发布我的Android应用程序谷歌播放之前“停用源代码中的Log方法的任何调用”。出版清单第3节节选:

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

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

if(condition)
  Log.d(LOG_TAG, "Something");
data.load();
data.show();

如果注释Log行,则该条件将应用于下一行,并且可能不会调用load()。这样的情况是否罕见到我可以决定它不应该存在?

那么,是否有更好的源代码级方法来做到这一点呢?或者是一些聪明的ProGuard语法,有效但安全地删除所有Log行?


当前回答

最简单的方法;

使用DebugLog

当应用程序发布时,DebugLog会禁用所有日志。

https://github.com/MustafaFerhan/DebugLog

其他回答

ProGuard将在你的发布版本中为你做这件事,现在来自android.com的好消息是:

http://developer.android.com/tools/help/proguard.html

ProGuard工具通过删除未使用的代码和用语义模糊的名称重命名类、字段和方法来缩小、优化和混淆代码。结果是一个更小的.apk文件,更难以进行反向工程。由于ProGuard使应用程序更难进行反向工程,因此当应用程序使用对安全性敏感的特性时,例如在应用程序授权时,使用它是很重要的。

ProGuard集成到Android构建系统中,所以你不需要手动调用它。ProGuard仅在以发布模式构建应用程序时运行,因此在以调试模式构建应用程序时不必处理混淆的代码。运行ProGuard是完全可选的,但强烈推荐。

本文档介绍如何启用和配置ProGuard,以及如何使用retrace工具解码混淆的堆栈跟踪

我喜欢使用Log。d(TAG,一些字符串,通常是字符串。格式())。

TAG总是类名

改变日志。d(TAG,——> Logd(在类的文本中

private void Logd(String str){
    if (MainClass.debug) Log.d(className, str);
}

通过这种方式,当您准备发布版本时,将MainClass.debug设置为false!

如果您不想弄乱额外的库或手动编辑代码,以下是我的解决方案。我创建了这个Jupyter笔记本来检查所有的java文件并注释掉所有的日志消息。虽然不完美,但它帮我完成了任务。

我对上述解决方案进行了改进,提供了对不同日志级别的支持,并根据代码是在活动设备上运行还是在模拟器上运行而自动更改日志级别。

public class Log {

final static int WARN = 1;
final static int INFO = 2;
final static int DEBUG = 3;
final static int VERB = 4;

static int LOG_LEVEL;

static
{
    if ("google_sdk".equals(Build.PRODUCT) || "sdk".equals(Build.PRODUCT)) {
        LOG_LEVEL = VERB;
    } else {
        LOG_LEVEL = INFO;
    }

}


/**
 *Error
 */
public static void e(String tag, String string)
{
        android.util.Log.e(tag, string);
}

/**
 * Warn
 */
public static void w(String tag, String string)
{
        android.util.Log.w(tag, string);
}

/**
 * Info
 */
public static void i(String tag, String string)
{
    if(LOG_LEVEL >= INFO)
    {
        android.util.Log.i(tag, string);
    }
}

/**
 * Debug
 */
public static void d(String tag, String string)
{
    if(LOG_LEVEL >= DEBUG)
    {
        android.util.Log.d(tag, string);
    }
}

/**
 * Verbose
 */
public static void v(String tag, String string)
{
    if(LOG_LEVEL >= VERB)
    {
        android.util.Log.v(tag, string);
    }
}


}

Per android.util.Log提供了一种启用/禁用日志的方法:

public static native boolean isLoggable(String tag, int level);

默认情况下,isLoggable(…)方法返回false,只有在你在设备中设置prop后,像这样:

adb shell setprop log.tag.MyAppTag DEBUG

这意味着DEBUG级别以上的任何日志都可以打印出来。参考android文档:

Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: 'setprop log.tag. ' Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: 'log.tag.=' and place that in /data/local.prop.

所以我们可以使用自定义log util:

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

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

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

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

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