An overview of the
Stand on the shoulders of giants to learn, this blog reference Gityuan, used to take notes and consolidate knowledge
The Android framework
The Linux kernel
HAL
System Native library and Android Runtime
Java framework layer
The application layer
The 5 layers provided by Google are very classic, but in order to further see the Android system architecture, to interpret the whole picture of Android from the perspective of process, to interpret the internal connection of the whole picture of Android
Android startup architecture diagram
The startup process of Android system is from bottom to top in the figure above. Boot Loader boots the Android system and then enters Kernel -> Native-> Framework-> App. The following steps are briefly introduced
Boot (about Loader layer)
- Boot ROM: When the phone is in the shutdown state, long press the Power key to Boot the chip from solidified in
ROM
The preset code starts executing and then loads the program intoRAM
- Boot Loader: this is the Boot program before starting the Android system. It mainly checks the RAM and initializes hardware parameters
About the Kernel (Linux Kernel Layer)
The Android platform is based on the Linux kernel. For example, ART virtual machine ultimately calls functions performed by the Linux kernel. The Security mechanism of the Linux kernel provides corresponding guarantee for Android and allows device manufacturers to develop hardware drivers for the kernel
- Start the swapper process of the Kernel (PID =0), which is also called idle process, the system initialization process of the Kernel from nothing, the first process created, used for initialization process management, memory management, loading Display, Camera Driver, Binder Driver, And other related work
- Start the kthreadd process (PID =2), which is a Linux kernel process. It will create the kernel worker thread kworkder, soft interrupt thread ksoftirqd, thermal and other kernel daemon processes.
The Kthreadd process is the granddaddy of all kernel processes
About Hardware Leisure Layer (HAL)
The hardware Abstraction layer (HAL) provides the standard interface. HAL consists of multiple library modules, each of which implements a set of interfaces for a specific hardware component, such as a Wifi/ Bluetooth module, for which the Android system loads a library module when the framework API requests access to the hardware
Android Runtime and System libraries
Each application runs in its own process and has its own virtual machine instance. ART can run multiple virtual machines on the device by executing the DEX file. The dex file is a bytecode format specially designed for Android, which is optimized and uses very little memory. Optimized garbage collection (GC) and debugging related support
The Native system library here mainly includes user-space daemons incubated by init, HAL layer, startup animation, etc. Start init process (PID =1), which is the user process of Linux system. Init process is the ancestor of all user processes
- The init process incubates user daemons such as Ueventd, Logd, Healthd, InstalLD, ADBD, and LMKD
- The init process is also started
ServiceManager
(Binder’s butler),bootanim
(boot animation) and other important services - The init process also incubates in addition to the Zygote process, which is the first Java process in Android (i.e., a virtual machine process),
Zygote is the parent of all Java processes
The Zygote process itself is hatched from the init process
The Framework layer
- Zygote process is generated by the init process after parsing init.rc file fork. Zygote process includes
- Load the Zygoteinit class and register the Zygote Socket server Socket
- Loading a VM
- Preload the PreloadClasses class
- Preload the resource PreLoadResouces
- The System Server process, derived from Zygote fork,
System Server was the first process hatched from Zygote
ActivityManagerService, WorkManagerService, PagerManagerService, PowerManagerService services such as - The Media Server process, created by init fork, is responsible for starting and managing the entire C++FrameWork, including AudioFlinger, Camera Service, etc
The App layer
- The first App process Zygote incubates is the Launcher, which is the desktop App that users see
- Zygote also creates App processes such as Browser, Phone, and Email. Each App runs on at least one process
- All App processes are created by Zygote fork
Syscall && JNI
- There is a layer of Syscall between Native and KerNel.
- The link between the Java layer and Native layer JNI
Important process
init
Zygote
ServiceMager
system_server
The init process
-
The first user-space process in Linux is init.main
-
The init process also starts ServiceManager (Binder’s butler), Bootanim (boot animation) and other important services
-
The Zygote process is the first Java process in Android (i.e. a virtual machine process). Zygote is the parent of all Java processes. The Zygote process itself is incubated by the Init process
The Zygote process
-
The parent process for all apps, zygoteinit.main
-
Zygote process is generated by the init process after parsing init.rc file fork. Zygote process includes
- Load the Zygoteinit class and register the Zygote Socket server Socket
- Loading a VM
- Preload the PreloadClasses class
- Preload the resource PreLoadResouces
-
System Server is the first process incubated by Zygote. System Server is responsible for starting and managing the entire Java FrameWork. Contains ActivityManagerService, WorkManagerService, PagerManagerService, PowerManagerService, and other services
System_server process
Main system_server process from the source point of view can be divided into boot services, core services and other services
- Bootstrap services (7) : ActivityManagerService, PowerManagerService, LightsService, DisplayManagerService, PackageManagerService, UserManagerService, SensorService;
- Core services (3) : BatteryService, UsageStatsService, WebViewUpdateService;
- Other services (70 +) : AlarmManagerService, VibratorService, etc.
ServiceManger process
Great steward of bInder services
ServiceManager is a daemon process in the communication process of Binder IPC, which is also a Binder itself, but it does not use the multithreaded model to communicate with Binder, but writes binders. C directly and Binder drivers to communicate. And there is only one binder_loop to read and process transactions. The benefit of this is simplicity and efficiency. The ServiceManager itself is relatively simple to query and register services
The flow chart
The startup mainly includes the following stages
- Open the Binder driver and allocate 128K of memory-mapped space by calling the mmap() method, binder_open
- Register as binder_become_context_manager for Binder services
- Verify selinux permissions to determine whether a process has the right to register to view a specified service
- Enter an infinite loop to process the request binder_loop from the Client
- Register a service by its name, and duplicate registrations remove previous registrations
- Death notification, when the process is dead, the binder_release method is called, and then binder_node_release is called, which issues the death notification callback
App process
- Start Starts the App Process activityThread. main
- The first App process hatched by Zygote was
Launcher
This is the desktop App that the user sees - Zygote will also create
Browser
.Phone
.Email
And other App processes, each App runs on at least one process - All App processes are run by
Zygote fork
And become
The difference between processes and threads
Process: Before each App starts, a process must be created. This process is created by Zygote fork. The process has an independent resource space, which is used to host the various activities /Service components of the App. Make apps run in Android Runtime. Most apps run in a single process, unless Android: Process is configured in the Androidmanifest.xml file, or a process is forked through Native code
Thread: Threads are familiar to us. For example, each new Thread().start creates a new Thread that has no independent space of its own, but shares resources with the process it is in
Take a look at process creation
- App initiator: The Launcher process is the process with which the application is launched from the desktop. When the remote process is launched from an App, the initiator process is the process with which the App is launched. The initiator process first sends information to the System_server process through Binder
- System_server Process: calls the process. start method and sends a request to Zygote Process to create a new Process through the Socket
- The zygote process: in the implementation
ZygoteInit.main()
After enteringrunSelectLoop()
The body of the loop, which executes when there is a client connectionZygoteConnection.runOnce()
Method, and then through layers of call after fork out a new application process - The new process: perform
handleChildProc
Method, set the process name, open the Binder driver, start a new binder thread, set the ART vm parameters, reflect the main method of the target class, that is, callActivityThread.main()
methods
The process.start () method is a blocking method that waits until the Process is created and returns a pid that responds