根据谷歌,我必须在发布我的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

其他回答

我知道这是个老问题了,但你为什么不把所有的日志调用都换成 布尔logCallWasHere = true;//——剩下的日志在这里

这就是为什么你会知道什么时候你想把它们放回去,他们不会影响你的if语句调用:)

我会考虑使用roboguice的日志记录工具,而不是内置的android.util.Log

它们的功能会自动禁用发布版本的调试和详细日志。 此外,您还可以免费获得一些漂亮的功能(例如,可定制的日志记录行为,每个日志的额外数据等等)

使用proguard可能是相当麻烦的,除非您有一个很好的理由(禁用日志不是一个好理由),否则我不会经历配置和使其与应用程序一起工作的麻烦。

我发现一个更简单的解决方案是忘记所有的if检查,只是使用ProGuard剥离任何Log.d()或Log.v()方法调用时,我们调用我们的Ant发布目标。

通过这种方式,我们总是可以输出常规构建的调试信息,而不必对发布构建进行任何代码更改。ProGuard还可以对字节码进行多次传递,以删除其他不需要的语句和空块,并可以在适当的地方自动内联短方法。

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

-dontskipnonpubliclibraryclasses
-dontobfuscate
-forceprocessing
-optimizationpasses 5

-keep class * extends android.app.Activity
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

因此,您可以将其保存到一个文件中,然后从Ant调用ProGuard,传入刚刚编译的JAR和正在使用的Android平台JAR。

请参见ProGuard手册中的示例。


更新(4.5年后):现在我使用Timber进行Android日志记录。

它不仅比默认的Log实现更好一些(日志标记是自动设置的,很容易记录格式化的字符串和异常),而且您还可以在运行时指定不同的日志行为。

在这个例子中,日志语句只会在我的应用程序的调试版本中写入logcat:

木材是在我的Application onCreate()方法中设置的:

if (BuildConfig.DEBUG) {
  Timber.plant(new Timber.DebugTree());
}

然后在我的代码中的其他地方,我可以很容易地记录:

Timber.d("Downloading URL: %s", url);
try {
  // ...
} catch (IOException ioe) {
  Timber.e(ioe, "Bad things happened!");
}

请参阅Timber示例应用程序以获得更高级的示例,其中所有日志语句都在开发期间发送到logcat,在生产中,没有调试语句被记录,但错误会无声地报告给Crashlytics。

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

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

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


}