1.启动流程/system/rootdir/init.rc
zygote
/framework/base/cmds/app_process/app_process.cpp
/framework/base/core/jni/AndroidRuntime.cpp
start
startVM 设置启动时的heapsize大小,default 16mstartrReg
app_process
main()create a AndroidRuntime first, then androidRuntime.start("com.android.internal.os.ZygoteInit"), create the zygote.
ZygoteInit
main()
1.register the IPC socket.
2.preload classes
3.startSystemServer
4.runSelectLoopMode
SystemServer start a ServerThread, in the ServerThread, create the java world, AMS, WMS, PMS and so on.
1. 系统启动时init进程会创建Zygote进程,Zygote进程负责后续Android应用程序框架层的其它进程的创建和启动工作。
2. Zygote进程会首先创建一个SystemServer进程,SystemServer进程负责启动系统的关键服务,如包管理服务PackageManagerService和应用程序组件管理服务ActivityManagerService。
3. 当我们需要启动一个Android应用程序时,ActivityManagerService会通过Socket进程间通信机制,通知Zygote进程为这个应用程序创建一个新的进程。
2.A new activity start
AMS::startProcessLocked
Process.start()
the Process will write some strings to Zygote socket, the runSelectLoopMode then call ZygoteConnection::runOnce() to fork a new process, then call RuntimeInit::zygoteInit to establish the binder communication mechanism and call the child process' main().
AMS in SystemServer's process, so the mechanism is SS send string message to Zygote, then Zygote fork a new child process and call it's main().
一. Step1 - Step 11:Launcher通过Binder进程间通信机制通知ActivityManagerService,它要启动一个Activity;
二. Step 12 - Step 16:ActivityManagerService通过Binder进程间通信机制通知Launcher进入Paused状态;
三. Step 17 - Step 24:Launcher通过Binder进程间通信机制通知ActivityManagerService,它已经准备就绪进入Paused状态,于是ActivityManagerService就创建一个新的进程,用来启动一个ActivityThread实例,即将要启动的Activity就是在这个ActivityThread实例中运行;
四. Step 25 - Step 27:ActivityThread通过Binder进程间通信机制将一个ApplicationThread类型的Binder对象传递给ActivityManagerService,以便以后ActivityManagerService能够通过这个Binder对象和它进行通信;
五. Step 28 - Step 35:ActivityManagerService通过Binder进程间通信机制通知ActivityThread,现在一切准备就绪,它可以真正执行Activity的启动操作了。
ActivityThread->performLaunchActivity