gpt4 book ai didi

android - 日志写入过多会降低 Android 应用程序性能吗?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:56:03 25 4
gpt4 key购买 nike

我想知道日志记录是否会降低应用程序性能?另外,请给我一些提高 Android 应用程序性能的技巧。

最佳答案

是的。过多的日志记录会影响任何应用程序的性能,而不仅仅是 Android。

developer guide建议您在发布前停用和禁用日志记录:

Turn off logging and debugging Make sure you deactivate logging and disable the debugging option before you build your application for release. You can deactivate logging by removing calls to Log methods in your source files. You can disable debugging by removing the android:debuggable attribute from the tag in your manifest file, or by setting the android:debuggable attribute to false in your manifest file. Also, remove any log files or static test files that were created in your project.

Also, you should remove all Debug tracing calls that you added to your code, such as startMethodTracing() and stopMethodTracing() method calls.

因此,您应该在应用的“发布”或“生产”版本中禁止显示日志。

通过设置 debuggable 在 Android Manifest 中关闭它:

<application android:icon="@drawable/icon" 
android:label="@string/app_name"
android:debuggable="false">

另一种方式

创建您自己的记录器类并在执行日志之前检查 Debug模式。它允许在 Debug模式和部署的应用程序之间进行单点修改,并允许执行额外的操作,例如写入日志文件。

import android.util.Log;
public class MyLog {
private static final boolean isDebug = false;;
public static void i(String tag, String msg) {
if (isDebug) {
Log.i(tag, msg);
}
}
public static void e(String tag, String msg) {
if (isDebug) {
Log.e(tag, msg);
}
}
}


更多信息请阅读 http://rxwen.blogspot.com/2009/11/logging-in-android.html

和 SO QA:

Log.d and impact on performance

Android Logging - How to clear for better performance

关于android - 日志写入过多会降低 Android 应用程序性能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com