[TOC]
preface
The underlying Kernel space of Android is based on [Linux Kernel], while the upper user space is composed of Native system library, VIRTUAL machine running environment and framework layer. The system is connected with [Kernel space] and [user space] through Syscall. The user space is mainly written by C++ and Java code, and the Java layer and Native layer (C++/C) of the user space are opened through JNI technology, so as to connect the whole system.
I. System architecture
Although The Android system is very large and complicated, which requires a comprehensive technology stack, the overall architecture design is clear. In order to make us have a general overview of the whole situation and generally understand the knowledge level involved in the Android system in the process of learning Android, we first introduce a classic hierarchical architecture diagram provided by Google.
1. A classic architecture layering diagram
As can be seen from the static hierarchical architecture diagram above, the Android system is divided into:
- The Linux kernel
- HAL (New Android architecture detail specific split into a Hardware Abstraction (HAL) layer)
- System Native library and Android runtime environment
- Java framework layer
- The application layer
Each tier provides different services:
* The low-level structure provides basic services such as file systems, memory management, I/O operations, and so on. * The high-level structure is built on top of the low-level structure to provide specific services such as UI controls and file access.Copy the code
2. Hierarchical diagram of sub-modules and subsystems with finer strength for each layer
In fact, each of the five layers of architecture contains a large number of sub-modules or subsystems, as shown below:
3. Brief introduction of each layer
-
** Application layer: ** applications are programs written in the Java language that run on virtual machines, such as
- Email client
- SMS short message application
- Calendar, etc.
-
** Application Framework layer: ** This layer is the API framework used to write core applications published by Google. Developers can also use these frameworks to develop their own applications. This simplifies the structure of application development, but the development principles of the framework must be followed.
-
** System runtime (C/C++ library and Android runtime) layer: ** When using the Android application framework, the Android system will support the various components we use through some C/C++ libraries, such as SQLite (relational database), Webkit (Web browser engine), so that it can better serve us.
-
**Linux kernel layer: **Android core system services are given to the Linux2.6 kernel, such as
- security
- Memory management
- Process management
- The network protocol stack and the driver model depend on the kernel, such as the driver model
- Binder Internet Process Connection driver
- A special android driver with a separate device node that provides interprocess communication
Through the Android system architecture diagram, we not only have a macro understanding of the Android system, but also point out the direction for our study and practice:
- For Android application development, usually work should focus on Android application framework layer and application layer;
- For Android system development, usually work should focus on the Android system library and Android runtime;
- For Android driver development, usually work should focus on the Android Linux kernel
Of course, as a friend who strives to become an excellent programmer, we also have the opportunity to step beyond the technical requirements of our work and further understand our Android system in the process of continuous learning and progress.
4. Understand Android system architecture from the perspective of application life cycle:
Ii. Further understand each layer of Android system architecture
The 5-layer architecture diagram provided by Google is very classic, but in order to have a better understanding of the Android system architecture and its interlocking internal connections, this article will introduceSystem startup architecture diagram, to further reveal the full picture of Android system with layered architecture:The Android system Boot process from bottom to top is booted by the Boot Loader and then entered successively-> Kernel -> Native -> Framework -> AppHere is a brief description of each process:
1. The Loader layer
- Boot ROM: When the phone is in the shutdown state, long press the Power key to Power up, the Boot chip starts to execute from the preset code solidified in ROM, and then load the Boot program into RAM;
- Boot Loader: this is the Boot program before starting the Android system. It mainly checks the RAM and initializes hardware parameters.
2. The Linux kernel layer
Underneath Android is the Linux kernel. For example, the Android Runtime (ART for ART Virtual Machine) eventually calls the underlying Linux kernel to handle low-level functions such as threading and low-level memory management. Using the Linux kernel allows Android to take advantage of the Linux kernel’s security mechanisms for Android and also allows device manufacturers to develop hardware drivers for the kernel. With the improvement of Android, with the improvement of the product, many times, the application level App simple call system API can not meet our development needs, then at this time, we have to go deep into the bottom, understand NDK, learn JNI and other advanced programming technology.
2.1 What is the NDK?
- [Definition] :
- Native Development Kit is a Development Kit for Android
- [Role] :
- Quickly develop C, C++ dynamic library, and automatically package so and application into APK
- You can use JNI to interact with native code (such as C and C++) in Android through the NDK
- [Advantages] :
- High efficiency, high code security, cross-platform
2.2 What is JNI?
- [Definition] :
- Java Native Interface
- [Role] :
- JNI enables Java to call C++
Process loading process:
- Start the Kernel swapper process (pid=0):
- This process is also called idle process. It is the first process that the Kernel starts from nothing to something during system initialization
- This process is used to initialize process management, memory management, loading Display, Camera Driver, Binder Driver and other related work;
- Start kthreadd (pid=2) :
- This process is a Linux kernel process that creates kernel daemons such as kworkder, ksoftirqd, and Thermal.
- The Kthreadd process is the granddaddy of all kernel processes.
3. Hardware Abstraction Layer (HAL)
- The Hardware Abstraction Layer (HAL) provides a standard interface to expose device hardware functionality to higher-level Java API frameworks.
- HAL contains multiple library modules, each of which implements a set of interfaces for a specific type of hardware component, such as a WIFI/ Bluetooth module, for which the Android system loads a library module when the framework API requests access to device hardware.
4.Android Runtime & Core library and Native System library
Android application program is developed and written in Java language, the program is executed in the Android operating environment; Android operating environment is divided into two parts :** Core Labraries ** and **Dalvik Virtual Machine **
4.1Android Runtime Android Runtime environment
- Core Libraries (android development Libraries)
- Android contains a collection of core libraries that provide most of the functions in the Java language API. The core libraries are compatible with most of the functions that the Java language needs to call, and also contain some of the Android core apis. Such as Android. OS, Android.net, Android. media and so on.
- Dalvik Virtual Machine (Dalvik Virtual Machine)
- Dalvik virtual machine is a register-based Java VIRTUAL machine. Dalvik virtual machine mainly performs important functions such as life cycle management, stack management, thread management, security and exception management and garbage collection.
- Android applications are different from J2ME (Java ME, also known as Java Platform, Micro Edition) applications. Each Android application has a proprietary process, and instead of multiple applications running in a virtual machine, Instead, each Android application has an instance of the Dalivik Virtual Machine and executes in that instance.
- The Dalvik VIRTUAL machine is a region-based Java virtual machine, rather than a traditional stack-based virtual machine, with optimized memory resource usage and support for multiple virtual machines. It is important to note that, unlike J2me, Android programs do not execute compiled bytecode in the virtual machine, but instead convert Java bytecode into intermediate code in dex format using the conversion tool DX.
The Android 5.0 belowThe version of theDalvik virtual machine. **Android 5.0+** runsART VM (Android Runtime)
For devices running Android 5.0 (API level 21) or higher, each application runs in its own process and has its own Android Runtime (ART VIRTUAL machine) instance.
ART is written to run multiple virtual machines on low-memory devices by executing DEX files, a bytecode format designed for Android that is optimized to use very little memory.
A compilation tool chain (such as Jack) compiles Java source code into DEX bytecode, making it available to run on the Android platform.
4.4.1 Dalvik virtual machine
Each Android application is an instance of the Dalvik virtual machine, running in its own process. The Dalvik Virtual machine is designed to be, Multiple VMS can be efficiently run on one device. The Dalvik VM executable file format is. Dex, which is a compressed format specially designed for Dalvik. Most virtual machines, including the JVM, are stack-based, while the Dalvik vm is register-based. Both architectures have their advantages and disadvantages. In general, stack-based machines require more instructions, while register-based machines require larger instructions The Dalvik VM relies on the Linux kernel to provide basic functions, such as threading and low-level memory management
Differences between Dalvik and JVM:
- 1) Dalvik is register-based, while JVM is stack based.
- 2) The Dalvik VIRTUAL machine runs its proprietary file format, Dex, while the Java Virtual machine runs Java bytecode.
- 3) Dex file format can reduce the overall file size and improve the class search speed of I/O operations.
- 4) Each APP corresponds to an independent virtual machine instance, and each thread of APP corresponds to a Linux thread.
- 5) There is a special virtual machine process, Zygote, which is an incubator for virtual machine instances.
-
Zygote is created when the system boots up, and if the system needs a new virtual machine instance, it quickly copies itself to provide the system with the fastest data available
-
For some read-only system libraries, all virtual machine instances share a memory area with Zygote.
-
4.1.2 Some main functions of ART
Each application runs in its own process and has its own virtual machine instance.
-
ART can run multiple virtual machines on a device by executing a DEX file, a bytecode format file designed for Android that is optimized to use very little memory.
-
The main functions of ART include:
- AOT and JUST-in-time (JIT) compilation
- Optimized garbage Collection (GC)
- And debugging related support. Include:
- Special sampling analyzer
- Detailed diagnostic exception and crash reports
- And you can set up monitoring points to monitor specific fields
Note: Prior to Android version 5.0 (API level 21), Dalvik is the Android Runtime. If the user’s application works well on ART, it should also work on Dalvik, but not the other way around (downward compatibility)
Android also includes a set of core runtime libraries that provide most of the functionality of the Java programming language used by the Java API framework, including some Java 8 language functionality.
4.2Native System library (system runtime library, namely the Native C/C++ library)
This layer contains the basic libraries that support the normal operation of the entire system, and since most of these libraries are implemented in C/C++, some developers call them the “C library layer” to distinguish them from the application framework layer.
Many of Android’s core system components and services, such as ART and HAL, are built from native code, requiring native libraries written in C and C++. The Android platform provides Java framework apis to provide some of the functionality of the native libraries to applications. For example, you can access OpenGL ES through the Android framework’s Java OpenGL API to support drawing and manipulating 2D and 3D graphics in your application.
In summary, Android contains a collection of C/C++ libraries that are used by various components of the Android system. These capabilities are exposed to developers through Android’s Application Framework. If you are developing an application that requires C or C++ code, you can use the Android NDK to access some native platform libraries directly from native code.
Android NDK provides the Android native C/C++ development environment and tools. Native system library includes 9 subsystems:
- System C library
- Standard C system function library inherited from BSD
- Customized for embedded Linux based devices
- Media Manager Media management
- OpenCORE based on PacketVideo
- These libraries support playback and recording of many popular audio and video formats, as well as still image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, PNG
- Surface Manager Layer management
- Responsible for managing the interaction between display and access operations when executing multiple applications, as well as for display composition of 2D and 3D drawings
- LibWebCore
- Web browser Engine
- Drive the Android browser and embedded Web view
- SGL
- Basic 2D graphics engine library
- 3 d library (OpenGL | ES)
- 3D drawing function library based on OpenGL ES 1.0 API standard
- OpenGLESate is an open graphics library to support 3D effects
- FreeType – Bitmap and vector font rendering, providing bitmap and vector font rendering and display
- SQLite – a small relational database engine. A powerful and lightweight relational database engine available to all applications
- SSL – Handshake is implemented during communication on Andorid, which provides support for data communication
The Native system library mainly includes user-space daemons incubated by Init, HAL layer and boot animation, etc.
Start the init process (PID =1), which is the user process on Linux. The init process is the ancestor of all user processes.
Process loading process:
- The init process incubates user daemons such as Ueventd, Logd, Healthd, Installd, ADBD, and LMKD.
- Init also starts important services such as servicemanager(binder servicemanager) and bootanim(startup animation)
- The Init process incubates the Zygote process, which is the first Java process on the Android operating system (i.e. virtual machine process). The Zygote process is the parent of all Java processes. The Zygote process itself is incubated from the init process.
5.Java API Framework (Application Framework)
The application framework layer is also known as the “JAVA library”. The components provided by the application framework layer are typically written in the JAVA language. On the one hand, the Java library layer provides the API interface for the upper application program; On the other hand, it also covers the implementation of many system-level service processes, which are most directly related to Android application developers. Developers have full access to Android system applications to reuse framework apis provided by the system. By providing an open development platform, Android enables developers to create extremely rich and innovative applications. Developers are free to take advantage of the device’s hardware, access location information, run background services, set alarms, add notifications to the status bar, and much more. Developers can fully access and use the Framework APIS provided by the Android system. The application architecture is designed to simplify the reuse of components so that any application can publish its functionality and any other application can use it (subject to security restrictions imposed by the framework). This mechanism allows users to replace components. The entire feature set of Android OS can be used through apis written in the Java language. These apis form the building blocks required by Android applications to simplify the reuse of core modular system components and services. All applications are actually a set of services and systems. The Application Framework layer consists of 10 parts:
- Activity Manager – Manages the application life cycle, from installation to running to uninstallation. Provides universal navigation rollback (back) functionality
- Window Manager Window management
- Content Providers – Enable applications to access data from other applications, such as address books, or to share their own data
- View System – a rich and extensible collection of views for building UI applications. This includes lists, grids, text boxes, buttons, and even embedded Web browsers
- Notification Manager Notification Management – Enables all applications to display custom notifications in the status bar.
- Package Manager
- Telephony Manager Manages calls
- Resource Manager Resource management – Provides access to non-code resources such as localized strings, graphics, and layout files
- Location Manager
- XMPP Services XMPP Services
- XMPP(Extensible Messaging and Presence Protocol, formerly known as Jabber) is an XML-based open real-time communication Protocol. XMPP networks are server-based (that is, clients do not talk to each other directly) but also distributed. Unlike services such as AOL Live Messenger or MSN Messenger, XMPP has no central official server.
- Jabber.org has a large number of users on its public server, so some people misinterpret it as an official server, but in fact anyone can run an XMPP server on their own domain.
- A Jabber identifier (JID) is an account that a user uses to log in, often looking like an E-mail address, such as [email protected]; The first part is the user name, and the second part is the XMPP server domain name. The two fields are separated by the @ symbol.
Suppose Juliet ([email protected]) wants to talk to Romeo ([email protected]), whose accounts are on the servers of Capulet.com and Montague.net. When Juliette entered the message and pressed the send button, a chain of events took place:
Juliette’s XMPP client sends her information to the Capulet.com XMPP server. Capulet.com XMPP server Opens a connection to the Montague.net XMPP server. The Montague.net XMPP server sends the information to Romeo. If he is not currently online, store the information for later delivery.
Process loading process:
- Zygote is a Zygote process generated by fork after parsing init.rc.
- Load the ZygoteInit class and register the Zygote Socket server Socket
- Loading a VM
- Preload the preloadClasses class
- Preload the resource preloadResouces
- SystemServer is the first process incubated by Zygote. SystemServer is responsible for starting and managing the entire Java Framework, including ActivityManager. WindowManager, PackageManager, PowerManager and other services.
- Media Server process, fork from init process, is responsible for starting and managing the entire C++ framework, including AudioFlinger, Camera Service and other services.
6. The App layer
Mobile phones equipped with Android system have many applications running on virtual machine Dalvik. Through these applications, mobile phone users can have rich functional experience. Android comes with a core set of apps for email, messaging, calendar, Internet browsing and contacts. Apps that come with the platform have no special status, just like apps that users can choose to install. So third-party apps can become users’ default web browser, SMS Messenger, and even their default keyboard (with some exceptions, such as the system’s Settings app). System applications are available to users’ applications and provide major functions that developers can access from their own applications. For example, if a user’s application wants to send SMS messages, the user can use the default SMS application to send SMS messages. System applications not only provide users with common applications in daily life, but also provide developers with key functions that can directly call system applications. Applications mounted on the system are engineering codes written by application development engineers, which are compiled and converted into executable machine code files by compilation tools for work, that is, Java Code →.class →.o → dex (Dalvik Executable) process loading process:
- The first App that the Zygote process hatches is the Launcher, which is the desktop App that users see;
- The Zygote process also creates Browser, Phone, Email and other App processes. Each App runs on at least one process.
- All App processes are generated by the Zygote process fork.
7.Syscall && JNI
- There is a SysCall layer between Native and Kernel. For further understanding, you can refer to the principle of Linux SysCall
- The link between Java layer and Native(C/C++) layer JNI, for further understanding, please refer to this Android JNI principle analysis
Afterword.
Welcome all friends of programming rivers and lakes to read and comment on my blog, if you feel that my article is helpful to your work, I feel gratified. You can also share it with other Android developers, and hope that more friends can contribute to the development of this great community and make the world of programming more powerful and meaningful.
I will devote more energy in the future to continue to strive for the output of high-quality articles for my friends, you are also welcome to follow me, thank you very much!!
At the same time, I also welcome friends to discuss Android knowledge with me. Here is my contact information :QQ 1101819159