App启动过程

启动过程概述

  • Launcher 接收到点击事件,获取应用信息,向ActivityManagerService(AMS)发起启动应用的请求(例子中启动calculator计算器)
  • AMS请求Launcher Pause(Launcher保存状态进入后台)
  • Launcher Pause,并向AMS发送Pause完毕的请求
  • AMS向Zygote进程请求启动一个新进程(calculator)(Note:通过Socket的方式)
  • Zygote fork出新进程(calculator),在新进程中执行ActivityThread类的main方法
  • calculator想AMS请求attach到AMS
  • AMS请求calculator lauch
  • calculator调用onCreate,onResume方法
App启动过程_第1张图片
App启动流程图

代码调用分析

Launcher 接收到点击事件向AMS发起启动应用的请求

ActivityManagerProxy.java的startActivity 函数打印的调用栈:

07-29 07:06:11.691  6231  6231 D zhibinw : java.lang.RuntimeException: ActivityManagerProxy-startActivity
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2409)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.app.Activity.startActivityForResult(Activity.java:3788)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.app.Activity.startActivity(Activity.java:4055)
07-29 07:06:11.691  6231  6231 D zhibinw :  at com.android.launcher3.Launcher.startActivity(Launcher.java:3314)
07-29 07:06:11.691  6231  6231 D zhibinw :  at com.android.launcher3.Launcher.startActivitySafely(Launcher.java:3338)
07-29 07:06:11.691  6231  6231 D zhibinw :  at com.android.launcher3.Launcher.startAppShortcutOrInfoActivity(Launcher.java:3108)
07-29 07:06:11.691  6231  6231 D zhibinw :  at com.android.launcher3.Launcher.onClickAppShortcut(Launcher.java:3067)
07-29 07:06:11.691  6231  6231 D zhibinw :  at com.android.launcher3.Launcher.onClick(Launcher.java:2895)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.view.View.performClick(View.java:4781)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.view.View$PerformClick.run(View.java:19874)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.os.Handler.handleCallback(Handler.java:739)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.os.Handler.dispatchMessage(Handler.java:95)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.os.Looper.loop(Looper.java:135)
07-29 07:06:11.691  6231  6231 D zhibinw :  at android.app.ActivityThread.main(ActivityThread.java:5258)
07-29 07:06:11.691  6231  6231 D zhibinw :  at java.lang.reflect.Method.invoke(Native Method)
07-29 07:06:11.691  6231  6231 D zhibinw :  at java.lang.reflect.Method.invoke(Method.java:372)
07-29 07:06:11.691  6231  6231 D zhibinw :  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
07-29 07:06:11.691  6231  6231 D zhibinw :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
  • Launcher进程启动的调用栈:
    ZygoteInit.main -> ActivityThread.main -> Looper.loop(进入消息循环)
  • 消息循环中收到系统分发过来的消息,回调onClick方法,调用startActivity方法
  • 通过ActivityManagerProxy代理对象将请求发送给AMS
AMS请求Launcher Pause

ApplicationThreadProxy 的 schedulePauseActivity 函数打印的调用栈:

07-29 07:06:11.922  4151  4183 D zhibinw : java.lang.RuntimeException: ApplicationThreadProxy-schedulePauseActivity
07-29 07:06:11.922  4151  4183 D zhibinw :  at android.app.ApplicationThreadProxy.schedulePauseActivity(ApplicationThreadNative.java:699)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStack.startPausingLocked(ActivityStack.java:842)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.pauseBackStacks(ActivityStackSupervisor.java:680)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:1638)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStack.resumeTopActivityLocked(ActivityStack.java:1477)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.resumeTopActivitiesLocked(ActivityStackSupervisor.java:2520)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:2125)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.startActivityUncheckedLocked(ActivityStackSupervisor.java:2258)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1560)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:994)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:3415)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:3402)
07-29 07:06:11.922  4151  4183 D zhibinw :  at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:140)
07-29 07:06:11.922  4151  4183 D zhibinw :  at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2223)
07-29 07:06:11.922  4151  4183 D zhibinw :  at android.os.Binder.execTransact(Binder.java:446)

在一系列调用之后,AMS进程向Launcher发送pause的请求,同样通过IPC。
在onPause方法中查看调用栈:

07-29 07:06:12.019  6231  6231 D zhibinw :  at com.android.launcher3.Launcher.onPause(Launcher.java:1237)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.Activity.performPause(Activity.java:6144)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1310)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3248)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3221)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3195)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.ActivityThread.access$1000(ActivityThread.java:151)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.os.Looper.loop(Looper.java:135)
07-29 07:06:12.019  6231  6231 D zhibinw :  at android.app.ActivityThread.main(ActivityThread.java:5258)
07-29 07:06:12.019  6231  6231 D zhibinw :  at java.lang.reflect.Method.invoke(Native Method)
07-29 07:06:12.019  6231  6231 D zhibinw :  at java.lang.reflect.Method.invoke(Method.java:372)
07-29 07:06:12.019  6231  6231 D zhibinw :  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
07-29 07:06:12.019  6231  6231 D zhibinw :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

至此,Launcher的onPause结束。

Launcher 向AMS发送Pause完毕的请求
07-29 07:06:12.022  6231  6231 D zhibinw : java.lang.RuntimeException: ActivityManagerProxy-activityPaused
07-29 07:06:12.022  6231  6231 D zhibinw :  at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:2920)
07-29 07:06:12.022  6231  6231 D zhibinw :  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3206)
07-29 07:06:12.022  6231  6231 D zhibinw :  at android.app.ActivityThread.access$1000(ActivityThread.java:151)
07-29 07:06:12.022  6231  6231 D zhibinw :  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
07-29 07:06:12.022  6231  6231 D zhibinw :  at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 07:06:12.022  6231  6231 D zhibinw :  at android.os.Looper.loop(Looper.java:135)
AMS向Zygote请求启动一个新进程(calculator)

AMS尝试去启动calculator的界面,发现没有,所有想Zygote进程请求创建一个新的进程。Zygote存在的意义就是创建新的进程,AMS通过Socket和Zygote进行通信。

07-29 07:06:12.077  4151  5830 D zhibinw :  at android.os.Process.zygoteSendArgsAndGetResult(Process.java:579)
07-29 07:06:12.077  4151  5830 D zhibinw :  at android.os.Process.startViaZygote(Process.java:692)
07-29 07:06:12.077  4151  5830 D zhibinw :  at android.os.Process.start(Process.java:491)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3034)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:2902)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:2787)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1337)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:1926)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityStack.resumeTopActivityLocked(ActivityStack.java:1477)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.resumeTopActivitiesLocked(ActivityStackSupervisor.java:2520)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1017)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:913)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:6374)
07-29 07:06:12.077  4151  5830 D zhibinw :  at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:513)
07-29 07:06:12.077  4151  5830 D zhibinw :  at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2223)
07-29 07:06:12.077  4151  5830 D zhibinw :  at android.os.Binder.execTransact(Binder.java:446)
Zygote fork出新进程(calculator),在新进程中执行 ActivityThread类的main方法
07-29 07:06:12.104  9272  9272 D zhibinw : ZygoteInit.invokeStaticMain
07-29 07:06:12.104  9272  9272 D zhibinw : java.lang.RuntimeException: android.app.ActivityThread
07-29 07:06:12.104  9272  9272 D zhibinw :  at com.android.internal.os.RuntimeInit.invokeStaticMain(RuntimeInit.java:232)
07-29 07:06:12.104  9272  9272 D zhibinw :  at com.android.internal.os.RuntimeInit.applicationInit(RuntimeInit.java:322)
07-29 07:06:12.104  9272  9272 D zhibinw :  at com.android.internal.os.RuntimeInit.zygoteInit(RuntimeInit.java:277)
07-29 07:06:12.104  9272  9272 D zhibinw :  at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:911)
07-29 07:06:12.104  9272  9272 D zhibinw :  at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:267)
07-29 07:06:12.104  9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:789)
07-29 07:06:12.104  9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
alculator 向 AMS 请求 attach 到 AMS

新进程启动后的第一件事就是上报(attach)到 SystemServer(AMS) ,使得 SystemServer(AMS) 可以统一管理调度。

07-29 07:06:12.107  9272  9272 D zhibinw : ActivityManagerProxy-attachApplication
07-29 07:06:12.107  9272  9272 D zhibinw : java.lang.RuntimeException: ActivityManagerProxy-attachApplication
07-29 07:06:12.107  9272  9272 D zhibinw :  at android.app.ActivityManagerProxy.attachApplication(ActivityManagerNative.java:2874)
07-29 07:06:12.107  9272  9272 D zhibinw :  at android.app.ActivityThread.attach(ActivityThread.java:5095)
07-29 07:06:12.107  9272  9272 D zhibinw :  at android.app.ActivityThread.main(ActivityThread.java:5247)
07-29 07:06:12.107  9272  9272 D zhibinw :  at java.lang.reflect.Method.invoke(Native Method)
07-29 07:06:12.107  9272  9272 D zhibinw :  at java.lang.reflect.Method.invoke(Method.java:372)
07-29 07:06:12.107  9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
07-29 07:06:12.107  9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
07-29 07:06:12.134  4151  6177 D zhibinw : scheduleLaunchActivity
07-29 07:06:12.134  4151  6177 D zhibinw : java.lang.RuntimeException: ApplicationThreadProxy-scheduleLaunchActivity
07-29 07:06:12.134  4151  6177 D zhibinw :  at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:779)
07-29 07:06:12.134  4151  6177 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1226)
07-29 07:06:12.134  4151  6177 D zhibinw :  at com.android.server.am.ActivityStackSupervisor.attachApplicationLocked(ActivityStackSupervisor.java:594)
07-29 07:06:12.134  4151  6177 D zhibinw :  at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:6084)
07-29 07:06:12.134  4151  6177 D zhibinw :  at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:6146)
07-29 07:06:12.134  4151  6177 D zhibinw :  at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:481)
07-29 07:06:12.134  4151  6177 D zhibinw :  at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2223)
07-29 07:06:12.134  4151  6177 D zhibinw :  at android.os.Binder.execTransact(Binder.java:446)
AMS请求calculator launch

AMS 调用 scheduleLaunchActivity 通过IPC 传递到 ActivityThread 的 scheduleLaunchActivity,在 ActivityThread 里面它用 Message 的方式发送自己主线程的 Handler 来异步处理

9272  9272 D zhibinw :  at com.android.calculator2.Calculator.onCreate(Calculator.java:158)
9272  9272 D zhibinw :  at android.app.Activity.performCreate(Activity.java:6033)
9272  9272 D zhibinw :  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
9272  9272 D zhibinw :  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
9272  9272 D zhibinw :  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
9272  9272 D zhibinw :  at android.app.ActivityThread.access$800(ActivityThread.java:151)
9272  9272 D zhibinw :  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
9272  9272 D zhibinw :  at android.os.Handler.dispatchMessage(Handler.java:102)
9272  9272 D zhibinw :  at android.os.Looper.loop(Looper.java:135)
9272  9272 D zhibinw :  at android.app.ActivityThread.main(ActivityThread.java:5258)
9272  9272 D zhibinw :  at java.lang.reflect.Method.invoke(Native Method)
9272  9272 D zhibinw :  at java.lang.reflect.Method.invoke(Method.java:372)
9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
9272  9272 D zhibinw :  at com.android.calculator2.Calculator.onResume(Calculator.java:260)
9272  9272 D zhibinw :  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
9272  9272 D zhibinw :  at android.app.Activity.performResume(Activity.java:6119)
9272  9272 D zhibinw :  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
9272  9272 D zhibinw :  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
9272  9272 D zhibinw :  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
9272  9272 D zhibinw :  at android.app.ActivityThread.access$800(ActivityThread.java:151)
9272  9272 D zhibinw :  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
9272  9272 D zhibinw :  at android.os.Handler.dispatchMessage(Handler.java:102)
9272  9272 D zhibinw :  at android.os.Looper.loop(Looper.java:135)
9272  9272 D zhibinw :  at android.app.ActivityThread.main(ActivityThread.java:5258)
9272  9272 D zhibinw :  at java.lang.reflect.Method.invoke(Native Method)
9272  9272 D zhibinw :  at java.lang.reflect.Method.invoke(Method.java:372)
9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
9272  9272 D zhibinw :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

参考

Android 应用程序启动过程分析

你可能感兴趣的:(App启动过程)