应用的启动流程
来自个人维基
com.android.launcher中执行:
W/System.err( 527): java.lang.Exception: kaigo print trace - startActivity W/System.err( 527): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1610) W/System.err( 527): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1385) W/System.err( 527): at android.app.Activity.startActivityForResult(Activity.java:3315) W/System.err( 527): at com.android.launcher2.Launcher.startActivityForResult(Launcher.java:1504) W/System.err( 527): at android.app.Activity.startActivity(Activity.java:3422) W/System.err( 527): at com.android.launcher2.Launcher.startActivitySafely(Launcher.java:2025) W/System.err( 527): at com.android.launcher2.AppsCustomizePagedView$3.run(AppsCustomizePagedView.java:539) W/System.err( 527): at com.android.launcher2.PagedView$1.onAnimationRepeat(PagedView.java:1068) W/System.err( 527): at android.animation.ValueAnimator.animationFrame(ValueAnimator.java:1128) W/System.err( 527): at android.animation.ValueAnimator$AnimationHandler.handleMessage(ValueAnimator.java:656) W/System.err( 527): at android.os.Handler.dispatchMessage(Handler.java:99) W/System.err( 527): at android.os.Looper.loop(Looper.java:154) W/System.err( 527): at android.app.ActivityThread.main(ActivityThread.java:4643) W/System.err( 527): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 527): at java.lang.reflect.Method.invoke(Method.java:511) W/System.err( 527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811) W/System.err( 527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) W/System.err( 527): at dalvik.system.NativeStart.main(Native Method)
通过 binder 跨进程,进而在 system_server中执行:
W/System.err( 305): java.lang.Exception: kaigo print trace - schedulePauseActivity W/System.err( 305): at android.app.ApplicationThreadProxy.schedulePauseActivity(ApplicationThreadNative.java:572) W/System.err( 305): at com.android.server.am.ActivityStack.startPausingLocked(ActivityStack.java:914) W/System.err( 305): at com.android.server.am.ActivityStack.resumeTopActivityLocked(ActivityStack.java:1444) W/System.err( 305): at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:1853) W/System.err( 305): at com.android.server.am.ActivityStack.startActivityUncheckedLocked(ActivityStack.java:2946) W/System.err( 305): at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:2518) W/System.err( 305): at com.android.server.am.ActivityStack.startActivityMayWait(ActivityStack.java:3099) W/System.err( 305): at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:2535) W/System.err( 305): at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:132) W/System.err( 305): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1756) W/System.err( 305): at android.os.Binder.execTransact(Binder.java:338) W/System.err( 305): at dalvik.system.NativeStart.run(Native Method)
通过 binder,调回 lanucher的 schedulePauseActivity函数:
W/System.err( 527): java.lang.Exception: kaigo print trace - schedulePauseActivity W/System.err( 527): at android.app.ActivityThread$ApplicationThread.schedulePauseActivity(ActivityThread.java:530) W/System.err( 527): at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:78) W/System.err( 527): at android.os.Binder.execTransact(Binder.java:338) W/System.err( 527): at dalvik.system.NativeStart.run(Native Method)
launcher进入暂停状态:
W/System.err( 527): java.lang.Exception: kaigo print trace - activityPaused W/System.err( 527): at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1906) W/System.err( 527): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2801) W/System.err( 527): at android.app.ActivityThread.access$800(ActivityThread.java:134) W/System.err( 527): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255) W/System.err( 527): at android.os.Handler.dispatchMessage(Handler.java:99) W/System.err( 527): at android.os.Looper.loop(Looper.java:154) W/System.err( 527): at android.app.ActivityThread.main(ActivityThread.java:4643) W/System.err( 527): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 527): at java.lang.reflect.Method.invoke(Method.java:511) W/System.err( 527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811) W/System.err( 527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) W/System.err( 527): at dalvik.system.NativeStart.main(Native Method)
通过 socket通知 zygote,为即将启动的 APP fork一个新进程:
W/System.err( 305): java.lang.Exception: kaigo print trace W/System.err( 305): at android.os.Process.zygoteSendArgsAndGetResult(Process.java:389) W/System.err( 305): at android.os.Process.startViaZygote(Process.java:525) W/System.err( 305): at android.os.Process.start(Process.java:290) W/System.err( 305): at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:2225) W/System.err( 305): at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:2133) W/System.err( 305): at com.android.server.am.ActivityStack.startSpecificActivityLocked(ActivityStack.java:747) W/System.err( 305): at com.android.server.am.ActivityStack.resumeTopActivityLocked(ActivityStack.java:1688) W/System.err( 305): at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1084) W/System.err( 305): at com.android.server.am.ActivityStack.activityPaused(ActivityStack.java:988) W/System.err( 305): at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:4751) W/System.err( 305): at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:372) W/System.err( 305): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1756) W/System.err( 305): at android.os.Binder.execTransact(Binder.java:338) W/System.err( 305): at dalvik.system.NativeStart.run(Native Method)
ZygoteInit.runSelectLoop接收到上面发的 socket,通过 forkAndSpecialize创建进程,完毕通过 ZygoteInit.invokeStaticMain抛出 ZygoteInit.MethodAndArgsCaller异常,清除之前的调用堆栈
ZygoteInit.main └─ZygoteInit.runSelectLoop └─ZygoteConnection.runOnce └─Zygote.forkAndSpecialize └─ZygoteConnection.handleChildProc └─ZygoteInit.invokeStaticMain └─throw "ZygoteInit.MethodAndArgsCaller" Exception //如此则将之前的调用堆栈清除 └─ZygoteInit.MethodAndArgsCaller.run
从此便已进入 App自己的进程,开始执行 App自己的代码,执行 onCreate等函数:
W/System.err( 1148): java.lang.Exception: kaigo print trace W/System.err( 1148): at com.example.helloandroid_4_0.MainActivity.onCreate(MainActivity.java:106) W/System.err( 1148): at android.app.Activity.performCreate(Activity.java:4590) W/System.err( 1148): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) W/System.err( 1148): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2042) W/System.err( 1148): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105) W/System.err( 1148): at android.app.ActivityThread.access$600(ActivityThread.java:134) W/System.err( 1148): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248) W/System.err( 1148): at android.os.Handler.dispatchMessage(Handler.java:99) W/System.err( 1148): at android.os.Looper.loop(Looper.java:154) W/System.err( 1148): at android.app.ActivityThread.main(ActivityThread.java:4643) W/System.err( 1148): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 1148): at java.lang.reflect.Method.invoke(Method.java:511) W/System.err( 1148): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811) W/System.err( 1148): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) W/System.err( 1148): at dalvik.system.NativeStart.main(Native Method)
各 pid对应的进程如下:
system 305 116 429368 85188 ffffffff 40019c70 S system_server app_21 527 116 357424 60444 ffffffff 4001a9a0 S com.android.launcher app_63 1148 116 297944 52100 ffffffff 4001a9a0 S com.example.helloandroid_4_0
I/ActivityManager( 308): java.lang.RuntimeException: here I/ActivityManager( 308): at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:1754) I/ActivityManager( 308): at com.android.server.am.ActivityStack.startActivityUncheckedLocked(ActivityStack.java:2926) I/ActivityManager( 308): at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:2498) I/ActivityManager( 308): at com.android.server.am.ActivityStack.startActivityMayWait(ActivityStack.java:3079) I/ActivityManager( 308): at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:2535) I/ActivityManager( 308): at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:131) I/ActivityManager( 308): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1756) I/ActivityManager( 308): at android.os.Binder.execTransact(Binder.java:338) I/ActivityManager( 308): at com.android.server.SystemServer.init1(Native Method) I/ActivityManager( 308): at com.android.server.SystemServer.main(SystemServer.java:946) I/ActivityManager( 308): at java.lang.reflect.Method.invokeNative(Native Method) I/ActivityManager( 308): at java.lang.reflect.Method.invoke(Method.java:511) I/ActivityManager( 308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) I/ActivityManager( 308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) I/ActivityManager( 308): at dalvik.system.NativeStart.main(Native Method)