One, foreword

1. Introduction to Android platform layering

Android system from top to bottom is mainly divided into the following layers:

System application layer: mainly some built-in apps of the system and apps developed by developers;

Java API framework layer: mainly the development of the required Android and Java class libraries;

Native C/C++ layer: Native layer, mainly C and C++ written some class libraries, and the Java Framework layer call each other;

Android Runtime layer: mainly includes the Runtime virtual machine and the Runtime required core libraries;

Hardware Abstraction Layer: Abstraction of Bluetooth, audio and video, sensors, camera components;

Linux kernel layer: mainly Linux operating system and various hardware drivers;

2. Simplified startup process of Android system

Boot ROM — Boot Loader — Kernel — init — Zygote — SystemServer — App

After the device is powered on, a small program of loader is started from a specific area of ROM, and then the Linux kernel is pulled up by Loader. The first user process init is started, and the init process starts the Zygote process, and the Zygote process starts the SystemServer process. Finally, launch the application process Launcher to display the desktop icon;

Brief analysis of the main process

1. Init process

1) Create and mount the file directory required for startup

2) Initialize and start the property service

Rc configuration file and start Zygote

2. Zygote process

All Java application processes and SystemServer processes are incubated by Zygote through Linux fork(). Zgyote is the first Android ART virtual machine, which communicates with other processes through sockets. This other process mainly refers to the SystemServer process. As a server, Zygote process has the following functions.

1) Responsible for creating Java virtual machines;

2) Load system resources;

Start the SystemServer process.

4) Wait for common application requests and create application process, usually through AMS requests; The waiting process is in the runSelectLoop function;

The SystemServer process

In the SystemServer main function, the main function calls the run function. The main process is as follows:

Comment 1 creates the main thread looper first;

SystemContext is created in comment 2;

Comments 3, 4, 5 Start various services;

Note 6 Enable the loop;

PrepareMainLooper (); // Create message Looper looper.prepareMainLooper (); // create the system Context createSystemContext(); //2 // Start services. traceBeginAndSlog("StartServices"); // Start the boot service startBootstrapServices(); // start the core service startCoreServices(); // startOtherServices startOtherServices(); //5 // Loop forever. Looper.loop(); / / 6Copy the code

The function in note 2 is as follows:

private void createSystemContext() {
    ActivityThread activityThread = ActivityThread.systemMain();
    mSystemContext = activityThread.getSystemContext();
    mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);

    final Context systemUiContext = activityThread.getSystemUiContext();
    systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
Copy the code

The function that starts the boot service in comment 3 is shown below;

The startService at comment A calls mservices.add (service), which is an ArrayList of type SystemService, used to store all opened services.

Annotate the method will be called b ServiceManager. The addService (Context. ACTIVITY_SERVICE, this, true); Add AMS to the ServiceManager so that AMS can be used for communication between Binder processes;

Here we take an example of AMS, which is very important in boot services. We will introduce AMS separately later. There are many other important services, such as WMS, PMS, PKMS, IMS, and so on. It is with the help of these system services to make the Android system between the various applications in order to run;

private void startBootstrapServices() { // Activity manager runs the show. traceBeginAndSlog("StartActivityManager"); mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); //a mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); traceEnd(); // Set up the Application instance for the system process and get started. TraceBeginAndSlog ("SetSystemProcess"); mActivityManagerService.setSystemProcess(); //b traceEnd(); ,,,,}Copy the code