Activity的启动一般分为普通Activity的启动,根Activity的启动.而根Activity是指应用启动的第一个Activity过程,所以也可以理解为应用的启动过程.
相关内容链接:android系统启动流程
Activity的启动流程大致分为如下四个阶段:
一.应用进程(Launcher)调用ATMS系统进程的过程
二.ActivityTaskManagerService到ApplicationThread的调用过程
三.AMS向Zygote发送启动应用进程的过程
四.ActivityThread启动Activity的过程
Activity启动过程涉及到进程之间的关系:
三.AMS向Zygote发送启动应用进程的过程
熟悉系统启动流程的可以知道,Zygote进程的Java框架层会创建一个server端的Socket,
而这个Socket就是用来等待AMS请求Zygote来创建新的应用进程用.
Zygote进程通过fock自身创建应用进程,这样应用进程就获得Zygote进程在启动时 创建的虚拟机实例了,
除此之外还创建了Binder线程池和消息循环Looper,这样 应用进程就可以使用Binder进行进程间通信以及处理消息了
类名: com.android.server.am.ActivityManagerService.LocalService:
1. startProcess()
在上述流程序号14中,在startSpecificActivityLocked()方法中调用了ActivityManagerService.LocalService的startProcess()来请求启动新的应用进程.
/**
* Activity manager local system service interface.
*
* @hide Only for use within the system server.
*/
public abstract class ActivityManagerInternal {
/** Starts a given process. */
public abstract void startProcess(String processName, ApplicationInfo info,
boolean knownToBeDead, String hostingType, ComponentName hostingName);
}
public class ActivityManagerService extends IActivityManager.Stub
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
// ... ...
public final class LocalService extends ActivityManagerInternal {
@Override
public void startProcess(String processName, ApplicationInfo info,
boolean knownToBeDead, String hostingType, ComponentName hostingName) {
try {
if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "startProcess:"
+ processName);
}
synchronized (ActivityManagerService.this) {
startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */,
new HostingRecord(hostingType, hostingName),
false /* allowWhileBooting */, false /* isolated */,
true /* keepIfLarge */);
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
}
}
}
// ... ...
}
类名: com.android.server.am.ActivityManagerService
2. startProcessLocked()
// android-29\com\android\server\am\ActivityManagerService.java
@GuardedBy("this")
final ProcessRecord startProcessLocked(String processName,
ApplicationInfo info, boolean knownToBeDead, int intentFlags,
HostingRecord hostingRecord, boolean allowWhileBooting,
boolean isolated, boolean keepIfLarge) {
return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,
hostingRecord, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge,
null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */,
null /* crashHandler */);
}
类名: com.android.server.am.ProcessList
3. startProcessLocked(java.lang.String, android.content.pm.ApplicationInfo, boolean, int, HostingRecord, boolean, boolean, int, boolean, java.lang.String, java.lang.String, java.lang.String[], java.lang.Runnable)
4. startProcessLocked(ProcessRecord, HostingRecord, java.lang.String)
5. startProcessLocked(ProcessRecord, HostingRecord, boolean, boolean, java.lang.String)
6. startProcessLocked(HostingRecord, java.lang.String, ProcessRecord, int, int[], int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long)
7. startProcess
类名: android.os.Process:
8. start()
Process的start方法只调用了ZYGOTE_PROCESS的start方法,ZYGOTE_PROCESS类是用于保持与Zygote进程的通信状态.
public static ProcessStartResult start(@NonNull final String processClass,
@Nullable final String niceName,
int uid, int gid, @Nullable int[] gids,
int runtimeFlags,
int mountExternal,
int targetSdkVersion,
@Nullable String seInfo,
@NonNull String abi,
@Nullable String instructionSet,
@Nullable String appDataDir,
@Nullable String invokeWith,
@Nullable String packageName,
@Nullable String[] zygoteArgs) {
return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids,
runtimeFlags, mountExternal, targetSdkVersion, seInfo,
abi, instructionSet, appDataDir, invokeWith, packageName,
/*useUsapPool=*/ true, zygoteArgs);
}
类名: android.os.ZygoteProcess:
9. start()
10. startViaZygote()
11. zygoteSendArgsAndGetResult(ZygoteState, boolean, ArrayList
创建字符串列表argsForZygote,将应用进程的启动参数保持在argsForZygote中,然后调用zygoteSendArgsAndGetResult(),该方法的主要作用是将传入的应用进程的启动参数argsForZygote写入到ZygoteState中.
private Process.ProcessStartResult startViaZygote(@NonNull final String processClass,
@Nullable final String niceName,
final int uid, final int gid,
@Nullable final int[] gids,
int runtimeFlags, int mountExternal,
int targetSdkVersion,
@Nullable String seInfo,
@NonNull String abi,
@Nullable String instructionSet,
@Nullable String appDataDir,
@Nullable String invokeWith,
boolean startChildZygote,
@Nullable String packageName,
boolean useUsapPool,
@Nullable String[] extraArgs)
throws ZygoteStartFailedEx {
// 创建字符串列表argsForZygote,将应用进程的启动参数保持在argsForZygote中
ArrayList argsForZygote = new ArrayList<>();
// --runtime-args, --setuid=, --setgid=,
// and --setgroups= must go first
argsForZygote.add("--runtime-args");
argsForZygote.add("--setuid=" + uid);
argsForZygote.add("--setgid=" + gid);
argsForZygote.add("--runtime-flags=" + runtimeFlags);
if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) {
argsForZygote.add("--mount-external-default");
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) {
argsForZygote.add("--mount-external-read");
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) {
argsForZygote.add("--mount-external-write");
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_FULL) {
argsForZygote.add("--mount-external-full");
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_INSTALLER) {
argsForZygote.add("--mount-external-installer");
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_LEGACY) {
argsForZygote.add("--mount-external-legacy");
}
// ... ...
synchronized(mLock) {
// The USAP pool can not be used if the application will not use the systems graphics
// driver. If that driver is requested use the Zygote application start path.
return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi),
useUsapPool,
argsForZygote);
}
}
12. openZygoteSocketIfNeeded
上述zygoteSendArgsAndGetResult()方法的第一个参数调用了openZygoteSocketIfNeeded(),这里会调用ZygoteState的connect方法与Zygote建立连接
/**
* Tries to open a session socket to a Zygote process with a compatible ABI if one is not
* already open. If a compatible session socket is already open that session socket is returned.
* This function may block and may have to try connecting to multiple Zygotes to find the
* appropriate one. Requires that mLock be held.
*/
@GuardedBy("mLock")
private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
try {
//与Zygote进程建立socket连接
attemptConnectionToPrimaryZygote();
if (primaryZygoteState.matches(abi)) {
return primaryZygoteState;
}
// 如果上述条件不匹配,则尝试连接Zygote辅助模式
if (mZygoteSecondarySocketAddress != null) {
// The primary zygote didn't match. Try the secondary.
attemptConnectionToSecondaryZygote();
if (secondaryZygoteState.matches(abi)) {
return secondaryZygoteState;
}
}
} catch (IOException ioe) {
throw new ZygoteStartFailedEx("Error connecting to zygote", ioe);
}
throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
}
/**
* Creates a ZygoteState for the primary zygote if it doesn't exist or has been disconnected.
*/
@GuardedBy("mLock")
private void attemptConnectionToPrimaryZygote() throws IOException {
if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
primaryZygoteState =
ZygoteState.connect(mZygoteSocketAddress, mUsapPoolSocketAddress);
maybeSetApiBlacklistExemptions(primaryZygoteState, false);
maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState);
maybeSetHiddenApiAccessStatslogSampleRate(primaryZygoteState);
}
}
类名: android.os.ZygoteProcess.ZygoteState:
13. connect()