SoFunction
Updated on 2025-04-11

A brief analysis of Android's startup principle

Linux kernel startup

The startup process of an Android device begins with loading the Linux kernel. This kernel is the core of the entire Android system, responsible for managing hardware resources, providing system-level services, and providing an operating environment for applications.

Kernel Boot

The processor starts running when you press the power button of the device, or the device is restarting. At this moment, the processor executes a bootloader, usually in a specific location in the read-only memory of the device (such as flash memory). The task of booting the loader is to boot the device and load the Linux kernel.

Initialization and hardware configuration

Once the Linux kernel is loaded into memory, it begins performing tasks for initialization and hardware configuration. This includes the following key actions:

  • Initialization process management: Kernel initialization process management to ensure that the system can run multiple user space processes at the same time.

  • Initialize the device driver: The kernel loads and initializes the device drivers for interaction with the hardware. This includes hardware devices such as processors, memory, input devices, display screens, network interfaces, etc.

  • Start the first user space process: Usually, the kernel starts the first user-level process, which is the initialization process (init) of the Android system. This marks the switch from kernel space to user space.

Init process

During the startup process of an Android device,initThe process is an indispensable part. It plays the guiding force of the Android system, coordinating and managing the initialization and startup of the entire system.

Role of init process

Once the Linux kernel is started,initThe process becomes the first user-level process. Its main tasks include:

  • System initializationinitResponsible for initializing system resources of Android devices, such as mounting file systems, setting file permissions, and loading drivers.

  • Launch Android ComponentsinitProcess coordinates the launch of other important components of the Android system, such as the Zygote process and System Server.

  • Start the applicationinitThe process is also responsible for launching installed applications on the device.

  • Process ManagementinitMonitor and restart processes under its management to ensure system stability.

Configuration File

In earlier versions of Android, the system startup process relies on a set ofscript. However, modern Android systems usually useConfiguration file to define startup behavior.is a text file located on an Android device/initin the directory. It allows developers and device manufacturers to flexibly configure system initialization and startup processes.

The following is oneSimple example of configuration files:

# Example Configuration Fileservice servicename /system/bin/executable
    class main
    user root
    oneshot
  • serviceThe declaration defines a service.
  • servicenameis the name of the service.
  • /system/bin/executableSpecifies the binary executable file to be executed.
  • class mainSpecifies the category of the service.
  • user rootSpecifies the user to which the service belongs.
  • oneshotIndicates that the service will only run once.

The file allows you to configure the order, permissions, dependencies, and other details of the service startup.

Zygote process

The Zygote process is a crucial component during Android startup. It acts as an application incubator, preloading commonly used classes and resources to speed up application startup.

The role of the Zygote process

The main task of the Zygote process is to serve as a template for the application process. When a user starts a new application, the Zygote process quickly clones itself and creates a new application process. This fast cloning process reduces the application startup time because some classes and resources are preloaded into the new process.

Preload common classes and resources

When the Zygote process starts, it performs the following critical tasks:

  • Preload common classes: The Zygote process loads and initializes a set of commonly used Java classes, which are usually used by multiple applications. For example, Android core libraries, system service classes, etc.

  • Resource preload: Some application resources, such as icons, layout files, etc., will also be loaded in advance in the Zygote process. This reduces the time it takes for each application to reload these resources when it starts.

Reduce startup time

By preloading classes and resources in the Zygote process, the Android system can significantly reduce the startup time of the application. When a user starts an application, it actually cloned the Zygote process, which already contains certain key components that enable the application to start faster. This mechanism is particularly helpful in improving cold start performance.

Source Code Example

Here is a simple example showing how the Zygote process preloads common classes and resources:


public class ZygoteInit {
    public static void main(String[] args) {
        preloadClasses();
        // Start the application process        startAppProcess();
    }

    private static void preloadClasses() {
        // Preload common classes        Class<?>[] classesToPreload = {
            ,
            ,
            // ...
        };
        for (Class<?> clazz : classesToPreload) {
            preloadClass(clazz);
        }
    }
    
    private static void preloadClass(Class<?> clazz) {
        // Load and initialize the class    }

    private static void startAppProcess() {
        // Start the application process    }
}

The Zygote process is a key component in the Android startup process. It preloads commonly used classes and resources at startup to improve the startup speed of the application.

System Server

System Server is the core of the Android system and hosts multiple system services, such as Activity Manager, Package Manager, Window Manager, etc. These services are started in the System Server process, providing critical system functionality for applications.

The role of System Server

The main tasks of the System Server process include:

  • Start the system service: System Server starts multiple system services, which provide core functions of the Android system, such as application management, package management, window management, etc. Each service is responsible for a specific task.

  • Application Lifecycle Management: The System Server Activity Manager service is responsible for managing the application's life cycle, including the start, pause, stop, and destruction of the application.

  • Application Package Management: The Package Manager service is responsible for installing, uninstalling, and maintaining application packages, as well as handling application permissions and manifest files.

  • Window Management: The Window Manager service is responsible for managing the application's windows and user interface, including the creation, adjustment and display of windows.

  • Other system services: In addition, System Server has also launched other important system services, such as notification management, power management, network management, etc., to support the normal operation of applications and system functions.

Startup process

When the Android device starts, the Linux kernel starts and then starts the init process. The init process is responsible for starting the System Server process. Once System Server starts, it starts and initializes the system services. These services are created and run in the System Server process.

Source Code Example

Here is a simplified example showing how the System Server process starts and initializes multiple system services:

public class SystemServer {
    public static void main(String[] args) {
        // Start the System Server process
        // Start the Activity Manager service        ActivityManagerService ams = new ActivityManagerService();
        ();

        // Start Package Manager service        PackageManagerService pms = new PackageManagerService();
        ();

        // Start Window Manager service        WindowManagerService wms = new WindowManagerService();
        ();

        // Start other system services        // ...
    }
}

This example shows how the System Server process starts and initializes multiple system services.

Application process start

In Android, the startup of an application is a carefully coordinated process, co-hosted by Activity Manager and Zygote processes.

Activity Manager role

Activity Manager is a key component in the Android system and is responsible for the life cycle management of applications. Its main tasks include:

  • Application Startup Request: When the user starts an application, Activity Manager intercepts the request and is responsible for starting the corresponding application process.

  • Lifecycle Management: Activity Manager is responsible for tracking the life cycle of an application, including the activities of creating, pausing, stopping, and destroying applications.

  • Task stack management: Activity Manager manages task stacks to ensure that multiple applications are displayed correctly on the screen at the same time.

Participation of Zygote process

When Activity Manager receives a request to start the application, it works with the Zygote process to quickly create new application processes. Here are the basic steps for starting an application process:

  • Activity Manager Request: Activity Manager sends a request to the Zygote process, requesting to create a new application process.

  • Zygote process cloning: The Zygote process clones itself and creates a new process, which will become the host of the application to be started.

  • Resource Sharing: The new process will inherit some classes and resources that have been loaded in the Zygote process. Preloading of these classes and resources reduces the startup time of the application.

  • Application code loading: The new process loads the application's code, initializes the application's entry point (usually MainActivity), and starts the application's lifecycle.

Source Code Example

Here is a simplified example showing how Activity Manager requests the Zygote process to create a new application process:

public class ActivityManagerService {
    public void startActivity(Intent intent) {
        // Process startup requests and determine the application and components to be started        String packageName = ().getPackageName();
        String className = ().getClassName();
        
        // Send a request to the Zygote process to request the creation of a new application process        (packageName, className);
    }
}

Activity Manager is responsible for startup request processing and application lifecycle management, while the Zygote process helps create new application processes and speed up startup through resource sharing.

Summarize

Android startup principle involves multiple key components, including the Linux kernel, init process, Zygote process, System Server, and Activity Manager. Understanding the interaction and function between these components is very important for developers and can help them optimize the performance of their applications and provide a better user experience. At the same time, optimizing the Android application startup process is also an important step to improve application competitiveness.

The above is a detailed analysis of the startup principle of Android. For more information about the startup principle of Android, please pay attention to my other related articles!