One, foreword


When we click the application icon on the desktop of the mobile phone, the main Activity interface of the application is displayed on the desktop of the mobile phone and the application is launched. The seemingly simple process actually contains complex low-level interaction. The whole startup process involves many core knowledge points of the Android system.

Ii. Introduction to the startup process


First of all, the mobile phone desktop is an APP application called Launcher, which is provided by the mobile phone manufacturer. Different mobile phone manufacturers have developed a set of display desktops with their own UI style. We click on the APP icon of the desktop Launcher to open other apps.

The startup process of an APP can be divided into cold startup and hot startup.

  • Cold start: If the APP process is not started, the system forks a child process to load the Application and start the Activity. This is called cold start.

  • Hot start: The APP process is started and running in the background. All the system does is bring the Activity of the APP to the foreground.

Cold startup is an application that starts completely from zero, and there’s a lot more involved, so let’s start with the cold startup process of an APP.

Analysis of startup process


In general, cold start consists of the following steps:

  • Start the APP process: When we click on the APP icon of the desktop program Launcher, the program calls the startActivity() function, which communicates with the Binder across processes and sends messages to the System_server process. In the system_server process, AMS tells the Zygote process to fork out a child process (APP process) through socket communication.

  • Start the main thread: Once the APP starts, it instantiates an ActivityThread, executes its main function, creates ApplicationThread, Looper, and Handler objects, and opens the main thread message loop looper.loop ().

  • Create and initialize Application and Activity: The Main function of the ActivityThread communicates with the Binder by calling attach and notifies the System_server process of attachApplication. In attachApplication, AMS uses the bindApplication and scheduleLaunchActivity methods to notify the main thread Handler of the APP process. Initializes the Application and Activity of the APP process and executes the Application and Activity lifecycle.

  • UI layout and drawing: When the main thread Handler initializes the Activity, it creates the PhoneWindow, initializes the DecorView, and adds the layout to the DecorView’s ContentView. ContentView, which corresponds to the outermost parent layout of the layout.xml layout file set in the Activity’s setContentView. The Android view hierarchy is shown below:

At this point, the application startup process is complete and the source code details are not analyzed too much.

Here are some of the key roles involved in the startup process.

3.1 the Zygote process

During the startup of the Android system, the Linux kernel is started and the init process is started by loading the init.rc file. The init process then generates Zygote, the first Java process on Android, by parsing the init.rc file fork. The Zygote process is then responsible for incubating System Server and APP processes.

3.2 SystemServer process

  • Generated by Zygote process fork, SystemServer is the first process hatched by Zygote.
  • It is responsible for starting and managing the entire Java Framework. Important services in the system are started in this process, such as ActivityManagerService, PackageManagerService, and WindowManagerService.

3.3 APP process

  • The first process Zygote incubates in the App layer is the Launcher process, the mobile phone’s desktop App.
  • Zygote also incubates Browser, Email, Phone and other APP processes. Each APP runs on at least one process.
  • All APP processes are generated by Zygote process fork.

3.4 Client/Server Mode in Android

The well-known front-end (Web\Android\iOS) communicates with the server through the network, which is the embodiment of the client-server mode. In the Android Framework, the creation and life cycle of the four major components also communicate through this mode:

  • Server refers to the SystemServer process. This process provides many services, such as AMS, PMS, WMS, etc. All APP processes can communicate with it.
  • A client is a separate APP process.

In Android development, we can open an APP by using the name of the Package and the name of the Activity class. In fact, the startActivity() method in the business code in the project does not directly create the process and pull up the APP. Instead, the request is passed to SystemServer’s AMS through a series of calls. After AMS receives the request from the client, it notifies zygote to fork a new process to start our target APP. The life cycle process of all activities in the APP is uniformly scheduled by AMS (SystemServer process) and specifically completed in the APP’s own process.

This process involves three processes: APP process, SystemServer process, Zygote process.

  • APP process communicates with SystemServer process through Binder mechanism.
  • The SystemServer process communicates with Zygote process through Socket.

3.5 Android Binder Mechanism

In Android system, the space of a process is divided into user space and kernel space. In-process user space and kernel space can interact with each other. The user space between processes is isolated, and only the kernel space can interact with data.

The interaction between APP process and SystemServer process is inter-process communication (IPC) through Binder mechanism to realize the kernel data interaction process between processes. Android has two Binder interfaces specifically designed for interactive use.

  • IApplicationThread: Serves as an interface for system processes to request application processes.
  • IActivityManager: an interface for application processes to request system processes.

Binder interfaces of App and SystemServer processes are shown as follows:

Four,


  • In Android, all applications are a separate process.
  • Each application process is fork by the Zygote process.
  • Application startup is a complex cross-process task. The application startup process mainly revolves around the ActivityManagerService of SystemServer and the ActivityThread of application processes.

The startup process of an APP has a lot of content, which can be understood through four parts: the response screen click of the Launcher, the creation of the APP process, Application initialization, UI layout and drawing.