Android应用程序启动源码浅析-(三万字长文慎点&Android14)

Android桌面Launcher源码浅析中介绍了Android的桌面程序Launcher是如何响应用户点击事件并启动App的,这篇文章继续介绍App在Android系统层的启动流程。

一、启动流程

sequenceDiagram participant User participant Launcher participant ActivityManagerService participant Zygote participant AppProcess participant Application participant FirstActivity User ->> Launcher: 点击应用图标 Launcher ->> ActivityManagerService: 请求启动Activity ActivityManagerService ->> Zygote: 创建新进程 Zygote -->> AppProcess: fork新进程 AppProcess ->> Application: 创建Application实例 Application ->> FirstActivity: 启动第一个Activity FirstActivity ->> User: 显示界面

二、Launcher通知AndroidOS(用户点击图标)

2.1 Activity.java

frameworks/base/core/java/android/app/Activity.java 源码地址

  • Android桌面Launcher源码浅析中提到Launcher最终通过frameworks/base/core/java/android/app/Activity.java中的startActivity方法启动了对应的应用程序。
  • startActivity方法是通过调用startActivityForResult方法来实现的。
  • startActivityForResult方法最终调用了/frameworks/base/core/java/android/app/Instrumentation.javaexecStartActivity方法
public class Activity {

    @Override
    public void startActivity(Intent intent) {
        this.startActivity(intent, null);
    }

    @Override
    public void startActivity(Intent intent, @Nullable Bundle options) {
        if (options != null) {
            startActivityForResult(intent, -1, options);
        } else {
            startActivityForResult(intent, -1);
        }
    }
    
    @Override
    public void startActivityForResult(...) {
        if (mParent == null) {
            options = transferSpringboardActivityOptions(options);
            // 调用Instrumentation的execStartActivity方法
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(...);
        }
    }

}

2.2 Instrumentation.java

frameworks/base/core/java/android/app/Instrumentation.java 源码地址

  • Instrumentation.java中的execStartActivity方法是Activity启动流程的关键。
  • execStartActivity方法通过ActivityTaskManager.getService().startActivity方法与系统服务进行通信.
// Instrumentation中有多个execStartActivity同名方法,注意是target的类型为Activity的方法
public ActivityResult execStartActivity(
        Context who, IBinder contextThread, IBinder token, Activity target,
        Intent intent, int requestCode, Bundle options) {
        
        // ActivityTaskManager.getService()获取到ActivityTaskManagerService的实例对象
        // 调用ActivityTaskManagerService.startActivity
        int result = ActivityTaskManager.getService()
                .startActivity(...);
}

2.3 ActivityTaskManagerService.java

/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java 源码地址

  • ActivityTaskManagerService是一个系统服务,负责管理Activity的启动。
  • startActivity方法调用了startActivityAsUser方法
  • startActivityAsUser方法通过ActivityStartControllerobtainStarter方法获取了ActivityStarter对象实例,并调用ActivityStarterexecute方法
public final int startActivity(...) {
    // 调用startActivityAsUser方法
    return startActivityAsUser(...);
}

public final int startActivityAsUser(...) {

    // ActivityStartController的obtainStarter获取了ActivityStarter对象
    return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
                ... // 构造参数
                .execute();
}

2.4 ActivityStarter.java

/frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java 源码地址

  • ActivityStarter中最终会调用RootWindowContainerresumeFocusedTasksTopActivities方法
int execute() {
    // 调用executeRequest方法
    res = executeRequest(mRequest);
}

private int executeRequest(Request request) {
    // 创建ActivityRecord对象
    final ActivityRecord r = new ActivityRecord.Builder(mService)
            .setCaller(callerApp)
            ... // 构造参数
            .build();
    // 调用startActivityUnchecked方法
    mLastStartActivityResult = startActivityUnchecked(...);
}

private int startActivityUnchecked(...) {
    // 调用startActivityInner
    result = startActivityInner(...);
}

int startActivityInner(...) {
    // 调用RootWindowContainer的resumeFocusedTasksTopActivities方法
    mRootWindowContainer.resumeFocusedTasksTopActivities(...);
}

2.5 RootWindowContainer.java

/frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java 源码地址

  • RootWindowContainer是WindowManagerService的主要组成部分之一,是一个管理窗口的容器。
  • 调用TaskTaskFragment将前台程序Pause,为新的应用程序启动做准备。
  • resumeFocusedTasksTopActivities中调用TaskresumeTopActivityUncheckedLocked方法。

private boolean resumeFocusedTasksTopActivities(...) {
    // 调用Task的resumeTopActivityUncheckedLocked方法
    result = targetRootTask.resumeTopActivityUncheckedLocked(...);
}

2.5.1 Task.java

/frameworks/base/services/core/java/com/android/server/wm/Task.java 源码地址

  • Task最终调用TaskFragmentresumeTopActivity方法
@GuardedBy("mService")
boolean resumeTopActivityUncheckedLocked(...) {
    // 调用resumeTopActivityInnerLocked
    someActivityResumed = resumeTopActivityInnerLocked(...);
}

@GuardedBy("mService")
private boolean resumeTopActivityInnerLocked(...) {
    final TaskFragment topFragment = topActivity.getTaskFragment();
    // 调用TaskFragment的resumeTopActivity
    resumed[0] = topFragment.resumeTopActivity(...););
    return resumed[0];
}

2.5.2 TaskFragment.java

/frameworks/base/services/core/java/com/android/server/wm/TaskFragment.java 源码地址

  • TaskFragment最终调用ActivityTaskManagerServicestartProcessAsync方法
final boolean resumeTopActivity(...) {
   // 暂停当前窗口的Aciticity,可参见源码
   ...
   // 调用ActivityTaskManagerService的startProcessAsync方法创建新的Activity
ActivityTaskManagerService的startProcessAsync方法
   mAtmService.startProcessAsync(...);
}

2.6 再次回到ActivityTaskManagerService.java

/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java 源码地址

  • 处理完窗口容器数据以后(核心工作是将前台程序Pause),再次回到了ActivityTaskManagerService
  • startProcessAsync方法发送异步消息,调用ActivityManagerInternalstartProcess方法
  • ActivityManagerInternal的实现类是ActivityManagerService
// mAmInternal的获取方法,注册方法在ActivityManagerService中(下一小节2.8做介绍)
mAmInternal = LocalServices.getService(ActivityManagerInternal.class);

void startProcessAsync(...) {
    
        // PooledLambda会自动调用mAmInternal(ActivityManagerInternal)的startProcess方法
        final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess, mAmInternal, ...);
        mH.sendMessage(m);
}
  • ActivityManagerInternal是一个抽象类,具体实现在ActivityManagerService.java中实现

2.7 ActivityManagerService.java

/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java 源码地址

  • ActivityManagerService最终调用ProcessListstartProcessLocked方法
public class ActivityManagerService extends IActivityManager.Stub {
    
    // 初始化LocalService(ActivityManagerInternal)
    private final ActivityManagerInternal mInternal = new LocalService();

    public void init() {
        // 在LocalServices中进行注册
        LocalServices.addService(ActivityManagerInternal.class, mInternal);
    }
    private class LocalService extends ActivityManagerInternal {
        // ActivityManagerInternal 接口方法的具体实现
        @Override
        public void startProcess(...) {
            synchronized (ActivityManagerService.this) {
                // 调用ActivityManagerService的startProcessLocked方法
                startProcessLocked(...)
            }
        }
    }
        @GuardedBy("this")
    final ProcessRecord startProcessLocked(...) {
        // 调用ProcessList的startProcessLocked
        return mProcessList.startProcessLocked(...);
    }
}

2.7.1 ProcessList.java

/frameworks/base/services/core/java/com/android/server/am/ProcessList.java 源码地址

  • ProcessList最终调用到ZygoteProcessstart方法
@GuardedBy("mService")
boolean startProcessLocked(ProcessRecord app, ...) {
    // 定义创建Activity完成后回调的入口点(重要)
    final String entryPoint = "android.app.ActivityThread";
    return startProcessLocked(hostingRecord, ...);
}

@GuardedBy("mService")
boolean startProcessLocked(HostingRecord hostingRecord, ...) {
    // 调用startProcess
    final Process.ProcessStartResult startResult = startProcess(...);
}

private Process.ProcessStartResult startProcess(...) {
    // 获取AppZygote
    final AppZygote appZygote = createAppZygoteForProcessIfNeeded(app);
    // appZygote.getProcess()获取到ChildZygoteProcess类(ZygoteProcess的子类)
    // 调用ZygoteProcess的start方法
    startResult = appZygote.getProcess().start(entryPoint,...);
}

2.8 ZygoteProcess.java

/frameworks/base/core/java/android/os/ZygoteProcess.java 源码地址

  • ZygoteProcess发送消息给Zygote进程,通过Zygote进程创建新的activity进程
public final Process.ProcessStartResult start(...) {
    // 调用startViaZygote
    return startViaZygote(...)
}

private Process.ProcessStartResult startViaZygote(...) {
    // 通过openZygoteSocketIfNeeded(abi)打开一个到Zygote进程的套接字连接(Socket)。
    // 调用zygoteSendArgsAndGetResult
    return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi),zygotePolicyFlags,argsForZygote);
}

@GuardedBy("mLock")
private Process.ProcessStartResult zygoteSendArgsAndGetResult(...) {
    // 调用attemptZygoteSendArgsAndGetResult
    return attemptZygoteSendArgsAndGetResult(...);
}

private Process.ProcessStartResult attemptZygoteSendArgsAndGetResult(...) {
    try {
        // 创建Zygote套接字的输入输出流
        final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter;
        final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream;

        // 发送消息给 Zygote 进程
        zygoteWriter.write(msgStr);
        zygoteWriter.flush();

        // 读取 Zygote 进程返回的结果
        Process.ProcessStartResult result = new Process.ProcessStartResult();
        result.pid = zygoteInputStream.readInt();
        result.usingWrapper = zygoteInputStream.readBoolean();
        // 检查 PID 是否有效
        if (result.pid < 0) {
            throw new ZygoteStartFailedEx("fork() failed");
        }

        return result;
    } catch (IOException ex) {
        zygoteState.close();
        Log.e(LOG_TAG, "IO Exception while communicating with Zygote - "
                + ex.toString());
        throw new ZygoteStartFailedEx(ex);
    }
}

三、Activity进程创建(Zygote进程fork)

Android启动过程-万字长文(Android14)介绍了Zygote进程(孵化器进程)

3.1 Zygote简介

  • Zygote进程是一个用户进程,由init进程(1号进程)fork而来。
  • Zygote进程通过fork的方式创建新的应用程序进程。
  • Zygote进程的入口点是ZygoteInit类中的main方法。

下面将简单介绍在Zygote进程的代码流转。

3.2 ZygoteInit.java

Android14的ZygoteInit源码地址

  • Zygote进程是在Android系统启动过程中创建的,创建完成后会通过ZygoteServer来监听消息
public static void main(String argv[]) {
    ZygoteServer zygoteServer = new ZygoteServer();
    ...

    // 启动Zygote服务器,循环监听消息
    caller = zygoteServer.runSelectLoop(abiList);
    if (caller != null) {
        // 有新的消息就执行对应Runnable代码
        caller.run();
    }
    ...
}

3.3 ZygoteServer.java

Android14的ZygoteServer源码地址

  • ZygoteServer获取到消息后会调用ZygoteConnectionprocessCommand方法
Runnable runSelectLoop(String abiList) {

    while (true) {
        // 使用 select 监听套接字
        StructPollfd[] pollFDs = new StructPollfd[socketFDs.size()];
        
        if (pollIndex == 0) {
            // 接收到新的连接
            ZygoteConnection newPeer = acceptCommandPeer(abiList);
            peers.add(newPeer);
            socketFDs.add(newPeer.getFileDescriptor());
        } else {
            // 处理已有连接的请求
            ZygoteConnection connection = peers.get(pollIndex);
            final Runnable command = connection.processCommand(this, multipleForksOK);
        }
    }
}

3.4 ZygoteConnection.java

Android14的ZygoteConnection源码地址

  • ZygoteConnectionprocessCommand方法最终调用ZygoteInitzygoteInit方法

Runnable processCommand(ZygoteServer zygoteServer, boolean multipleOK) {
    ...
    // 调用Zygote的forkAndSpecialize方法fork出Acitivity的进程
    pid = Zygote.forkAndSpecialize(...);
    
    if (pid == 0) {
        // 在子进程中,即创建出来的应用程序所在进程
        return handleChildProc(parsedArgs, childPipeFd,
                parsedArgs.mStartChildZygote);
    } else {
        // 在父线程中,pid为创建好的子进程的id
        handleParentProc(pid, serverPipeFd);
        return null;
    }
}

private void handleParentProc(int pid, FileDescriptor serverPipeFd) {
    // 通过套接字Socket将子进程的 PID 返回给请求方(ActivityManagerService)
    os.writeInt(pid);
}


private Runnable handleChildProc(...) {
    
        // 子进程负责应用程序后续的初始化工作
        ZygoteInit.zygoteInit(...)
}

  • fork系统调用会创建一个新的进程(子进程)。在调用fork后,父进程和子进程(新创建出来的进程)会各自执行后续的代码。
  • 在父进程中,fork返回子进程的PID。这是一个正整数,表示新创建的子进程的进程ID。
  • 在子进程中(新进程),fork 返回 0。这表示当前进程是新创建的子进程。
  • 应用程序(App)的进程就是新创建的子进程

3.5 再次回到ZygoteInit.java

Android14的ZygoteInit源码地址

  • 调用RuntimeInit.applicationInit方法,进行应用程序的初始化过程
public static Runnable zygoteInit(...) {
    // 常见的初始化工作,例如设置系统属性、初始化默认的未捕获异常处理器等
    RuntimeInit.commonInit();
    // Zygote相关的初始化工作。这个初始化过程在C/C++层面进行,设置了必要的Zygote运行环境
    ZygoteInit.nativeZygoteInit();
    // 调用RuntimeInit.applicationInit方法,进行应用程序的初始化过程
    return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv, classLoader);
}

3.6 RuntimeInit.java

Android14的RuntimeInit源码地址

  • RuntimeInitapplicationInit方法完成初始化工作后,通过反射的方式,调用android.app.ActivityThreadmain方法
  • 参数列表中的startClass2.9 ProcessList.java源码中的entryPoint(android.app.ActivityThread)
protected static Runnable applicationInit(...) {
    // 查找并返回应用程序的主方法
    return findStaticMain(args.startClass, args.startArgs, classLoader);
}

private static Runnable findStaticMain(...) {
    // 反射得到android.app.ActivityThread类
    cl = Class.forName(className, true, classLoader);
    // 反射获取main方法
    m = cl.getMethod("main", new Class[] { String[].class });
    // 返回可被执行的Runnable对象
	return new MethodAndArgsCaller(m, argv);
}

四、初始化Application实例

通过Zygote进程fork出应用程序的进程后,下一步就是创建整个应用程序的Application实例

4.1 ActivityThread.java

Android14的ActivityThread源码地址

  • 创建应用程序的ActivityThread实例
  • 创建应用程序的Application实例
  • 创建应用程序的Looper循环
public static void main(String[] args) {

    // 初始化主线模块
    initializeMainlineModules();
    // 创建MainLooper
    Looper.prepareMainLooper();
    // 创建ActivityThread
    ActivityThread thread = new ActivityThread();
    // 创建应用程序Application实例
    thread.attach(false, startSeq);

    if (sMainThreadHandler == null) {
        sMainThreadHandler = thread.getHandler();
    }
    // 循环监听消息
    Looper.loop();
}

@UnsupportedAppUsage
private void attach(boolean system, long startSeq) {
    // 获取ActivityManagerService实例
    final IActivityManager mgr = ActivityManager.getService();
    // 调用ActivityManagerService的attachApplication方法
    mgr.attachApplication(mAppThread, startSeq);
}

4.2 ActivityManagerService.java

Android14的ActivityManagerService源码地址

  • ActivityManagerService完成Application创建和第一个Activity的创建
@Override
public final void attachApplication(IApplicationThread thread, long startSeq) {
    // 调用attachApplicationLocked方法
    attachApplicationLocked(thread, callingPid, callingUid, startSeq);
}

private void attachApplicationLocked(...) {
    // 调用ActivityThread的bindApplication方法创建并绑定Application
    thread.bindApplication(...)
    // 在结束Application创建后调用finishAttachApplicationInner启动app的第一个Activity页面,在4.5会介绍这一部分代码
    finishAttachApplicationInner(startSeq, callingUid, pid);
}

4.3 再次回到ActivityThread.java

Android14的ActivityThread源码地址

  • 通过Handler机制完成消息的传递,正式加载apk文件
public final void bindApplication(...) {
    AppBindData data = new AppBindData();
    ... // 构造data的数据
    // H为Handler, BIND_APPLICATION是int值
    
    sendMessage(H.BIND_APPLICATION, data);
}

// ActivityThread的内部类H
class H extends Handler {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case BIND_APPLICATION:
                AppBindData data = (AppBindData)msg.obj;
                handleBindApplication(data);
                break;
        }
    }
}

@UnsupportedAppUsage
private void handleBindApplication(AppBindData data) {
    Application app;
    // 最终调用LoadedApk的makeApplicationInner方法加载apk文件到内存中
    app = data.info.makeApplicationInner(data.restrictedBackupMode, null);
    // 调用Application的onCreate方法,正式进入apk执行文件
    mInstrumentation.callApplicationOnCreate(app);
}

4.4 LoadedApk.java

Android14的LoadedApk源码地址

private Application makeApplicationInner(...) {
    // 获取app的application类(在AndroidManifast.xml中定义的),没有就使用默认的android.app.Application
    String appClass = mApplicationInfo.getCustomApplicationClassNameForProcess(
            myProcessName);
    if (forceDefaultAppClass || (appClass == null)) {
        appClass = "android.app.Application";
    }
    // 调用Instrumentation的newApplication方法创建Application对象
    app = mActivityThread.mInstrumentation.newApplication(
                    cl, appClass, appContext);
}

4.5 Instrumentation.java

Android14的Instrumentation源码地址

  • Instrumentation完成Application实例的初始化,并调用onCreate方法
// 创建Application对象
static public Application newApplication(Class<?> clazz, Context context) {
    // Application实例的初始化
    Application app = (Application)clazz.newInstance();
    app.attach(context);
    return app;
}

// 调用Application的onCreate方法
public void callApplicationOnCreate(Application app) {
    app.onCreate();
}

五、启动第一个Activity

5.1 ActivityManagerService.java

Android14的ActivityManagerService源码地址

  • 在执行完Application的onCreate方法后,我们再回到ActivityManagerService.javaattachApplicationLocked方法中
  • attachApplicationLocked方法最终会调用LocalServiceattachApplication方法来加载我们的第一个Acitivity页面
  • ActivityTaskManagerInternal是定义在ActivityTaskManagerService.javaLocalService
private void attachApplicationLocked(...) {
    // 调用ActivityThread的bindApplication方法创建并绑定Application
    thread.bindApplication(...)
    // 在结束Application创建后调用finishAttachApplicationInner启动app的第一个Activity页面
    finishAttachApplicationInner(startSeq, callingUid, pid);
}

private void finishAttachApplicationInner(long startSeq, int uid, int pid) {
    if (normalMode) {
        try {
        // 调用`ActivityTaskManagerInternal`(即ActivityTaskManagerService.LocalService)的**attachApplication**方法来加载我们的第一个Acitivity页面
            didSomething = mAtmInternal.attachApplication(app.getWindowProcessController());
        }
    }
}

5.2 ActivityTaskManagerService.java

Android14的ActivityTaskManagerService源码地址

  • 在LocalService中调用RootWindowContainer.attachApplication方法
final class LocalService extends ActivityTaskManagerInternal {
    @Override
    public boolean attachApplication(WindowProcessController wpc){
        // 调用RootWindowContainer的attachApplication
        return mRootWindowContainer.attachApplication(wpc);
    }
}

5.3 RootWindowContainer.java

Android14的RootWindowContainer源码地址

  • 最终调用ActivityTaskSupervisorrealStartActivityLocked方法
boolean attachApplication(WindowProcessController app) throws RemoteException {
    // 调用RootWindowContainer内部类AttachApplicationHelper的process方法
    return mAttachApplicationHelper.process(app);
}
private class AttachApplicationHelper implements Consumer<Task>, Predicate<ActivityRecord> {
    boolean process(WindowProcessController app) throws RemoteException {
        mApp = app;
        for (int displayNdx = getChildCount() - 1; displayNdx >= 0; --displayNdx) {
            // 通过/frameworks/base/services/core/java/com/android/server/wm/Task.java中的forAllRootTasks调用test方法
            getChildAt(displayNdx).forAllRootTasks(this);
        }
    }
    
    public boolean test(ActivityRecord r) {
        // 调用ActivityTaskSupervisor的realStartActivityLocked方法
        mTaskSupervisor.realStartActivityLocked(...)
    }
}

5.3.1 Task.java

/frameworks/base/services/core/java/com/android/server/wm/Task.java源码地址

boolean forAllRootTasks(Predicate<Task> callback, boolean traverseTopToBottom) {
    // 调用test方法
    return isRootTask() ? callback.test(this) : false;
}

5.4 ActivityTaskSupervisor.java

Android14的ActivityTaskSupervisor源码地址

boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
            boolean andResume, boolean checkConfig) throws RemoteException {
        ...
        // 创建ClientTransaction启动事务:
        final ClientTransaction clientTransaction = ClientTransaction.obtain(
                proc.getThread(), r.token);
        ...
        // 添加LaunchActivityItem回调
        clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent...));
        ...
        // 执行启动事务,调用ClientLifecycleManager的scheduleTransaction方法
        mService.getLifecycleManager().scheduleTransaction(clientTransaction);
        ...

}

  • ClientTransaction事务对象,用于描述一系列客户端(即应用进程)需要执行的操作。
  • LaunchActivityItem回调参数中包括如何启动Activity,启动所需的Intent、配置、状态等信息。

5.4.1 ClientLifecycleManager.java

Android14的ClientLifecycleManager源码地址

void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
    final IApplicationThread client = transaction.getClient();
    // 调用ClientTransaction的schedule
    transaction.schedule();
}

5.4.2 ClientTransaction.java

Android14的ClientTransaction源码地址

public void schedule() throws RemoteException {
    // 调用mClient的scheduleTransaction方法
    mClient.scheduleTransaction(this);
}
  • mClientIApplicationThread接口,实际是ActivityThread的内部类ApplicationThread对象。
  • mClientActivityTaskSupervisor的realStartActivityLocked方法中通过ClientTransaction.obtain(proc.getThread(), r.token),具体可参考ActivityTaskSupervisor源码

5.5 回到ActivityThread.java

Android14的ActivityThread源码地址

private class ApplicationThread extends IApplicationThread.Stub {
    @Override
    public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
        // 调用ActivityThread的scheduleTransaction
        ActivityThread.this.scheduleTransaction(transaction);
    }
}
  • scheduleTransaction方法实际调用的是ActivityThread父类ClientTransactionHandlerscheduleTransaction方法

5.5.1 ClientTransactionHandler.java

Android14的ClientTransactionHandler源码地址

void scheduleTransaction(ClientTransaction transaction) {
   transaction.preExecute(this);
   // 将事务放入消息队列中,等待主线程的处理
   sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
}

5.5.2 ActivityThread的内部Handler类H处理消息

Android14的ActivityThread源码地址

public void handleMessage(Message msg) {
    switch (msg.what) {
        case EXECUTE_TRANSACTION:
            final ClientTransaction transaction = (ClientTransaction) msg.obj;
            mTransactionExecutor.execute(transaction);
            break;
    }
}

5.5.3 TransactionExecutor.java

Android14的TransactionExecutor源码地址

public void execute(ClientTransaction transaction) {
    // 执行事务中的回调和生命周期状态请求
    executeCallbacks(transaction);
    executeLifecycleState(transaction);
}

public void executeCallbacks(ClientTransaction transaction) {
    final List<ClientTransactionItem> callbacks = transaction.getCallbacks();
    // 遍历事务中的所有回调项,并调用每个回调项的execute方法。
    for (int i = 0, size = callbacks.size(); i < size; ++i) {
        final ClientTransactionItem item = callbacks.get(i);
        item.execute(mClient, mTransactionHandler, transaction.getLifecycleStateRequest());
    }
}
  • 每个回调项的execute方法实际就是调用LaunchActivityItemexecute方法

5.5.4 LaunchActivityItem.java

Android14的LaunchActivityItem源码地址

@Override
public void execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) {
    client.handleLaunchActivity(new LaunchActivityItem.ActivityClientRecord(this), pendingActions, null);
}
  • 调用ClientTransactionHandler即ActivityThreadhandleLaunchActivity方法

5.6 回到ActivityThread

Android14的ActivityThread源码地址

public Activity handleLaunchActivity(ActivityClientRecord r, PendingTransactionActions pendingActions, Intent customIntent) {
    // 调用performLaunchActivity
    final Activity a = performLaunchActivity(r, customIntent);
}

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    // 创建activity对象
    activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
}

5.6.1 Instrumentation.java

Android14的Instrumentation源码地址

public Activity newActivity(...) {
    // 创建Activity
    Activity activity = (Activity)clazz.newInstance();
    ...
    return activity;
}

5.6.2 回到ActivityThread的performLaunchActivity方法

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    // 创建activity对象
    activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
    // activity绑定上下文Context
    activity.attach(appContext, this, getInstrumentation(), r.token,...);
    // 调用Instrumentation的callActivityOnCreate方法
    mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
}

5.6.3 Instrumentation的callActivityOnCreate方法

public void callActivityOnCreate(Activity activity, Bundle icicle) {
    // 调用Activity的performCreate方法
    activity.performCreate(icicle);
}

5.7 Activity.java

Android14的Activity源码地址

  • 在Activity中实现了生命周期方法的调用逻辑

5.7.1 onCreate方法

final void performCreate(...) {
    // 调用onCreate方法
    if (persistentState != null) {
        onCreate(icicle, persistentState);
    } else {
        onCreate(icicle);
    }
}
  • 绕了一大圈,最终又回到了Activity.java
  • 在performCreate中调用onCreate生命周期方法

5.7.2 onStart方法

5.7.2.1 TransactionExecutor.java

  • TransactionExecutor.java的方法execute中通过executeCallbacks创建了Activity并调用onCreate方法
  • 然后在executeLifecycleState方法中调用后续的生命周期方法
public void execute(ClientTransaction transaction) {
    // 执行事务中的回调
    executeCallbacks(transaction);
    // 执行生命周期
    executeLifecycleState(transaction);
}

public void executeLifecycleState(ClientTransaction transaction) {
    final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest();
    if (lifecycleItem != null) {
        // 调用ActivityLifecycleItem的execute方法
        lifecycleItem.execute(transaction.getClientTransactionHandler(), token, pendingActions);
    }
}

  • ActivityResultItem是一个抽象类
  • 这里实际调用的是ActivityResultItemexecute方法

5.7.2.2 ActivityResultItem.java

Android14的ActivityResultItem源码地址

public void execute(ClientTransactionHandler client, IBinder token,
                    PendingTransactionActions pendingActions) {
    client.handleResumeActivity(token, true /* isForward */, "RESUME_ACTIVITY");
}
  • 这里实际调用的是ActivityThreadhandleResumeActivity方法

5.7.2.3 ActivityThread的handleResumeActivity方法

Android14的ActivityThread源码地址

public void handleResumeActivity(IBinder token, boolean finalStateRequest, String reason) {
    // 先调用ActivityThread的performStart方法
    performRestartActivity(r);
    // 再调用Activity的performResume
    r.activity.performResume();
}

public void performRestartActivity(ActivityClientRecord r) {
    // 调用Activity的performStart
    r.activity.performStart();
}


5.7.2.4 Activity的performStart方法

Android14的Activity源码地址

final void performStart() {
    // 调用Instrumentation的callActivityOnStart方法
    mInstrumentation.callActivityOnStart(this);
}

5.7.2.5 Instrumentation的callActivityOnStart方法

Android14的Instrumentation源码地址

public void callActivityOnStart(Activity activity) {
    activity.onStart();
}

5.7.3 onResume方法

5.7.2.3 ActivityThread的handleResumeActivity方法中提到在onStart方法执行后会调用r.activity.performResume();Activity的performResume方法

5.7.3.1 Activity的performResume方法

Android14的Activity源码地址

final void performResume(boolean followedByPause, String reason) {
    mInstrumentation.callActivityOnResume(this);
}

5.7.3.2 Instrumentation的callActivityOnResume方法

Android14的Instrumentation源码地址

public void callActivityOnResume(Activity activity) {
    activity.onResume();
}
  • 至此Activity创建完成,并完成了核心生命周期方法的创建
  • 在onResume方法后,Activity进入前台,准备显示给用户

六、 后续工作

在生命周期完成后,应用程序就会被展示在屏幕上,后续的工作主要是渲染,这里做一个简单的流程说明

  • Activity实例化时创建一个Window对象,默认情况下是PhoneWindow。在PhoneWindow中,有一个 DecorView,它是整个视图层次的根视图。
  • 在Activity的onCreate方法中,Activity会调用setContentView方法,将布局资源加载到DecorView中
  • WindowManager负责管理应用程序窗口,将DecorView添加到窗口中
  • 当DecorView被添加到窗口中后。ViewRootImpl类负责视图层次结构的测量(measure)、布局(layout)和绘制(draw)
  • 最终由SurfaceFlinger合成并显示在屏幕上

以上就是应用程序启动的全过程,如有错漏,欢迎留言讨论。

热门相关:腹黑老公,请离婚!   首席天价逼婚:老婆不准逃   重生之神级败家子   神级幸运星   我的豪横婆婆