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