Android 相关启动执行流程
init:
- 挂载和创建系统文件
- 解析rc文件
- 进入无限循环
- 执行action -- zygote启动
- 检测并重启进程
- 接收子进程的SIGCHLD信号,执行响应的方法
zygote:
- native
- 初始化Android运行环境
- 注册jni
- 执行ZygoteInit.main 方法 进入Java层
- Java
- 创建 socket
- 执行预加载
- 通过fork创建 SystemServer 进程
- 进入循环:等待AMS的通知,创建对应的进程
SystemServer
nice-name 给进程的名称
pid = Zygote.forkSystemServer
//启动 SystemServer.main
return handleSystemServerProcess(parsedArgs);
//开启Binder线程池
ZygoteInit.nativeZygoteInit();
gCurRuntime->onZygoteInit();
sp<ProcessState> proc = ProcessState::self();
proc->startThreadPool();
// 运行 SystemServer.main
RuntimeInit.applicationInit
// 通过反射执行 SystemServer.main
mMethod.invoke(null, new Object[] { mArgs });
SystemServer进程:
-
createSystemContext();
//执行systemMain()@ActivityThread,在systemMain方法中- new ActivityThread(),执行attach()@ActivityThread
//在attach方法中 - new Instrumentation(),
- ContextImpl context = ContextImpl.createAppContext(
this, getSystemContext().mPackageInfo); - Application mInitialApplication = context.mPackageInfo.makeApplication(true, null);
- new ActivityThread(),执行attach()@ActivityThread
-
mSystemServiceManager = new SystemServiceManager(mSystemContext); // 系统服务
- startService --> service.onStart(); // 继承自 SystemService 的服务
-
startBootstrapServices(t); // 引导服务 --AMS
//android 10开始 多了个 ATMS(ActivityTaskManagerService)-
mService = new ActivityTaskManagerService(context);
mService.start();
ServiceManager.addService(name, service,resumeTopActivityInnerLocked allowIsolated, dumpPriority);
-
创建 AMS -- 其他三个服务,持有 ATMS
mService = new ActivityManagerService(context, sAtm);mService.start();
-
-
startCoreServices(t); // 核心服务
-
startOtherServices(t); // 其他服务 -- WMS
mActivityManagerService.systemReady // 启动Launcher
startSystemUi(context, windowManagerF); // 启动 SystemUI
mActivityManagerService.systemReady -->
mActivityTaskManager.onSystemReady@ActivityTaskManagerService -->
mAtmInternal.resumeTopActivities@ActivityTaskManagerService -->
mRootWindowContainer.resumeFocusedStacksTopActivities()@RootWindowContainer -->
resumeFocusedStacksTopActivities@RootWindowContainer -->
targetStack.resumeTopActivityUncheckedLocked@ActivityStack
--> resumeTopActivityInnerLocked@ActivityStack -->
resumeNextFocusableActivityWhenStackIsEmpty@ActivityStack
--> mRootWindowContainer.resumeHomeActivity@RootWindowContainer -->
startHomeOnTaskDisplayArea@RootWindowContainer -->
resolveHomeActivity@RootWindowContainer -->
mService.getActivityStartController().startHomeActivity@ActivityStartController obtainStarter@ActivityStartController -->
execute@ActivityStarter -->
executeRequest@ActivityStarter -->
startActivityInner@ActivityStarter
这里调用了startActivity()方法来启动Launcher,最终会进入Launcher的onCreate()方法,Launcher启动完成。
mSystemServiceManager.startBootPhase // 状态设置
SystemServiceManager:管理服务的生命周期
ServerManager:管理Binder服务的
ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
ServiceManager.addService(name, service, allowIsolated, dumpPriority); // 注册Binder服务
Activity启动流程:
startActivity@Activity --> startActivityForResult@Activity
--> mInstrumentation.execStartActivity@Instrumentation -->
ActivityTaskManager.getService().startActivity@ActivityTaskManagerService
--> startActivityAsUser@ActivityTaskManagerService
--> execute@ActivityStarter -->
executeRequest@ActivityStarter -->
startActivityUnchecked@ActivityStarter -->
startActivityInner@ActivityStarter -->
resumeFocusedStacksTopActivities@RootWindowContainer -->
resumeTopActivityUncheckedLocked@ActivityStack -->
resumeTopActivityInnerLocked@ActivityStack -->
startSpecificActivity@ActivityStackSupervisor -->
realStartActivityLocked@ActivityStackSupervisor -->
scheduleTransaction@ClientLifecycleManager -->
schedule@ClientTransaction -->
scheduleTransaction@ApplicationThread -->
scheduleTransaction@Activity -->
execute@TransactionExecutor -->
executeCallbacks@TransactionExecutor -->
execute@LaunchActivityItem -->
handleLaunchActivity@ActivityThread -->
performLaunchActivity@ActivityThread -->
createBaseContextForActivity@ActivityThread 、mInstrumentation.newActivity、makeApplication@LoadApk -->
attach@Activity、callActivityOnCreate@Instrumentation -->
onCreate@Activity
小结
- 调用从system_server进程传过来的ClientTransaction.CallBack类中的execute方法,其继续调用ActivityThread.handleLaunchActivity方法来启动Activity。
- ActivityThread.handleLaunchActivity调用performLaunchActivity方法启动Activity
- performLaunchActivity先获取启动Activity的ActivityInfo,而后创建一个LoadedApk和上下文ContextImpl。在通过反射去创建此Activity对象,并执行Activity的onCreate方法。
SystemServer执行流程图:
image.png
Activity启动:
image.png
Android 启动流程时序图:
image.png
Activity生命周期管理类图:
image.png
下图是ATMS处理startActivity过程,并回调启动进程的ApplicationThread:
22650779-eabf22046e2f33d4.png
下图为Android 30 之后,ActivityThread启动Activity过程的时序图:
22650779-72d2d3d9acda317d.png