private static final boolean IS_DEBUG_BUILD = Build.TYPE.equals("eng") || Build.TYPE.equals("userdebug");
分析异常时往往需要关注的重要目录:
/data/anr/traces.txt
/data/tombstones/tombstone_X
/data/system/dropbox/
Log.i("LogTag","------------", new Exception()); 人为抛出异常 打印运行堆栈,此命令适用于源码编译中,但在APP中使用就会报错,APP中可以这样使用:
Log.i("LogTag","------------",“ ---”+new Exception());
Thread.currentThread().dumpStack(); //方法1
Log.d(TAG,"Gityuan", new RuntimeException("Gityuan")); //方法2
new RuntimeException("Gityuan").printStackTrace(); //方法3
MTK的mtkloger:
进入应用暗码 : *#*#3646633#*#*
log的保存路径 /storage/emulated/0/mtklog
如部分平板系统预装拨号盘如何打开MTKlog
adb shell am start -n com.mediatek.mtklogger/com.mediatek.mtklogger.MainActivity
mtklog 的分类:MDLog、Mobile Log、Network Log,可能出现的aee_exp log
MD log:medom 相关底层的log
Mobile Log:主要是Android log 和kernel log
Network log:网络相关log
aee_exp log :crash ANR 重启相关的log输出
对应log文件名称为:
crash_log :崩溃日志,主要输出 程序崩溃造成的crash log
events_log:事件日志,主要输出记录各个activity周期及事件
kernel_log:底层驱动,按键,低内存相关log
sys_log:系统日志,Exception定位点
radio_log:输出通话,网络状态变化
main_log:详尽输出每一步的log
参考原文 : https://blog.csdn.net/weixin_33473519/article/details/53434800?utm_source=blogxgwz
https://rayleeya.iteye.com/blog/1955652 此文对android的anr问题有详细说明
ANR,是“Application Not Responding”的缩写,即“应用程序无响应”。在Android中,ActivityManagerService(简称AMS)和WindowManagerService(简称WMS)会监测应用程序的响应时间,如果应用程序主线程(即UI线程)在超时时间内对输入事件没有处理完毕,或者对特定操作没有执行完毕,就会出现ANR。对于输入事件没有处理完毕产生的ANR,Android会显示一个对话框,提示用户当前应用程序没有响应,用户可以选择继续等待或者关闭这个应用程序(也就是杀掉这个应用程序的进程)。
1:关键字搜索:
FATAL,leaked, block,Pid id XXX,crash,DALVIK THREADS, OOM,unfinished died
查找应用包名--快速定位ANR
The application may be doing too much work on its main thread.
AndroidRuntime(xxxx): Shutting down VM
线程的状态是native则表示当前正在执行jni,
聪明的你肯定已经知道,之所以trace信息会输出到“/data/anr/traces.txt”文件中,就是系统属性“dalvik.vm.stack-trace-file”设置的。你可以通过在设备的shell中使用setprop和getprop对系统属性进行设置和读取:
getprop dalvik.vm.stack-trace-file
setprop dalvik.vm.stack-trace-file /tmp/stack-traces.txt
此 Dialog的java位置:
frameworks\base\services\core\java\com\android\server\am\AppNotRespondingDialog.java
alps\frameworks\base\services\core\java\com\android\server\am\AppErrors.java ::appNotResponding()
此处发送Handler message为 SHOW_NOT_RESPONDING_UI_MSG
// Set the app's notResponding state, and look up the errorReportReceiver
makeAppNotRespondingLocked(app,
activity != null ? activity.shortComponentName : null,
annotation != null ? "ANR " + annotation : "ANR",
info.toString());
// Bring up the infamous App Not Responding dialog
Message msg = Message.obtain();
msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;
msg.obj = new AppNotRespondingDialog.Data(app, activity, aboveSystem);
mService.mUiHandler.sendMessage(msg);
接着会在ActivityManagerService.java类中接收此Message并对应处理
alps\frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java
final class UiHandler extends Handler {
public UiHandler() {
super(com.android.server.UiThread.get().getLooper(), null, true);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_ERROR_UI_MSG: {
mAppErrors.handleShowAppErrorUi(msg);
ensureBootCompleted();
} break;
case SHOW_NOT_RESPONDING_UI_MSG: {
mAppErrors.handleShowAnrUi(msg);
ensureBootCompleted();
} break;
在AppError.java中的handleShowAppErrorUI()
if (mService.canShowErrorDialogs() || showBackground) {
dialogToShow = new AppNotRespondingDialog(mService, mContext, data);
proc.anrDialog = dialogToShow;
} else {
MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,
AppNotRespondingDialog.CANT_SHOW);
// Just kill the app if there is no dialog to be shown.
mService.killAppAtUsersRequest(proc, null);
}
}
// If we've created a crash dialog, show it without the lock held
if (dialogToShow != null) {
dialogToShow.show();
}
两个标签同时过滤adb 命令: adb shell "logcat |grep -e ActivityManager: -e WindowManager:"
下面的打印为了log信息减少,所以分别打印方便查阅 ,
C:\Users\User>adb logcat |findstr "ActivityManager" \\点击Launcher中图标 打开APP DeskClock
08-12 06:35:09.605 1043 8961 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.deskclock/.DeskClock bnds=[795,131][990
,423]} from uid 10015
08-12 06:35:09.703 1043 8961 V WindowManager: Changing focus from Window{cfe72a u0 com.android.launcher3/com.android.launcher3.Launcher} to null Callers=com.android.server.wm.WindowManagerService.se
tFocusedApp:2739 com.android.server.am.ActivityManagerService.setResumedActivityUncheckLocked:3325 com.android.server.am.ActivityStack.setResumedActivityLocked:2312 com.android.server.am.ActivityStack
.minimalResumeActivityLocked:1115
08-12 06:35:09.890 1043 1121 I ActivityManager: Displayed com.android.deskclock/.DeskClock: +201ms
--------------------------------------------------------------------------------
C:\Users\User>adb logcat -c|findstr "ActivityManager"
C:\Users\User>adb logcat |findstr "ActivityManager" \\退出APP
08-12 06:35:29.715 1043 1078 V WindowManager: Changing focus from Window{4cebd29 u0 com.android.deskclock/com.android.deskclock.DeskClock} to null Callers=com.android.server.wm.WindowManagerService.
setFocusedApp:2739 com.android.server.am.ActivityManagerService.setResumedActivityUncheckLocked:3325 com.android.server.am.ActivityStack.setResumedActivityLocked:2312 com.android.server.am.ActivitySta
ck.resumeTopActivityInnerLocked:2694
User>adb logcat -c |findstr "WindowManager:"
User>adb logcat |findstr "WindowManager:" //点击Launcher中图标 打开APP:DeskClock
21:06.149 1043 1315 V WindowManager: Set focused app to: AppWindowToken{24e89b8 token=Token{b72727a ActivityRecord{5decea5 u0 com.android.deskclock/.DeskClock t43}}} old focus=AppWindowToke
token=Token{64857d6 ActivityRecord{eb9b98 u0 com.android.launcher3/.Launcher t33}}} moveFocusNow=true
21:06.149 1043 1315 V WindowManager: findFocusedWindow: No focusable windows.
21:06.149 1043 1315 V WindowManager: Changing focus from Window{cfe72a u0 com.android.launcher3/com.android.launcher3.Launcher} to null Callers=com.android.server.wm.WindowManagerService.se
pp:2739 com.android.server.am.ActivityManagerService.setResumedActivityUncheckLocked:3325 com.android.server.am.ActivityStack.setResumedActivityLocked:2312 com.android.server.am.ActivityStack
esumeActivityLocked:1115
21:06.149 1043 1315 D WindowManager: Input focus has changed to null
21:06.152 1043 1315 V WindowManager: findFocusedWindow: No focusable windows.
21:06.160 1043 6439 V WindowManager: findFocusedWindow: No focusable windows.
21:06.481 1043 2119 I WindowManager: Relayout Window{c7e57f5 u0 com.android.deskclock/com.android.deskclock.DeskClock}: oldVis=4 newVis=0 focusMayChange = true
21:06.487 1043 2119 V WindowManager: Changing focus from null to Window{c7e57f5 u0 com.android.deskclock/com.android.deskclock.DeskClock} Callers=com.android.server.wm.WindowManagerService.
indow:2179 com.android.server.wm.Session.relayout:235 android.view.IWindowSession$Stub.onTransact:286 com.android.server.wm.Session.onTransact:162
21:06.487 1043 2119 D WindowManager: Input focus has changed to Window{c7e57f5 u0 com.android.deskclock/com.android.deskclock.DeskClock}
-------------------------------------------------------------------------------------
User>adb logcat -c|findstr "WindowManager:"
User>adb logcat |findstr "WindowManager:" //退出APP:DeskClock 到Launcher
21:27.964 1043 1226 D WindowManager: interceptKeyTi keyCode=4 down=false repeatCount=0 keyguardOn=false mHomePressed=false canceled=false metaState:0
21:27.984 1043 6439 V WindowManager: Set focused app to: AppWindowToken{fb1c0e3 token=Token{64857d6 ActivityRecord{eb9b98 u0 com.android.launcher3/.Launcher t33}}} old focus=AppWindowToken{
oken=Token{b72727a ActivityRecord{5decea5 u0 com.android.deskclock/.DeskClock t43}}} moveFocusNow=true
21:27.984 1043 6439 V WindowManager: findFocusedWindow: No focusable windows.
21:27.985 1043 6439 V WindowManager: Changing focus from Window{c7e57f5 u0 com.android.deskclock/com.android.deskclock.DeskClock} to null Callers=com.android.server.wm.WindowManagerService.
dApp:2739 com.android.server.am.ActivityManagerService.setResumedActivityUncheckLocked:3325 com.android.server.am.ActivityStack.setResumedActivityLocked:2312 com.android.server.am.ActivitySta
TopActivityInnerLocked:2694
21:27.985 1043 6439 D WindowManager: Input focus has changed to null
21:28.002 1043 1315 V WindowManager: findFocusedWindow: No focusable windows.
21:28.044 1043 6439 I WindowManager: Relayout Window{cfe72a u0 com.android.launcher3/com.android.launcher3.Launcher}: oldVis=4 newVis=0 focusMayChange = true
21:28.048 1043 6439 V WindowManager: Changing focus from null to Window{cfe72a u0 com.android.launcher3/com.android.launcher3.Launcher} Callers=com.android.server.wm.WindowManagerService.re
dow:2179 com.android.server.wm.Session.relayout:235 android.view.IWindowSession$Stub.onTransact:286 com.android.server.wm.Session.onTransact:162
21:28.048 1043 6439 D WindowManager: Input focus has changed to Window{cfe72a u0 com.android.launcher3/com.android.launcher3.Launcher}