[Take the wind and waves] Android system launch process finishing

Foreword

For an Android application layer developer, understanding the startup process of the Android system is of great help to understanding the Android system. This contains a lot of details, and many of the previous steps include the native layer logic implemented in C/C++. As an application layer developer, it is difficult and unnecessary to master it too deeply. This article simply sorts out the startup process of the Android system, and friends who are interested in specific details can study in depth on their own.

Android system startup process

The startup process of Android system starts from pressing the power button to start the power supply to the Launcher application Startup is complete, here can be roughly divided into the following 7 steps.

1. Turn on the power to execute the BootLoader boot program

When the power is pressed, the chip code will be guided to execute from a predefined place (the predefined place is solidified in the ROM) , Load the bootloader BootLoader into RAM for execution. ROM and RAM are two parts of memory. The former is the abbreviation of Read-Only-Memmory. As the name suggests, it is read-only memory. Some information is stored in advance, such as the information needed when the system is started. The latter is the abbreviation of Read-Acess-Memmory. The written memory can dynamically write data during the running of the program. After the system is shut down, the data will be cleared.

2. BootLoader pulls up and executes the operating system

When BootLoader executes, it will pull up and run the operating system. BootLoader is a bootloader, a small program before the Android operating system starts to run, its role is to pull up and run the operating system.

3. The operating system starts the init process

The Android operating system is implemented based on the Linux kernel, so the Linux kernel starts to start at this time to perform system settings. After completing the system settings, it will first look for the init.rc script file in the system files and start the init process.

4. The init process starts

Init, you can judge by its name that its function is to do some initialization work. The init process is the first process in the user space of the Android system. The process number is 1, which is a key process in the Android system startup. As the first process, it is given many important responsibilities, which can be briefly summarized as:

(1) Create and mount the file directory needed to start the system.

(2) Initialize and start the attribute service. The attribute service here, similar to the registry manager in the Windows operating system, is used to record some information about users and software.

(3) Parse the init.rc configuration file and start the Zygote process.

5. The Zygote process starts

The Chinese translation of Zygote is “fertilized egg”, which is the beginning of life. From the literal meaning, we can roughly understand its status in the Android system . Dalvik/ART, the application process and the key service SystemServer of the running system are all created by the Zygote process, so it is generally called an incubator. Zygote needs to do a lot of work, which can be summarized as follows: (1) Create Dalvik/ART

(2) Enter the Java framework layer from the Native layer. In other words, Zygote created the Java framework layer. This step is achieved by calling the main method of the ZygoteInit class through the JNI method through the Native layer. The path of Zygote is: frameworks/base/core/java/com/android/internal/os/ZygoteInit.java

(3) In the main method of ZygoteInit, a Service-side Socket is created to wait for AMS Ask Zygote to create a new application process.

(4) In the main method of ZygoteInit, the SystemServer process is also created and started by fork.

6. Start the SytemServer process

SytemServer is a key service of the running system, mainly used to create system services, such as AMS, WMS, PMS, etc. Its main responsibilities are:

(1) Start the Binder thread pool. This process mainly uses ZygoteInit.nativeZygoteInit() to call the Native layer method to start the Binder thread pool, so that SystemServer can use Binder to communicate with other processes.

(2) Create SystemServerManager (SSM) and start various services. This process is implemented by calling the main method of SystemServer. You can refer to the following source code. SystemServerManager is used to create, start and lifecycle management of system services. The various services started here include boot services, core services, and other services: boot services include AMS, PowerMS, PackageMS, etc.; core services include BatteryService, etc.; other services include WMS, etc., and SystemUI is also started here.

 1 // =========SystemServer.java=========

2 public static void main(String[] args) {
3 new SystemServer().run();
4 }
5 private void run() {
6 ......
7 //Create Message Looper
8 Looper.prepareMainLooper();
9 // Load the dynamic library libandroid_servers.so and initialize the native service
10 System.loadLibrary("android_servers");
11 ......
12 //Initialize the system context
13 createSystemContext();
14 //Create SystemServiceManager
15 mSystemServiceManager = new SystemServiceManager(mSystemContext);
16 ......
17 //Start boot services, such as AMS, etc.
18 startBootstrapServices();
19 //Start core services
20 startCoreServices();
21 //Start other services, such as WMS, SystemUI, etc.
22 startOtherServices();
23 ....
24 }

What specific services are included in each service type can be Enter the corresponding method to view the 18th, 20th and 22nd lines in the above source code.

7. Start the Launcher

The last step of the system startup is to start an application to display the installed applications in the system. This application is the Launcher. This step is started by the AMS created by SystemServer. As mentioned in the previous point 5 “Zygote Process Startup”, a Socket is created to wait for AMS to request Zygote to create a new application. Launcher will request PowerManagerService to return the information of the installed applications in the system during the startup process, and encapsulate the information into a shortcut icon list and display it on the system screen, so that the user can start the corresponding application by clicking these icons. In summary, there are two points:

(1) As the launcher of the Android system, it is used to launch applications.

(2) As the desktop of the Android system, it is used to display and manage application shortcut icons or other desktop components.

Android system startup flow chart

According to the above process, the following flow chart can be obtained:

share picture

Conclusion

The content compiled in this article comes from the second chapter of “Android Advanced Decryption” by Liu Wangshu, Those who are interested in a deeper understanding can learn through this book. If there are any improper or incorrect descriptions in this article, please feel free to enlighten me, I am very grateful.

 1 //< span style="color: #008000;">=========SystemServer.java=========

2 public static void main(String[] args) {
3 new SystemServer().run();
4 }
5 private void run() {
6 ......
7 //Create Message Looper
8 Looper.prepareMainLooper();
9 // Load the dynamic library libandroid_servers.so and initialize the native service
10 System.loadLibrary("android_servers");
11 ......
12 //Initialize the system context
13 createSystemContext();
14 //Create SystemServiceManager
15 mSystemServiceManager = new SystemServiceManager(mSystemContext);
16 ......
17 //Start boot services, such as AMS, etc.
18 startBootstrapServices();
19 //Start core services
20 startCoreServices();
21 //Start other services, such as WMS, SystemUI, etc.
22 startOtherServices();
23 ....
24 }

Leave a Comment

Your email address will not be published.