纸上得来终觉浅,绝知此事要躬行。 —— [宋]陆游
基于aosp_cf_x86_64_phone-trunk_staging-eng
, 下面是具体断点位置。
第一部分,桌面launcher进程
com.android.launcher3.touch.ItemClickHandler
- onClick
- onClickAppShortcut
- startAppShortcutOrInfoActivity
com.android.launcher3.Launcher
- startActivitySafely
com.android.launcher3.uioverrides.QuickstepLauncher
- startActivitySafely
com.android.launcher3.views.ActivityContext
- startActivitySafely
这里给Intent增加了FLAG_ACTIVITY_NEW_TASK标记
safely也許是指用了try-catch,保证在UI线程执行,处理空指针,Activity_not_found,SecurityException等等
// 当前Intent的信息
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000
pkg=com.android.gallery3d cmp=com.android.gallery3d/.app.GalleryActivity bnds=[188,775][360,1010] }
android.app.Activity
- startActivity(android.content.Intent, android.os.Bundle)
com.android.launcher3.uioverrides.QuickstepLauncher
- startActivityForResult
com.android.launcher3.Launcher
- startActivityForResult
android.app.Activity
- startActivityForResult(android.content.Intent, int, android.os.Bundle)
如果requestCode 是负数,没有result返回
如果启动模式是FLAG_ACTIVITY_NEW_TASK,得到一个cancel result
注意,在onCreate中调用,屏幕会闪烁,会等待result的返回
android.app.Instrumentation
- execStartActivity
Instrumenttation 监控系统和应用程序交互
至此,Intent已经组装,判断正常
第二部分,系统服务进程
com.android.server.wm.ActivityTaskManagerService
- startActivity
- startActivityAsUser
- startActivityAsUser // 初始化了ActivityStarter
startActivityAsUser
- 启动Activity的进程PID和PackageName是一致的
- 0号进程启动的
- SYSTEM_UID(1000)进程启动的,如果开发系统应用,指定android:sharedUserId=“android.uid.system”,使用系统签名
- 不是以上三种情况,抛出安全异常-SecurityException
- Android系统为新安装的应用分配UID,范围[10000, 19999]
- 多用户之后,UID = userid * 10000 + appid
扩展
0号进程:swapper进程,又称idle进程,系统初始化过程中,kernel从无到有开创的第一个进程,用于初始化进程管理,内存管理,加载binder driver,display,camera driver等
1号进程:init进程
2号进程:Kthread进程,所有内核进程的鼻祖
return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
.setCaller(caller) // IApplicationThread
.setCallingPackage(callingPackage) // com.android.launcher3
.setCallingFeatureId(callingFeatureId) // null
.setResolvedType(resolvedType) // null
.setResultTo(resultTo) // Token{9fa6e8e ActivityRecord{a50b853 u0 com.android.launcher3/.uioverrides.QuickstepLauncher t8}}
.setResultWho(resultWho) // null
.setRequestCode(requestCode) // -1
.setStartFlags(startFlags) // 0
.setProfilerInfo(profilerInfo) // null
.setActivityOptions(opts) // null
.setUserId(userId)
.execute();
com.android.server.wm.ActivityStarter
- execute
- executeRequest // 执行request,根据request组装ActivityRecord【An entry in the history task, representing an activity.】
ActivityTaskSupervisor#resolveActivity(Intent, ResolveInfo, int, ProfilerInfo) // -- 准备ActivityInfo
内含进程名字,目标Activity路径:
processName: com.android.gallery3d
name: com.android.gallery3d.app.GalleryActivity
com.android.server.wm.ActivityStarter.Request
包含:调用者进程,调用原因,Intent信息,目标ActivityInfo
ActivityStarter调用ActivityTaskSupervisor的resolveIntent得到ResolveInfo,该对象和Manifest中的intent
对应
ActivityStarter调用ActivityTaskSupervisor的resolveActivity得到ActivityInfo,该对象和manifest.xml中的<activity>, <service>
标签对应
- startActivityUnchecked
- startActivityInner
com.android.server.wm.Task
- startActivityLocked // 准备切换动画,此处WMS,showStartingWindow
com.android.server.wm.StartingSurfaceController
- showStartingWindow
com.android.server.wm.ActivityRecord
- showStartingWindow
com.android.server.wm.RootWindowContainer
- resumeFocusedTasksTopActivities
com.android.server.wm.Task
- resumeTopActivityUncheckedLocked
- resumeTopActivityInnerLocked
com.android.server.wm.TaskFragment
- resumeTopActivity
com.android.server.wm.ActivityTaskManagerService
- startProcessAsync // 提前异步启动进程,节省等待当前Activity-Pause的时间
com.android.server.am.ActivityManagerService.LocalService
- startProcess
com.android.server.am.ActivityManagerService - startProcessLocked
ProcessList$startProcessLocked 生成一个ProcessRecord // 携带目标进程的信息
com.android.server.am.ProcessList
- startProcessLocked // 新进程的入口:final String entryPoint = “android.app.ActivityThread”;
com.android.server.am.ProcessList
- startProcessLocked // 要么返回新的PID ,要么抛出运行时异常
com.android.server.am.ProcessList
- startProcess // 创建AppZygote,appZygote.getProcess().start
android.os.Process
- start
android.os.ZygoteProcess
- start
- startViaZygote
- zygoteSendArgsAndGetResult
- attemptZygoteSendArgsAndGetResult // 生成ProcessStartResult
android.app.ActivityThread
- main
- attach
com.android.server.am.ActivityManagerService
- attachApplication // 目标进程已启动
- attachApplicationLocked
- finishAttachApplicationInner
// — ActivityThread
android.app.ActivityThread.ApplicationThread
- bindApplication // 组装一个AppBindData
android.app.ActivityThread.H
- handleMessage
android.app.ActivityThread
- handleBindApplication
android.app.Instrumentation
- callApplicationOnCreate
// — ActivityThread
com.android.server.wm.ActivityTaskManagerService.LocalService
- attachApplication
com.android.server.wm.RootWindowContainer
- attachApplication // —> 注:在deubg这里,可以attach到目标进程
com.android.server.wm.ActivityTaskSupervisor // LaunchActivityItem
- realStartActivityLocked
注意:
com.android.server.wm.ClientLifecycleManager
- scheduleTransactionAndLifecycleItems(
IApplicationThread, // --> IApplicationThread S t u b Stub StubProxy
servertransaction.ClientTransactionItem, // LaunchActivityItem
ActivityLifecycleItem, // ResumeActivityItem
boolean
)
此处,给LauncheActivityItem安排的下一个生命周期是ResumenctivityItem,和onCreate() - onStart() - onResume(), 不一致,onStart()从哪里加入的?请看下面,TranslationExecutor$executeLifecycleItem函数。
com.android.server.wm.ClientLifecycleManager
- scheduleTransactionAndLifecycleItems
- scheduleTransaction
android.app.servertransaction.ClientTransaction
- schedule
第三部分,目标应用进程
android.app.ActivityThread.ApplicationThread
- scheduleTransaction
android.app.ClientTransactionHandler // ActivityThread的父类,此时,ActivityThread已经启动了
- scheduleTransaction
android.app.ActivityThread.H
- handleMessage // EXECUTE_TRANSACTION
android.app.servertransaction.TransactionExecutor
- execute
- executeLifecycleState
- executeLifecycleItem // 关键代码: cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */, transaction);
注意跟踪cycleToPath方法; 发现在performLifecycleSequence方法,是切换lifecycle的方法,在执行当前生命周期(ResumeActivityItem)转换之前,执行了onStart()
android.app.servertransaction.ActivityTransactionItem
- execute(android.app.ClientTransactionHandler, android.app.servertransaction.PendingTransactionActions)
android.app.servertransaction.LaunchActivityItem //ActivityClientRecord
- execute
android.app.ActivityThread
- handleLaunchActivity // 这里已经触发了ON_CREATE
android.app.ActivityThread
- performLaunchActivity
android.app.Instrumentation
- newActivity(java.lang.ClassLoader, java.lang.String, android.content.Intent)
android.app.Activity
- attach // 此时出现PhoneWindow 窗口
android.app.Instrumentation
- callActivityOnCreate(android.app.Activity, android.os.Bundle)
android.app.Activity
- performCreate(android.os.Bundle)
- onCreate(android.os.Bundle)
还有一点:
回到ActivityThread
H
的
E
X
E
C
U
T
E
T
R
A
N
S
A
C
T
I
O
N
里,同时在
T
r
a
n
s
a
c
t
i
o
n
E
x
e
c
u
t
o
r
H的EXECUTE_TRANSACTION里,同时在TransactionExecutor
H的EXECUTETRANSACTION里,同时在TransactionExecutorexecuteLifecycleItem里,deubg可以看到,LaunchActivityItem的下一个状态是ResumeActivityItem,在执行Resumectivitytem之前,执行了ON_START. 详情见:单步调试Android Framework——Activity生命周期切换
这样,onStart,onResume都触发了,App和首页Mainctivity 启动完毕,等待交互。