Android system architecture and system source code directory

foreword

The technical blog can finally resume the normal update speed. The reason is that the first draft of the advanced book I wrote has been completed. I secretly thought that it will be the most in-depth Android application book. It can be said that it is "Android Development Art Exploration" 's companion piece. In the last chapter of this book, I will analyze the underlying source code of Android, but after all, it is an Android application development book, and reading the underlying source code can only lead you to a door. Therefore, in the blog, I will open a new series of "system source code analysis", which starts with this article.

1.Android system architecture

The Android system architecture is divided into five layers, from top to bottom, the application layer, the application framework layer, the system runtime layer, the hardware abstraction layer and the Linux kernel layer. 
write picture description here

application layer

The system built-in applications and non-system-level applications belong to the application layer. Responsible for direct interaction with users, usually developed in Java .

Application Framework Layer (Java Framework)

The application framework layer provides developers with the APIs they need to develop applications. We usually call the APIs provided by this layer when developing applications, including system applications. This layer is written by Java code and can be called Java  Framework. Let's take a look at the main components provided by this layer.

name Function description
Activity Manager Manage individual application lifecycles and the usual navigation fallbacks
Location Manager Provide geographic location and positioning function services
Package Manager Manage all apps installed in Android system
Notification Manager Enables applications to display custom prompts in the status bar
Resource Manager Provide various non-code resources used by the application, such as localization strings, pictures, layout files, color files, etc.
Telephony Manager Manage all mobile device features
Window Manager Manage all open window programs
Content Providers Enables data sharing between different applications
View System Building the basic components of an application

Table 1

System runtime layer (Native)

The system runtime layer is divided into two parts, which are the C/C++ program library and the Android runtime library. They are introduced separately below.

1. C/C++ library

The C/C++ program library can be used by different components in the Android system and provide services for developers through the application framework. The main C/C++ program libraries are shown in Table 2 below.

name Function description
OpenGL ES 3D drawing function library
Libc Standard C system function library inherited from BSD, specially customized for embedded Linux-based devices
Media Framework Multimedia library that supports recording and playback of a variety of commonly used audio and video formats.
SQLite Lightweight relational database engine
SGL The underlying 2D graphics rendering engine
SSL Secure Sockets Layer is a security protocol that provides security and data integrity for network communications
FreeType Portable font engine that provides a unified interface to access multiple font format files

Table 2

2. Android runtime library

运行时库又分为核心库和ART(5.0系统之后,Dalvik虚拟机被ART取代)。核心库提供了Java语言核心库的大多数功能,这样开发者可以使用Java语言来编写Android应用。相较于JVM,Dalvik虚拟机是专门为移动设备定制的,允许在有限的内存中同时运行多个虚拟机的实例,并且每一个Dalvik 应用作为一个独立的Linux 进程执行。独立的进程可以防止在虚拟机崩溃的时候所有程序都被关闭。而替代Dalvik虚拟机的ART 的机制与Dalvik 不同。在Dalvik下,应用每次运行的时候,字节码都需要通过即时编译器转换为机器码,这会拖慢应用的运行效率,而在ART 环境中,应用在第一次安装的时候,字节码就会预先编译成机器码,使其成为真正的本地应用。

硬件抽象层(HAL)

硬件抽象层是位于操作系统内核与硬件电路之间的接口层,其目的在于将硬件抽象化,为了保护硬件厂商的知识产权,它隐藏了特定平台的硬件接口细节,为操作系统提供虚拟硬件平台,使其具有硬件无关性,可在多种平台上进行移植。 从软硬件测试的角度来看,软硬件的测试工作都可分别基于硬件抽象层来完成,使得软硬件测试工作的并行进行成为可能。通俗来讲,就是将控制硬件的动作放在硬件抽象层中。

Linux内核层

Android 的核心系统服务基于Linux 内核,在此基础上添加了部分Android专用的驱动。系统的安全性、内存管理、进程管理、网络协议栈和驱动模型等都依赖于该内核。 
Android系统的五层架构就讲到这,了解以上的知识对以后分析系统源码有很大的帮助。

2.Android系统源码目录

我们要先了解Android系统源码目录,为后期源码学习打下基础。关于源码的阅读,你可以访问http://androidxref.com/来阅读系统源码。当然,最好是将源码下载下来。下载源码可以使用清华大学开源软件镜像站提供的Android 镜像:https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ 。如果觉得麻烦也可以查找国内的网盘进行下载,推荐使用该百度网盘地址下载:http://pan.baidu.com/s/1ngsZs,它提供了多个Android版本的的源码下载。

整体结构

各个版本的源码目录基本是类似,如果是编译后的源码目录会多增加一个out文件夹,用来存储编译产生的文件。Android7.0的根目录结构说明如下表所示。

Android源码根目录 描述
abi 应用程序二进制接口
art 全新的ART运行环境
bionic 系统C库
bootable 启动引导相关代码
build 存放系统编译规则及generic等基础开发包配置
cts Android兼容性测试套件标准
dalvik dalvik虚拟机
developers 开发者目录
development 应用程序开发相关
device 设备相关配置
docs 参考文档目录
external 开源模组相关文件
frameworks 应用程序框架,Android系统核心部分,由Java和C++编写
hardware 主要是硬件抽象层的代码
libcore 核心库相关文件
libnativehelper 动态库,实现JNI库的基础
ndk NDK相关代码,帮助开发人员在应用程序中嵌入C/C++代码
out 编译完成后代码输出在此目录
packages 应用程序包
pdk Plug Development Kit 的缩写,本地开发套件
platform_testing 平台测试
prebuilts x86和arm架构下预编译的一些资源
sdk sdk和模拟器
system 底层文件系统库、应用和组件
toolchain 工具链文件
tools 工具文件
Makefile 全局Makefile文件,用来定义编译规则

表3

从表3可以看出,系统源码分类清晰,并且内容庞大且复杂。接下来分析packages中的内容,也就是应用层部分。

应用层部分

应用层位于整个Android系统的最上层,开发者开发的应用程序以及系统内置的应用程序都是在应用层。源码根目录中的packages目录对应着系统应用层。它的目录结构如表4所示。

packages目录 描述
apps 核心应用程序
experimental 第三方应用程序
inputmethods 输入法目录
providers 内容提供者目录
screensavers 屏幕保护
services 通信服务
wallpapers 墙纸

表4

从目录结构可以发现,packages目录存放着系统核心应用程序、第三方的应用程序和输入法等等,这些应用都是运行在系统应用层的,因此packages目录对应着系统的应用层。

应用框架层部分

应用框架层是系统的核心部分,一方面向上提供接口给应用层调用,另一方面向下与C/C++程序库以及硬件抽象层等进行衔接。 应用框架层的主要实现代码在/frameworks/base和/frameworks/av目录下,其中/frameworks/base目录结构如表5所示。

/frameworks/base目录 描述 /frameworks/base目录 描述
api 定义API cmds 重要命令:am、app_proce等
core 核心库 data 字体和声音等数据文件
docs 文档 graphics 图形图像相关
include 头文件 keystore 和数据签名证书相关
libs location 地理位置相关库
media 多媒体相关库 native 本地库
nfc-extras NFC related obex Bluetooth transmission
opengl 2D/3D graphics API packages Settings, TTS, VPN program
sax XML parser services system service
telephony Telephone communication management test-runner Test tool related
tests test related tools tool
wifi wifi wireless network    

table 5

C/C++ library part

The C/C++ libraries in the system runtime layer (Native) are of various types and powerful functions. The C/C++ libraries are not completely in one directory. Here are some commonly used and more important C/C++ libraries. directory location.

directory location describe
bionic/ A system C library developed by Google and open sourced under the BSD license.
/ frameworks / av / media System Media Library
/frameworks/native/opengl Third-party graphics rendering library
/frameworks/native/services/surfaceflinger Graphics display library, mainly responsible for graphics rendering, overlay and drawing functions
external/sqlite C++ Implementation of Lightweight Relational Database SQLite

Table 6

After talking about the C/C++ library part, we have given the rest in Table 3: the code of the Android runtime library is placed in the art/ directory. The code of the hardware abstraction layer is in the hardware/ directory. This part is the biggest change of the mobile phone manufacturer. It will be implemented differently according to the hardware platform used by the mobile phone terminal.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394308&siteId=291194637