Android的调试日志真的在运行时剥离吗?

前端之家收集整理的这篇文章主要介绍了Android的调试日志真的在运行时剥离吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Android文档( http://developer.android.com/reference/android/util/Log.html)说:

Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error,warning and info logs are always kept

我刚刚做了一个测试.在我的活动中,我写道:

private static String test(String what) {
    Log.e("test","I am called with argument: " + what);
    return what;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v("test","log level: " + test("v"));
    Log.d("test","log level: " + test("d"));
    Log.i("test","log level: " + test("i"));
    Log.w("test","log level: " + test("w"));
    Log.e("test","log level: " + test("e"));
}

我将我的项目导出为一个apk文件,然后我在手机上安装了这个apk.我在手机上运行这个应用程序,然后我看了日志.在那里我看到函数测试被全部五次调用,并且对Log.something函数的所有五个调用导致其文本被写入日志.

那么Log.d调用在运行时是否真的被剥离?

解决方法

这个问题报道了 here,并于2012年3月在 ADT 17.0.0提供了一个解决方

Added a feature that allows you to run some code only in debug mode. Builds now generate a class called BuildConfig containing a DEBUG constant that is automatically set according to your build type. You can check the (BuildConfig.DEBUG) constant in your code to run debug-only functions.

猜你在找的Android相关文章