framework——應用程式啟動流程
1 前言
2 原始碼分析
2.1 ActivityManagerInternal 初始化流程
(1)請求啟動應用程式的入口
/frameworks/base/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
final WindowProcessController wpc = mService.getProcessController(r.processName, r.info.applicationInfo.uid);
...
//判斷應用程式是否已建立,即 IApplicationThread 是否為空
if (wpc != null && wpc.hasThread()) {
try {
//啟動 Activity
realStartActivityLocked(r, wpc, andResume, checkConfig);
return;
}
}
try {
...
//應用程式不存在,通知建立應用程式,mService 為 ATMS;mService.mAmInternal 為 ActivityManagerInternal
final Message msg = PooledLambda.obtainMessage(
ActivityManagerInternal::startProcess, mService.mAmInternal, r.processName,
r.info.applicationInfo, knownToBeDead, "activity", r.intent.getComponent());
mService.mH.sendMessage(msg);
}
...
}
ActivityStackSupervisor 物件在 ATMS 的 initialize() 方法中建立,並將 ATMS 物件注入到 ActivityStackSupervisor 中。
PooledLambda.obtainMessage() 獲取一個 Message,併為其指定 Callback,即執行 mService.mAmInternal 物件的 ActivityManagerInternal::startProcess 函式,函式入參為 r.processName、r.info.applicationInfo 等5個引數。由此可知,ActivityManagerInternal 的 startProcess() 方法是請求啟動應用程式的起點。
ActivityManagerInternal 是抽象類,其實現類是 AMS 的內部類 LocalService。接下來將分析 ActivityManagerInternal 的建立流程和 ATMS 獲取起物件的流程。
(2)ActivityManagerInternal 的建立
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
private void start() {
...
LocalServices.addService(ActivityManagerInternal.class, new LocalService());
//呼叫 ATMS 的 onActivityManagerInternalAdded 方法進一步初始化
mActivityTaskManager.onActivityManagerInternalAdded();
...
}
start() 方法被 AMS.Lifecycle 的 onStart() 方法呼叫,onStart() 方法又被 SystemServiceManager 的 startService() 方法呼叫。
(3)ATMS 獲取 ActivityManagerInternal
/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public void onActivityManagerInternalAdded() {
synchronized (mGlobalLock) {
mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
...
}
}
(4)LocalServices
public final class LocalServices {
...
private static final ArrayMap<Class<?>, Object> sLocalServiceObjects = new ArrayMap<Class<?>, Object>();
public static <T> T getService(Class<T> type) {
synchronized (sLocalServiceObjects) {
return (T) sLocalServiceObjects.get(type);
}
}
public static <T> void addService(Class<T> type, T service) {
synchronized (sLocalServiceObjects) {
...
sLocalServiceObjects.put(type, service);
}
}
...
}
2.2 AMS 到 ZygoteState
(1)startProcess
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.LocalService.java
public void startProcess(String processName, ApplicationInfo info, boolean knownToBeDead, String hostingType, ComponentName hostingName) {
try {
...
synchronized (ActivityManagerService.this) {
startProcessLocked(processName, info, knownToBeDead, 0 , new HostingRecord(hostingType, hostingName), false, false, true);
}
}
...
}
(2)startProcessLocked
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags,
HostingRecord hostingRecord, boolean allowWhileBooting, boolean isolated, boolean keepIfLarge) {
//mProcessList 在定義時就被初始化,即:final ProcessList mProcessList = new ProcessList()
return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingRecord,
allowWhileBooting, isolated, 0, keepIfLarge, null, null, null, null);
}
(3)startProcessLocked
/frameworks/base/services/core/java/com/android/server/am/ProcessList.java
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags,
HostingRecord hostingRecord, boolean allowWhileBooting, boolean isolated, int isolatedUid,
boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
...
ProcessRecord app;
if (!isolated) {
app = getProcessRecordLocked(processName, info.uid, keepIfLarge);
...
}
...
if (app == null) {
...
} else {
app.addPackage(info.packageName, info.longVersionCode, mService.mProcessStats);
...
}
...
final boolean success = startProcessLocked(app, hostingRecord, abiOverride);
...
return success ? app : null;
}
(4)startProcessLocked
/frameworks/base/services/core/java/com/android/server/am/ProcessList.java
final boolean startProcessLocked(ProcessRecord app, HostingRecord hostingRecord, String abiOverride) {
return startProcessLocked(app, hostingRecord, false, false, false, abiOverride);
}
(5)startProcessLocked
/frameworks/base/services/core/java/com/android/server/am/ProcessList.java
boolean startProcessLocked(ProcessRecord app, HostingRecord hostingRecord, boolean disableHiddenApiChecks,
boolean disableTestApiChecks, boolean mountExtStorageFull, String abiOverride) {
...
try {
...
final String entryPoint = "android.app.ActivityThread";
return startProcessLocked(hostingRecord, entryPoint, app, uid, gids, runtimeFlags,
mountExternal, seInfo, requiredAbi, instructionSet, invokeWith, startTime);
}
...
}
(6)startProcessLocked
/frameworks/base/services/core/java/com/android/server/am/ProcessList.java
boolean startProcessLocked(HostingRecord hostingRecord, String entryPoint, ProcessRecord app,
int uid, int[] gids, int runtimeFlags, int mountExternal, String seInfo,
String requiredAbi, String instructionSet, String invokeWith, long startTime) {
...
final long startSeq = app.startSeq = ++mProcStartSeqCounter;
...
mPendingStarts.put(startSeq, app);
if (mService.mConstants.FLAG_PROCESS_START_ASYNC) {
...
mService.mProcStartHandler.post(() -> {
try {
final Process.ProcessStartResult startResult = startProcess(app.hostingRecord,
entryPoint, app, app.startUid, gids, runtimeFlags, mountExternal,
app.seInfo, requiredAbi, instructionSet, invokeWith, app.startTime);
synchronized (mService) {
handleProcessStartedLocked(app, startResult, startSeq);
}
}
...
});
return true;
} else {
try {
final Process.ProcessStartResult startResult = startProcess(hostingRecord, entryPoint, app, uid, gids,
runtimeFlags, mountExternal, seInfo, requiredAbi, instructionSet, invokeWith, startTime);
handleProcessStartedLocked(app, startResult.pid, startResult.usingWrapper, startSeq, false);
}
...
return app.pid > 0;
}
}
(7)startProcess
/frameworks/base/services/core/java/com/android/server/am/ProcessList.java
private Process.ProcessStartResult startProcess(HostingRecord hostingRecord, String entryPoint,
ProcessRecord app, int uid, int[] gids, int runtimeFlags, int mountExternal,
String seInfo, String requiredAbi, String instructionSet, String invokeWith, long startTime) {
try {
...
else {
startResult = Process.start(entryPoint, app.processName, uid, uid, gids, runtimeFlags, mountExternal,
app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet, app.info.dataDir, invokeWith,
app.info.packageName, app.mDisabledCompatChanges, new String[]{PROC_START_SEQ_IDENT + app.startSeq});
}
...
return startResult;
}
...
}
(8)ProcessStartResult
/frameworks/base/core/java/android/os/Process.java
public static ProcessStartResult start(final String processClass, final String niceName, int uid, int gid, @Nullable int[] gids,
int runtimeFlags, int mountExternal, int targetSdkVersion, String seInfo, String abi, String instructionSet,
String appDataDir, String invokeWith, String packageName, long[] disabledCompatChanges, String[] zygoteArgs) {
return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids, runtimeFlags, mountExternal, targetSdkVersion,
seInfo, abi, instructionSet, appDataDir, invokeWith, packageName, true, disabledCompatChanges, zygoteArgs);
}
(9)ProcessStartResult
/frameworks/base/core/java/android/os/Process.java
z
(10)ProcessStartResult
/frameworks/base/core/java/android/os/Process.java
z
(11)ProcessStartResult
/frameworks/base/core/java/android/os/Process.java
z
2.3 ZygoteInit 到 ActivityThread
相關文章
- framework——ATMS啟動流程Framework
- Spring Boot 應用程式啟動流程分析Spring Boot
- Android 應用啟動流程Android
- flutter 應用啟動流程Flutter
- Android應用啟動流程分析Android
- Android App應用啟動流程(一)AndroidAPP
- Android原始碼(二)應用程式啟動Android原始碼
- Flutter 動態更改應用程式啟動圖示Flutter
- SpringBoot 應用程式啟動過程探祕Spring Boot
- Application Wizard for Mac應用程式快速啟動工具APPMac
- RAC各個程式啟動的流程
- Android 8.0 原始碼分析 (三) 應用程式程式建立到應用程式啟動的過程Android原始碼
- 一張圖輕鬆掌握 Flink on YARN 應用啟動全流程(上)Yarn
- 筆記-iOS應用程式的啟動過程筆記iOS
- win10應用程式無法正常啟動怎麼解決_win10應用程式無法啟動解決教程Win10
- 快速修復應用程式無法正確啟動
- Spring Shell應用程式開發流程解析Spring
- Service啟動流程
- AMS啟動流程
- zygote啟動流程Go
- App啟動流程APP
- Flutter啟動流程Flutter
- linux啟動流程Linux
- IOC 啟動流程
- flowable 啟動流程
- kernel 啟動流程
- springboot啟動流程Spring Boot
- Android系統啟動流程(二)解析Zygote程式AndroidGo
- Android系統啟動流程(四)Launcher啟動過程與系統啟動流程Android
- 使用 nuxi dev 啟動 Nuxt 應用程式的詳細指南UXdev
- 智慧應用程式啟動工具:Alfred 4 for Mac漢化版AlfredMac
- 應用程式無法啟動,因為應用程式的並行配置不正確 解決備忘並行
- Windows啟動谷歌瀏覽器Chrome失敗(應用程式無法啟動,因為應用程式的並行配置不正確)解決方法Windows谷歌瀏覽器Chrome並行
- 用低程式碼平臺搭建應用程式的流程是什麼
- 主流流程引擎 flowable 啟動流程
- nodejs啟動流程分析NodeJS
- Laravel 的啟動流程Laravel
- FlutterEngin啟動流程&androidFlutterAndroid