Android startup process

Posted by Desdinova on Thu, 24 Feb 2022 09:29:14 +0100

Android startup process

Android system startup process

1. Start the power supply to start the system:

When the power key is pressed, the boot chip code is executed from a predefined place (solidified in ROM). Load the BootLoader into RAM and execute.

2. BootLoader:

BootLoader is a small program before the Android operating system starts running. Its main function is to pull up and run the system OS.

3. Linux Kernel startup:

When Kernel starts, set cache, protected memory, plan list and load driver. When the Kernel finishes setting up the system, it first looks for init RC file and start the init process.

4. init process start:

  • init process is the first process of user control in Android system, and the process number is 1.

Main functions: initialize and start the attribute service and start the Zygote process.

It can be divided into three parts:

  • Create and mount file directories required for startup
  • Initialize and start property services
  • Parse init RC configuration file and start Zygote process

The functions of init process are divided into four parts:

  • Resolve and run all init RC related documents
  • Generate the corresponding device driver node according to the rc file
  • Handle the termination of subprocess (signal mode)
  • Function of providing Attribute Service

Here is init For some code in CPP, you can see parser Parseconfig ("/ system / etc / init / HW / init.rc") for init RC is analyzed.

static void LoadBootScripts(ActionManager& action_manager, ServiceList& service_list) {
    Parser parser = CreateParser(action_manager, service_list);
    std::string bootscript = GetProperty("ro.boot.init_rc", "");
    if (bootscript.empty()) {
        if (!parser.ParseConfig("/system/etc/init")) {
        // late_import is available only in Q and earlier release. As we don't
        // have system_ext in those versions, skip late_import for system_ext.
        if (!parser.ParseConfig("/product/etc/init")) {
        if (!parser.ParseConfig("/odm/etc/init")) {
        if (!parser.ParseConfig("/vendor/etc/init")) {
    } else {

init.rc is a file written by the script of Android Init Language (hereinafter referred to as AIL).

Here is init Some codes in RC file:

on late-init
 # Now we can start zygote for devices with file based encryption
    trigger zygote-start

As you can see, the zygote process is started here. Android8. After 0, init RC is split: the zygote process is split to init zygote32. RC (32-bit), init zygote64. RC (64 bit) file. Take 64 bit files as an example:

service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server
    class main
    priority -20
    user root
    group root readproc reserved_disk
    socket zygote stream 660 root system
    socket usap_pool_primary stream 660 root system
    onrestart exec_background - system system -- /system/bin/vdc volume abort_fuse
    onrestart write /sys/power/state on
    onrestart restart audioserver
    onrestart restart cameraserver
    onrestart restart media
    onrestart restart netd
    onrestart restart wificond
    writepid /dev/cpuset/foreground/tasks

Obviously, there are some things we know, such as wifi, media, Internet, camera and so on.

5.Zygote process start:

**Main functions: * * create Java virtual machine, register JNI method for Java virtual machine, create server Socket and start SystemServer process. It is the first process of Java, which directly or indirectly incubates the whole java process in the system.

In Android system, DVM and ART, application process and SystemServer process running the key services of the system are all from Zygote process fork.

The zygote process has mainly done the following work:

  1. Create the AppRuntime and call its start method to start the Zygote process (this process is executed at the end of the init process)
  2. Create a java virtual machine and register the JNI method for the java virtual machine
  3. Call the main function of ZygoteInit through JNI to enter the java framework layer of Zygote
  4. Create a server Socket through the registerZygoteSocket method
  5. Start the SystemServer process
  6. Create a new application process by waiting for a request from AMS through the runSelectLoop method

6.System Server process startup:

**Main functions: * * start Binder thread pool and SystemServiceManager, and start various system services. AMS, WMS, PMS and so on are all created by it.

The main work is as follows:

  1. Start the Binder thread pool so that you can communicate with other processes.
  2. Create a system service manager, which is used to create, start, and lifecycle manage system service processes.
  3. Start various system services (guidance services, core services, other services).

7.Launcher startup:

AMS started by the SystemServer process will start the Launcher. The last step of system startup is to start an application to display the installed applications in the system. This application is called the Launcher. During the startup process, Lanucher will request PackageManagerService to return the installed application information in the system, and encapsulate these information into a list of shortcut icons to be displayed on the system screen.

The main functions are:

  1. As the initiator of Android system, it is used to start applications
  2. As the desktop of Android system, it is used to display and manage shortcut icons or other desktop components of applications.

Launcher startup process:

  1. First, the systemserver process will start PackageManagerService during startup. After PackageManagerService is started, the applications in the system will be installed. Then AMS that has been started before will start the Launcher. The entry to start the Launcher is the systemReady method of AMS, which is called in the startOtherServices method of systemserver. (Note: AMS is started in the startBootstrapServices() method).
  2. The systemReady method is finally started through Intent, where the action is: Intent ACTION_ Main and Category are Intent Category_ HOME. The startup method is similar to the normal activity method.

Application icon display process in Launcher:

  1. First, all application information is loaded by creating a HandlerThread internally, and then calling the handler loop to load all applications. Then pass the allApps information to the adapter of an AllAppsRecyclerView control for display.
  2. Launcher displays the shortcut icon of the application installed by the system in the form of workspace. Each workspace is used to describe an abstract desktop. It is composed of N screens, each screen is divided into n cells, and each cell is used to display the shortcut icon of an application.

The general process is as follows:

8.DirectBoot mode

Did you start the Launcher like this? It's not that simple. There's a little twists and turns in the middle! Searching the system source code, you can find that there is an Activity in Settings that also declares CATEGORY_HOME, i.e. FallbackHome. If the lock screen mode is set to none, you may see a "android is starting" page after restarting, and then the Launcher really appears.

So the question is, since there are two pages that declare CATEGORY - HOME, why doesn't a selection box appear after startup to let users choose which to start? According to the official information, a DirectBoot mode has been added after Android 7.0, which is roughly as follows:

When the device is powered on but the user has not unlocked the device, Android 7.0 will run in a safe "direct start" mode. To support this mode, the system provides two storage locations for data:

  • Credential encrypted storage, which is the default storage location and is only available after the user unlocks the device.
  • Device encrypted storage, which can be used in "direct start" mode and after the user unlocks the device.

By default, apps do not run in direct launch mode. If your application needs to operate in "direct launch" mode, you can register the application components that should run in this mode.

... details:

ok! Continue to compare the two applications. FallbackHome declares android:directBootAware="true" in manifestchina and its priority is - 1000, but not in Launcher. Therefore, only FallbackHome page will be retrieved at the beginning of startup, and the application selection dialog box will not appear. Then, the * * FallbackHome interface will always check whether the conditions for waking up the normal Launcher are met. If ok, finish yourself** The code snippet is as follows:

private void maybeFinish() {
    if (getSystemService(UserManager.class).isUserUnlocked()) {
        final Intent homeIntent = new Intent(Intent.ACTION_MAIN)
        final ResolveInfo homeInfo = getPackageManager().resolveActivity(homeIntent, 0);
        if (Objects.equals(getPackageName(), homeInfo.activityInfo.packageName)) {
            if (UserManager.isSplitSystemUser()
                && UserHandle.myUserId() == UserHandle.USER_SYSTEM) {
                // This avoids the situation where the system user has no home activity after
                // SUW and this activity continues to throw out warnings. See b/28870689.
            Log.d(TAG, "User unlocked but no home; let's hope someone enables one soon?");
            mHandler.sendEmptyMessageDelayed(0, 500);
        } else {
            Log.d(TAG, "User unlocked and real home found; let's go!");
                SystemClock.uptimeMillis(), false);

Then, after a series of logical processing, the Launcher finally appeared.

Topics: Java Android