由于四大元件的啟動都涉及到程序的啟動,是以我們這章先講一下程序啟動流程,然後再講四大元件的啟動流程。
基礎知識
Android應用程式架構層建立的應用程式程序具有兩個特點,一是程序的入口函數是ActivityThread.main,二是程序天然支援Binder程序間通信機制;這兩個特點都是在程序的初始化過程中實作的。(引用自老羅安卓之旅-Android應用程式程序啟動過程的源代碼分析)
程序按照重要性可以分為下面五類:
- 前台程序(Foreground process)
- 可見程序(Visible process)
- 服務程序(Service process)
- 背景程序(Background process)
- 空程序(Empty process)
程序啟動流程
AMS(ActivityMagagerService)啟動程序是從其成員函數startProcessLocked開始調用Process.start方法開始的。我們先看一下程序啟動的時序圖:
1. Process.start方法:
public static final ProcessStartResult start(final String processClass,
final String niceName,
int uid, int gid, int[] gids,
int debugFlags, int mountExternal,
int targetSdkVersion,
String seInfo,
String abi,
String instructionSet,
String appDataDir,
String[] zygoteArgs) {
try {
// 請求Zygote程序建立一個應用程序
return startViaZygote(processClass, niceName, uid, gid, gids,
debugFlags, mountExternal, targetSdkVersion, seInfo,
abi, instructionSet, appDataDir, zygoteArgs);
} catch (ZygoteStartFailedEx ex) {
Log.e(LOG_TAG,
"Starting VM process through Zygote failed");
throw new RuntimeException(
"Starting VM process through Zygote failed", ex);
}
}複制代碼
注意:傳入的第一個參數是“android.app.ActivityThread”,這是程序初始化要加載的類,這個類加載到程序之後,就會把這個類的靜态成員方法main作為程序的入口。然後調用startViaZygote方法。
2. startViaZygote方法:
private static ProcessStartResult startViaZygote(final String processClass,
final String niceName,
final int uid, final int gid,
final int[] gids,
int debugFlags, int mountExternal,
int targetSdkVersion,
String seInfo,
String abi,
String instructionSet,
String appDataDir,
String[] extraArgs)
throws ZygoteStartFailedEx {
synchronized (Process.class) {
ArrayList<String> argsForZygote = new ArrayList<String>();
// 儲存要建立應用程式程序的啟動參數到argsForZygote中
...
// 儲存id到argsForZygote中
...
// 儲存其他資訊到argsForZygote中
...
// 請求Zygote程序建立這個應用程序
return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
}
}複制代碼
這個方法主要是儲存資訊到argsForZygote中,然後調用openZygoteSocketIfNeeded,然後根據傳回的值調用zygoteSendArgsAndGetResult方法,首先先看openZygoteSocketIfNeeded方法。
3. openZygoteSocketIfNeeded方法:
private static ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
try {
// 通過調用ZygoteState.connect方法建立LocalSocket對象,以便将相應參數傳入Zygote程序
primaryZygoteState = ZygoteState.connect(ZYGOTE_SOCKET);
} catch (IOException ioe) {
throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);
}
}
if (primaryZygoteState.matches(abi)) {
return primaryZygoteState;
}
// The primary zygote didn't match. Try the secondary.
if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
try {
// 通過調用ZygoteState.connect方法建立LocalSocket對象,以便将相應參數傳入Zygote程序
secondaryZygoteState = ZygoteState.connect(SECONDARY_ZYGOTE_SOCKET);
} catch (IOException ioe) {
throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe);
}
}
if (secondaryZygoteState.matches(abi)) {
return secondaryZygoteState;
}
throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
}複制代碼
通過ZygoteState.connect放建立primaryZygoteState對象,如果第一次建立不成功,建立第二次。connect方法代碼如下:
4. ZygoteState.connect方法:
public static ZygoteState connect(String socketAddress) throws IOException {
DataInputStream zygoteInputStream = null;
BufferedWriter zygoteWriter = null;
// 這個Socket由ZygoteInit.java檔案中的ZygoteInit類在runSelectLoopMode函數偵聽的。
final LocalSocket zygoteSocket = new LocalSocket();
try {
// 開始建立連接配接,在連接配接過程中,LocalSocket對象zygoteSocket會在/dev/socket目錄下找到
// 一個對應的zygote檔案,然後将它與自己綁定起來,這就相當于與Zygote程序中的名稱為“zygote”
// 的Socket建立了連接配接
zygoteSocket.connect(new LocalSocketAddress(socketAddress,
LocalSocketAddress.Namespace.RESERVED));
// 連接配接成功以後,首先擷取LocalSocket對象zygoteSocket的一個輸入流,并且儲存在
// zygoteInputStream中,以便獲得Zygote程序發送過來的通信資料
zygoteInputStream = new DataInputStream(zygoteSocket.getInputStream());
// 又得到LocalSocket對象zygoteSocket的一個輸入流,并且儲存在zygoteWriter中,以便
// 可以向Zygote程序發送通信資料
zygoteWriter = new BufferedWriter(new OutputStreamWriter(
zygoteSocket.getOutputStream()), 256);
} catch (IOException ex) {
...
}
String abiListString = getAbiList(zygoteWriter, zygoteInputStream);
Log.i("Zygote", "Process: zygote socket opened, supported ABIS: " + abiListString);
// 建立的LocalSocket對象zygoteSocket會儲存在ZygoteState中
return new ZygoteState(zygoteSocket, zygoteInputStream, zygoteWriter,
Arrays.asList(abiListString.split(",")));
}複制代碼
首先建立一個LocalSocket對象,這個LocalSocket對象是在ZygoteInit中的runSelectLoop函數進行監聽的。然後通過connect方法并且傳入連接配接位址連接配接該Socket,連接配接以後會擷取輸入流DataInputStream,以便獲得Zygote程序發送過來的通信資料,然後又擷取BufferedWriter輸入流,以便向Zygote程序發送通信資料。最後會傳回一個ZygoteState對象。下面我們看一下LocalSocket.connect方法。
5. LocalSocket.connect方法:
public void connect(LocalSocketAddress endpoint) throws IOException {
synchronized (this) {
if (isConnected) {
throw new IOException("already connected");
}
implCreateIfNeeded();
impl.connect(endpoint, 0);
isConnected = true;
isBound = true;
}
}複制代碼
如果已經連接配接,抛出異常,因為連接配接完成後,會關閉連接配接,使用時在打開連接配接。最後調用native方法連接配接socket,并且改變連接配接标簽。
6. 回到第二步,調用完openZygoteSocketIfNeeded傳回參數ZygoteState傳入到zygoteSendArgsAndGetResult方法中:
private static ProcessStartResult zygoteSendArgsAndGetResult(
ZygoteState zygoteState, ArrayList<String> args)
throws ZygoteStartFailedEx {
try {
// Throw early if any of the arguments are malformed. This means we can
// avoid writing a partial response to the zygote.
int sz = args.size();
for (int i = 0; i < sz; i++) {
if (args.get(i).indexOf('\n') >= 0) {
throw new ZygoteStartFailedEx("embedded newlines not allowed");
}
}
final BufferedWriter writer = zygoteState.writer;
final DataInputStream inputStream = zygoteState.inputStream;
writer.write(Integer.toString(args.size()));
writer.newLine();
for (int i = 0; i < sz; i++) {
String arg = args.get(i);
writer.write(arg);
writer.newLine();
}
writer.flush();
// Zygote程序接收到這些資料之後,就會建立一個新的應用程式程序,并且将這個新建立的應用程式程序
// 的PID傳回給Activity管理服務AMS
// Should there be a timeout on this?
ProcessStartResult result = new ProcessStartResult();
// Always read the entire result from the input stream to avoid leaving
// bytes in the stream for future process starts to accidentally stumble
// upon.
result.pid = inputStream.readInt();
result.usingWrapper = inputStream.readBoolean();
if (result.pid < 0) {
throw new ZygoteStartFailedEx("fork() failed");
}
return result;
} catch (IOException ex) {
zygoteState.close();
throw new ZygoteStartFailedEx(ex);
}
}複制代碼
這方法通過Socket流的方式将啟動程序的資訊發送出去,從步驟4可知,這個Socket的監聽是ZygoteInit類中的runSelectLoop方法,我們接着看這個方法。
7. ZygoteInit.runSelectLoop方法:
private static void runSelectLoop(String abiList) throws MethodAndArgsCaller {
ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
fds.add(sServerSocket.getFileDescriptor());
peers.add(null);
while (true) {
StructPollfd[] pollFds = new StructPollfd[fds.size()];
for (int i = 0; i < pollFds.length; ++i) {
pollFds[i] = new StructPollfd();
pollFds[i].fd = fds.get(i);
pollFds[i].events = (short) POLLIN;
}
try {
Os.poll(pollFds, -1);
} catch (ErrnoException ex) {
throw new RuntimeException("poll failed", ex);
}
for (int i = pollFds.length - 1; i >= 0; --i) {
if ((pollFds[i].revents & POLLIN) == 0) {
continue;
}
if (i == 0) {
ZygoteConnection newPeer = acceptCommandPeer(abiList);
peers.add(newPeer);
fds.add(newPeer.getFileDesciptor());
} else {
boolean done = peers.get(i).runOnce();
if (done) {
peers.remove(i);
fds.remove(i);
}
}
}
}
}複制代碼
資料通過Socket發送以後,Zygote程序接收到後會調用peers.get(i).runOnce()方法。這個peers.get(i)是擷取ZygoteConnection對象,表示一個Socket連接配接,然後調用它的runOnce方法。
8. ZygoteConnection.runOnce方法:
boolean runOnce() throws ZygoteInit.MethodAndArgsCaller {
String args[];
Arguments parsedArgs = null;
FileDescriptor[] descriptors;
try {
// 獲得建立應用程式程序需要的啟動參數,并且儲存在一個Arguments對象parsedArgs中
args = readArgumentList();
descriptors = mSocket.getAncillaryFileDescriptors();
} catch (IOException ex) {
Log.w(TAG, "IOException on command socket " + ex.getMessage());
closeSocket();
return true;
}
...
/** the stderr of the most recent request, if avail */
PrintStream newStderr = null;
if (descriptors != null && descriptors.length >= 3) {
newStderr = new PrintStream(
new FileOutputStream(descriptors[2]));
}
int pid = -1;
FileDescriptor childPipeFd = null;
FileDescriptor serverPipeFd = null;
try {
parsedArgs = new Arguments(args);
if (parsedArgs.abiListQuery) {
return handleAbiListQuery();
}
...
// 調用forkAndSpecialize方法來建立這個應用程式程序,最終通過函數fork在目前程序中建立一個子程序,
// 是以,當它的傳回值等于0時,就表示是在新建立的子程序中執行的,這時候ZygoteConnection類就會調用
// 成員函數handleChildProc來啟動這個子程序
pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid, parsedArgs.gids,
parsedArgs.debugFlags, rlimits, parsedArgs.mountExternal, parsedArgs.seInfo,
parsedArgs.niceName, fdsToClose, parsedArgs.instructionSet,
parsedArgs.appDataDir);
} catch (ErrnoException ex) {
...
} catch (IllegalArgumentException ex) {
...
} catch (ZygoteSecurityException ex) {
...
}
try {
if (pid == 0) {
...
handleChildProc(parsedArgs, descriptors, childPipeFd, newStderr);
return true;
} else {
...
return handleParentProc(pid, descriptors, serverPipeFd, parsedArgs);
}
} finally {
...
}
}複制代碼
首先通過Zygote.forkAndSpecialize方法來建立一個新的程序,并且傳回其pid。因為我們在分心建立程序,是以我們隻分析pid為0的情況,pid為0時會調用handleChildProc方法,
9. handleChildProc方法:
private void handleChildProc(Arguments parsedArgs,
FileDescriptor[] descriptors, FileDescriptor pipeFd, PrintStream newStderr)
throws ZygoteInit.MethodAndArgsCaller {
...
// End of the postFork event.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
if (parsedArgs.invokeWith != null) {
WrapperInit.execApplication(parsedArgs.invokeWith,
parsedArgs.niceName, parsedArgs.targetSdkVersion,
VMRuntime.getCurrentInstructionSet(),
pipeFd, parsedArgs.remainingArgs);
} else {
// 初始化運作庫以及啟動一個Binder線程池
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion,
parsedArgs.remainingArgs, null /* classLoader */);
}
}複制代碼
由于我們之前加入參數是沒有parsedArgs.invokeWith這個參數,是以這裡是null,是以會走else裡面的代碼,執行RuntimeInit.zygoteInit方法。
10. RuntimeInit.zygoteInit方法:
public static final void zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
throws ZygoteInit.MethodAndArgsCaller {
if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application from zygote");
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "RuntimeInit");
redirectLogStreams();
// 首先調用下面函數來設定新建立的應用程式程序的時區和鍵盤布局等通用資訊
commonInit();
// 然後調用下面Native函數在新建立的應用程式程序中啟動一個Binder線程池
nativeZygoteInit();
applicationInit(targetSdkVersion, argv, classLoader);
}複制代碼
首先調用nativeZygoteInit函數,這是一個native函數,函數的目的是在新建立的應用程式程序中啟動一個Binder線程池然後進行程序間通信。然後調用applicationInit函數
11. applicationInit函數:
private static void applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
throws ZygoteInit.MethodAndArgsCaller {
...
// Remaining arguments are passed to the start class's static main
// 我們知道AMS指定了新建立的應用程式程序的入口函數為ActivityThread類的靜态成員函數main。實際是
// 通過下面方法進入到ActivityThread類的靜态成員函數main中的
invokeStaticMain(args.startClass, args.startArgs, classLoader);
}複制代碼
我們在前面講過args.startClass傳入進來的是"android.app.ActivityThread",表示要執行"android.app.ActivityThread"的main函數。
12. invokeStaticMain函數:
private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader)
throws ZygoteInit.MethodAndArgsCaller {
Class<?> cl;
try {
cl = Class.forName(className, true, classLoader);
} catch (ClassNotFoundException ex) {
...
}
Method m;
try {
// 擷取它的靜态成員函數main,并且儲存在Method對象m中
m = cl.getMethod("main", new Class[] { String[].class });
} catch (NoSuchMethodException ex) {
...
} catch (SecurityException ex) {
...
}
...
/*
* This throw gets caught in ZygoteInit.main(), which responds
* by invoking the exception's run() method. This arrangement
* clears up all the stack frames that were required in setting
* up the process.
* 将這個Method對象封裝在一個MethodAndArgsCaller對象中,并且将這個MethodAndArgsCaller對象作為
* 一個異常對象抛出來給目前應用程式處理
*/
throw new ZygoteInit.MethodAndArgsCaller(m, argv);
/**
* 引用自Android系統源代碼情景分析中的Android程序啟動分析一文
* 新建立的應用程式程序複制了Zygote程序的位址空間,是以,目前新建立的應用程式程序的調用棧與Zygote
* 程序的調用堆棧是一緻的。Zygote程序最開始執行的是應用程式app_process的入口函數main,接着再調用
* ZygoteInit類的靜态成員函數main,最後進入到ZygoteInit類的靜态成員函數runSelectLoopMode來循環
* 等待Activity管理服務AMS發送過來的建立新的應用程序的請求。當Zygote程序收到AMS發送過來的建立新的
* 應用程式程序的請求之後,它就會建立一個新的應用程式程序,并且讓這個新建立的應用程式程序沿着
* ZygoteInit類的靜态函數runSelectLoopModel一直執行到RuntimeInit類的靜态成員函數
* invokeStaticMain。是以,當RuntimeInit類的靜态成員函數invokeStaticMain抛出一個類型為
* MethodAndArgsCaller的常時,系統就會沿着這個調用過程往後找到一個适合的代碼塊來捕獲它。
* 由于ZygoteInit函數main捕獲了類型為MethodAndArgsCaller的異常,是以,接下來它就會被調用,以便
* 可以處理這裡抛出的一個MethodAndArgsCaller異常。是以,抛出這個異常後,會執行ZygoteInit中main
* 函數中的catch來捕獲異常。
*
*/
}複制代碼
這個就是通過類加載器加載ActivityThread,然後調用起main方法。然後抛出異常,通過ZygoteInit中main函數中的catch來捕獲異常。
13. ZygoteInit.main函數:
public static void main(String argv[]) {
...
} catch (MethodAndArgsCaller caller) {
// 捕獲MethodAndArgsCaller異常以後會調用MethodAndArgsCaller的run函數
// ActivityThread.main
caller.run();
} catch (Throwable ex) {
...
}
}複制代碼
通過步驟12可知抛出的異常是MethodAndArgsCaller異常,是以會執行caller.run方法。
14. MethodAndArgsCaller.run:
/**
* 注釋來自Android系統源代碼情景分析
* 這裡開始調用ActivityThread.main方法,為什麼要繞這麼遠呢,前面提到,AMS請求Zygote程序建立的應用
* 程式程序的入口函數為ActivityThread的main函數,但是由于新建立的應用程式程序一開始就需要再内部初始
* 化運作時庫,以及啟動Binder線程池,是以,ActivityThread的main函數被調用時,新建立的應用程式程序
* 實際上已經執行了相當多的代碼,為了使得西建立的應用程式的程序覺得它的入口函數就是ActivityThread類
* 的main函數,系統就不能直接調用,而是抛出異常回到ZygoteInit的main函數中,然後間接調用它,這樣就
* 可以巧妙的利用Java語言的異常處理來清理它前面調用的堆棧了
*/
public void run() {
try {
// 調用ActivityThread.main
mMethod.invoke(null, new Object[]{mArgs});
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException(ex);
}
}複制代碼
通過mMethod.invoke方法調用ActivityThread的main方法。
15. ActivityThread.mian方法:
/**
* 啟動新的程序時調用Process的start方法會最終調用改函數
* 啟動新的程序主要做了兩件事:
* 1.在程序中建立了一個ActivityThread對象,并調用了它的成員函數attach向AMS發送一個啟動完成的通知
* 2.調用Looper類的靜态成員函數prepareMainLooper建立一個消息循環,并且在向AMS發送啟動完成通知後,
* 使得目前程序進入到這個消息循環中
*
* @param args
*/
public static void main(String[] args) {
...
// 建立looper
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
// 傳入false表示非系統程序啟動
thread.attach(false);
if (sMainThreadHandler == null) {
// 擷取主線程的Handler
sMainThreadHandler = thread.getHandler();
}
...
// 開始無限循環
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}複制代碼
這裡主要是建立該線程的looper,然後建立ActivityThread對象,然後進入消息循環。然後我們就可以啟動Activity或者Service了。
注
Android開發群:192508518
微信公衆賬号:Code-MX
注:本文原創,轉載請注明出處,多謝。