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

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

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

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

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

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


当前回答

如果你可以运行全局替换(一次),然后保留一些编码惯例,你可以遵循Android框架中经常使用的模式。

而不是写作

Log.d(TAG, string1 + string2 + arg3.toString());

把它当作

if (BuildConfig.DEBUG) Log.d(TAG, string1 + String.format("%.2f", arg2) + arg3.toString());

现在proguard可以从优化版的DEX中删除StringBuilder和它使用的所有字符串和方法。使用proguard-android- optimization .txt,你就不用担心android.util了。

android {
  …
  buildTypes {
    release {
      minifyEnabled true
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
}

与Android Studio gradle插件,BuildConfig。DEBUG非常可靠,因此不需要额外的常数来控制剥离。

其他回答

正如zserge的评论所言,

Timber非常好,但如果你已经有一个现有的项目-你可以尝试github.com/zserge/log。它是android.util.Log的替代品,具有Timber的大部分功能,甚至更多。

他的日志库提供了简单的启用/禁用日志打印开关,如下所示。

此外,它只需要更改导入行,对于Log.d(…)不需要更改任何内容;声明。

if (!BuildConfig.DEBUG)
    Log.usePrinter(Log.ANDROID, false); // from now on Log.d etc do nothing and is likely to be optimized with JIT

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

我的方法:

1)启用列选择模式(alt+shift+insert)

2)选择一个日志。d(标签,“文本”);“日志”部分。

3)然后按shift + CTRL + Alt + j

4)点击左箭头

分离完成

6)点击删除。

这将一次性删除java文件中的所有LOG调用。

我发布的这个解决方案特别适用于Android Studio用户。我最近也发现了Timber,并通过以下方法成功地将其导入到我的应用程序中:

将最新版本的库放入build.gradle:

compile 'com.jakewharton.timber:timber:4.1.1'

然后在Android工作室,点击编辑->查找->替换路径…

输入Log。e(TAG,或任何您已经定义到“Text to find”文本框中的日志消息。然后你只需将其替换为timber。

单击“查找”,然后替换全部。

Android Studios现在将遍历项目中的所有文件,并将所有日志替换为木材。

我用这个方法遇到的唯一问题是,gradle之后会出现一百万个错误消息,因为它无法在每个java文件的导入中找到“Timber”。只需点击错误和Android工作室将自动导入“木材”到你的java。一旦你为所有的错误文件做了这些,gradle就会再次编译。

你还需要把这段代码放在你的Application类的onCreate方法中:

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

这将导致只有当你处于开发模式而不是生产模式时才会出现应用日志记录。你也可以使用BuildConfig。RELEASE表示登录RELEASE模式。

我在谷歌IO示例应用程序中使用了LogUtils类。我将其修改为使用特定于应用程序的DEBUG常量而不是BuildConfig。因为BuildConfig. DEBUG。DEBUG不可靠。然后在我的课程中,我有以下内容。

import static my.app.util.LogUtils.makeLogTag;
import static my.app.util.LogUtils.LOGV;

public class MyActivity extends FragmentActivity {
  private static final String TAG = makeLogTag(MyActivity.class);

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LOGV(TAG, "my message");
  }
}