扩展:App在发生Crash的时候,是如何打印日志,如何退出的?
App启动时,Android会通过zygote进程fork一个进程,然后执行初始化工作。在初始化的过程中,会执行RuntimeInit.commonInit();
中的方法:
// /frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
protected static final void commonInit() {
if (DEBUG) Slog.d(TAG, “Entered RuntimeInit!”);
/*
- set handlers; these apply to all threads in the VM. Apps can replace
- the default handler, but not the pre handler.
*/
LoggingHandler loggingHandler = new LoggingHandler();
Thread.setUncaughtExceptionPreHandler(loggingHandler);
Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler));
// …略
}
在上面的代码中我们可以发现,Android已经为App设置了默认的UncaughtExceptionHandler
进行异常处理:
LoggingHandler
:用于处理打印日志。KillApplicationHandler
:调用AMS,弹出“应用异常”的dialog,然后结束进程并退出等等。
2.2 分发
当发生未捕获的异常(没有被try-catch
的异常)时,虚拟机会调用当前线程Thread的dispatchUncaughtException
方法,进行异常分发。
// art/runtime/thread.cc
void Thread::HandleUncaughtExceptions(ScopedObjectAccessAlreadyRunnable& soa) {
if (!IsExceptionPending()) {
return;
}
ScopedLocalRef peer(tlsPtr_.jni_env, soa.AddLocalReference(tlsPtr_.opeer));
ScopedThreadStateChange tsc(this, kNative);
// Get and clear the exception.
ScopedLocalRef exception(tlsPtr_.jni_env, tlsPtr_.jni_env->ExceptionOccurred());
tlsPtr_.jni_env->ExceptionClear();
// 调用当前线程的dispatchUncaughtException方法进行分发
// Call the Thread instance’s dispatchUncaughtException(Throwable)
tlsPtr_.jni_env->CallVoidMethod(peer.get(),
WellKnownClasses::java_lang_Thread_dispatchUncaughtException,
exception.get());
// If the dispatchUncaughtException threw, clear that exception too.
tlsPtr_.jni_env->ExceptionClear();
}
所以,Thread的dispatchUncaughtException
负责处理异常的分发逻辑。
2.2.1 分发过程
- 首先处理
setUncaughtExceptionPreHandler()
方法注册的异常处理Handler —— 这里的是LoggingHandler
,负责记录日志(默认,不可修改过)
// java/lang/Thread.java
public final void dispatchUncaughtException(Throwable e) {
// 上面进行打印日志
// BEGIN Android-added: uncaughtExceptionPreHandler for use by platform.
Thread.UncaughtExceptionHandler initialUeh =
Thread.getUncaughtExceptionPreHandler();
if (initialUeh != null) {
try {
initialUeh.uncaughtException(this, e);
} catch (RuntimeException | Error ignored) {
// Throwables thrown by the initial handler are ignored
}
}
// END Android-added: uncaughtExceptionPreHandler for use by platform.
// 然后在这里进行分发和处理
getUncaughtExceptionHandler().uncaughtException(this, e);
}
- 然后处理线程私有的
uncaughtExceptionHandler
异常处理方法;如果私有Handler不存在,则使用ThreadGroup
的异常处理方法;
// java/lang/Thread.java
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
return uncaughtExceptionHandler != null ?
uncaughtExceptionHandler : group;
}
ThreadGroup
中,优先调用父线程组的处理逻辑,否则,调用setDefaultUncaughtExceptionHandler() 方法注册的Handler处理异常。
// java/lang/ThreadGroup.java
public void uncaughtException(Thread t, Throwable e) {
if (parent != null) {
parent.uncaughtException(t, e);
} else {
Thread.UncaughtExceptionHandler ueh = Thread.getDefaultUncaughtExceptionHandler();
if (ueh != null) {
ueh.uncaughtException(t, e);
} else if (!(e instanceof ThreadDeath)) {
System.err.print("Exception in thread “” + t.getName() + “” ");
e.printStackTrace(System.err);
}
}
}
流程图如下:
2.3 处理
Android已经为App设置了默认的UncaughtExceptionHandler
进行异常处理:
LoggingHandler
:用于打印日志。KillApplicationHandler
:调用AMS,弹出“应用异常”的dialog,结束进程并退出等等。
关键代码如下:
// /frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
private static class KillApplicationHandler implements Thread.UncaughtExceptionHandler {
// …略
@Override
public void uncaughtException(Thread t, Throwable e) {
try {
// …略
// 调用AMS,弹出crash dialog,详细过程可以去ActivityManagerService.java中去看
// 但是现在有很多厂商在系统层屏蔽了这个页面,因此表现为闪退。(反正自己写的demo没弹出这个dialog)
// Bring up crash dialog, wait for it to be dismissed
ActivityManager.getService().handleApplicationCrash(
mApplicationObject, new ApplicationErrorReport.ParcelableCrashInfo(e));
} catch (Throwable t2) {
// …略
} finally {
// 杀死进程并退出
// Try everything to make sure this process goes away.
Process.kil
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
lProcess(Process.myPid());
System.exit(10);
}
}
}
系统默认异常处理逻辑在KillApplicationHandler
类的uncaughtException()
方法中,系统默认Crash弹框等逻辑是通过AMS的handleApplicationCrash()
方法执行的。
3、FAQ
3.1 设置了Thread.setDefaultUncaughtExceptionHandler
之后,APP就不会崩溃了吗
设置UncaughtExceptionHandler
不等于给App穿上大号的try-catch
。
UncaughtExceptionHandler
只是一种提醒开发者发生了未捕获的异常的一种手段。开发者可以在此基础上做一些记录和上报的动作等等。但并不能当什么事情也没有发生一样,让程序继续执行。
以Android主线程为例。Android主线程本质上就是一个由Looper开启的死循环,然后不断的处理MessageQueue中的Message。
这个模型可以简化为:
public static void loop() {
for (;😉 {
// 取Msg,阻塞调用
Message msg = queue.next();
if (msg == null) {
// 结束退出
return;
}
// 分发处理
msg.target.dispatchMessage(msg);
}
}
public static void loop() {
for (;😉 {
// 取Msg,阻塞调用
Message msg = queue.next();
if (msg == null) {
// 结束退出
return;
}
// 分发处理
msg.target.dispatchMessage(msg);
}
}