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
- flutter 應用啟動流程Flutter
- Spring Boot 應用程式啟動流程分析Spring Boot
- Android 應用啟動流程Android
- Android應用啟動流程分析Android
- Android App應用啟動流程(一)AndroidAPP
- Android 7.0 應用啟動流程分析Android
- flask 原始碼解析:應用啟動流程Flask原始碼
- Phalcon Framework的MVC結構及啟動流程分析FrameworkMVC
- Android應用程式程式啟動過程Android
- Flutter 動態更改應用程式啟動圖示Flutter
- 開機自動啟動應用程式 ON LinuxLinux
- android重新啟動應用程式和重新啟動系統 .Android
- Android原始碼(二)應用程式啟動Android原始碼
- 設定Windows應用程式啟動窗體Windows
- Word工具欄也能啟動應用程式
- 讓Symbian應用程式開機自動啟動
- RAC各個程式啟動的流程
- Phalcon Framework的Mvc結構及啟動流程(部分原始碼分析)FrameworkMVC原始碼
- 一張圖輕鬆掌握 Flink on YARN 應用啟動全流程(上)Yarn
- 讓AutoCAD啟動時自動載入應用程式
- 筆記-iOS應用程式的啟動過程筆記iOS
- Application Wizard for Mac應用程式快速啟動工具APPMac
- 在網頁上啟動你的應用程式網頁
- 啟動關聯的應用程式開啟特殊檔案 (轉)
- Flutter啟動流程Flutter
- AMS啟動流程
- App啟動流程APP
- Service啟動流程
- zygote啟動流程Go
- kernel 啟動流程
- flowable 啟動流程
- webx啟動流程Web
- SystemServer啟動流程Server
- IOC 啟動流程
- Android 8.0 原始碼分析 (三) 應用程式程式建立到應用程式啟動的過程Android原始碼
- win10應用程式無法正常啟動怎麼解決_win10應用程式無法啟動解決教程Win10
- Android系統啟動流程(三)解析SyetemServer程式啟動過程AndroidServer