** This is a serialized blog series, and I will continue to provide you with the most thorough possible analysis of Android source code
preface
There are many benefits of reading Android source code, can deepen our understanding of the system, can refer to elegant code implementation, can fundamentally find out the cause of some bugs… We should be thankful that Android is open source, all features can be seen and implemented, and all bugs can be traced.
I think everyone has more or less heard of the Android Open Source Project, AOSP(Android Open Source Project). Some people may not know how to download AOSP, and some people may download it, but they are afraid of the huge Android Source Project. I don’t know where to start.
So this article will start from the above two problems, specifically explain how to download and read the Android system source code.
How to download AOSP
Source code download is the beginning of our analysis of the source code, Android source code can be downloaded in full, can also be downloaded individually, we first introduce the full download
1.1 Full Download
Official documents, as long as the step by step in accordance with the above can be done, but due to the need to turn over the wall, the domestic can not be directly accessed, and the entire Android project source huge, even after the download is very slow, so it is better to use the domestic mirror.
I recommend tsinghua university open source mirror, address, this is also a complete tutorial, I will not copy and paste, but there is a point to note, you must prepare a relatively large disk, at least 60 G, not late compilation.
We analyze the source code is actually don’t need all of the code, because the AOSP includes not only the system source code, there are some tools code, such as aapt, adb, etc., these we don’t need, and even system source code, not all we all need to look at, if really see, all you in this life can not see out, so I still recommend a single download.
1.2 Single Download
Official address, such as we need to download the platform/frameworks/base/directory of the code, we can git clone https://android.googlesource.com/platform/frameworks/base, But there’s still the problem of going over the wall, and of course we can use mirror images.
Mirror address, such as we want to download the platform/frameworks/base/directory, use the git clone https://aosp.tuna.tsinghua.edu.cn/platform/frameworks/base, If you have enough bandwidth, you can usually download the individual source code you want in a few minutes.
If you want to download individual files, or search for file names and codes, you can visit http://androidxref.com/, which has some of the Android source code
1.3 AOSP Directory Structure
Let’s start with an architectural diagram of the entire Android project
We all know that the Android system is roughly divided into four layers from top to bottom, so we will explain the AOSP directory structure based on these four layers:
- Layer 1: Applications layer corresponds to platform/ Packages /apps in the root directory
- Layer 2: The Application Framework corresponds to platform/ Frameworks in the root directory
- Layer 3: Runtime layer includes runtime libraries and Android Runtime environment.
- Libraries include the liBC library for Platform/Bionic
- The Android runtime environment (Core Libraries) corresponds to platform/libcore in the root directory and the Dalvik Virtual Machine corresponds to platform/ Dalvik in the root directory. So the directory is platform/art
- The fourth layer: The Linux kernel layer corresponds to the kernel in the root directory, and each directory corresponds to a kernel version. Because Android needs to be compatible with various chips, it is listed below:
- The Piranere project contains kernel source code suitable for the platform being simulated.
- The MSM project contains source code for ADP1, ADP2, Nexus One, Nexus 4, Nexus 5, Nexus 6, Nexus 5X, Nexus 6P, Nexus 7 (2013), Pixel and Pixel XL, Use as a starting point for using the Qualcomm MSM chipset.
- The OMAP project is used for the PandaBoard and Galaxy Nexus and can be used as a starting point for using the TI OMap chipset.
- The Samsung project is for the Nexus S and can be used as a starting point for using the Samsung Hummingbird chipset.
- The Tegra project is available on the Xoom, Nexus 7 (2012), Nexus 9, and can be used as a starting point for using the NVIDIA Tegra chipset.
- The Exynos project contains kernel source code for Nexus 10, which can be used as a starting point for using the Samsung Exynos chipset.
- The x86_64 project contains the kernel source code for Nexus Player, which can be used as a starting point for using the Intel X86_64 chipset.
- The Hikey-Linaro project is for the Hikey reference board, which can be used as a starting point for using the HiSilicon 620 chipset.
- There is also a hardware abstraction layer (HAL) between layer 3 and layer 4 that corresponds to platform/ Hardware in the root directory
The current directory I download is as follows:
git clone https://aosp.tuna.tsinghua.edu.cn/platform/packages/apps/Launcher2
git clone https://aosp.tuna.tsinghua.edu.cn/platform/frameworks/base
git clone https://aosp.tuna.tsinghua.edu.cn/platform/frameworks/native
git clone https://aosp.tuna.tsinghua.edu.cn/platform/system/core
git clone https://aosp.tuna.tsinghua.edu.cn/platform/bionic
git clone https://aosp.tuna.tsinghua.edu.cn/platform/libcore
git clone https://aosp.tuna.tsinghua.edu.cn/platform/art
git clone https://aosp.tuna.tsinghua.edu.cn/kernel/msm
Ii. How to read AOSP
When we download the source code, we are confused because there is so much source code for AOSP that we need to clarify some questions:
- Which source code to read
- The order and manner in which source code is read
- What tools do you use to read
I’m going to start with these three questions one by one
2.1 What source code to read
This question is quite personal, because different people do different jobs. Some people are in application development and may be interested in Java layer stuff. Some people do Framework development and may be interested in the Framework layer; Some are engaged in hardware development and may be interested in the underlying implementation.
This all varies from person to person, but there is a point, can not blindly look at the source code without destination, because then eventually you will be submerged in the sea of AOSP, see a year and a half of what all see, but feel did not see through, others ask you the source code of things, can say one or two, but a deeper said, I do not know so.
So for AOSP source code, it’s not about much, it’s about excellence, you don’t try to read all the source code, you just have to dig deep into the part that you’re interested in, because even Google engineers can’t read all of AOSP.
For me, I am engaged in application layer development, I will mainly understand the following aspects of the source code:
- Android system startup process, application startup process, four component startup process, which will be included in the system startup section
- System commonly used service ActivityManagerService WindowManagerService etc., this will be included in the system services
- Communication mechanisms, primarily binders and handlers, will be included in the communications section
- Processes and threads are created, run, and destroyed, which will be listed in the processes section
- The process of drawing and displaying views, and the event distribution mechanism, will be covered in the graph drawing section
- Android VIRTUAL Machine ART runtime mechanism, class loading mechanism, Java annotations, Java reflection, this will be included in the virtual machine section
- Android optimization algorithms for Java collections, which will be included in the Java Basics section
2.2 Order and method of reading source code
2.2.1 Reading order
Reading source code is a cumulative process that cannot be accomplished overnight. After listing the source code that interests us, we need to make a reading plan and read what we read first and then what we read. This is also different from person to person, according to their own interests to be, what you want to read, that is the first.
Said I have been interested in, because interest is the best teacher, only interested in one thing, you will be motivated to learn, to study, will not feel tired, if I had to face some things you are not interested in, also boring don’t focus in the end, the degree of understanding is not deep, and is likely to lose confidence, finally gave up reading.
Of course, if you’re interested in several things, there are a few rules:
- Things are prioritised, just as trees take root in the earth, the earth first, then the trees, the basic things first
- Look at things that are related to each other, don’t look at boot up and then all of a sudden look at event distribution
2.2.2 Reading Methods
Android system covers a wide range, from the upper application, to the Framework, then to Libraries and hardware, from Java layer to C++ layer, just like a building with dozens of floors, each floor has stairs and elevators, what we need to do is to shuttle up and down in the building.
When we read the source code of a certain knowledge point, different knowledge points can be read in different ways, some are suitable to read from the bottom up, such as the system startup process, I read from the event beginning, init. CPP, then to the Zygote process, to the Java virtual machine, and finally to Luncher;
Some are good for reading from the top down, such as the Activity start, which I read from the startActivity method, then to ActivityThread, then to ActivityManagerService;
Some are suitable for both ends to read in the middle, such as Binder. I started from the Java layer to the C++ layer, but couldn’t move when I saw the driver. Then I looked back from the place where the Binder was received, and finally focused on the driver at both ends to see the Binder.
A good way to look at this is to start from where the event was triggered.
2.3 What tools do YOU use to read
Source Insight, of course
Benefits of Source Insight:
- Support method jump, class jump, and C++ support is very good
- Support file search, Java,c++, XML support, and support content search
- Supports one-click import and configure paths at any time
- And most importantly, the number of imported files when not stuck
Now let me talk about how to use Source Insight
2.3.1 Downloading and installing Source Insight
Download address, http://download.csdn.net/download/foxlee1991/9882553, I also specially configured with a like Android Studio Darcula theme, Download address http://download.csdn.net/download/foxlee1991/9882535
2.3.2 Importing AOSP source code
I haven’t downloaded the full AOSP source code yet, just a few important ones. Open Source Insight, select Project -> New Project, give it a name like AOSP, and click OK
Select the source directory you want to view and click OK
Select which directories you want to import the source code into and click Add Tree
After successful import, there will be many files listed below. Click Close
2.3.3 Viewing the source code
Now enter the project or a blank, need to open the toolbar, and then you can see the source code
On the left is method and member variable search, on the right is Project File search class name, and Project Symbol search content
There are also some shortcut keys, such as Ctrl+ left for method jump, Ctrl+G for forward and backward in the upper left corner, Ctrl+G for forward and backward in the upper left corner, Ctrl+F for search. Sometimes, the method fails to jump, so we need to click Project and Choose Synchronize Files for global association, as shown in the figure
Do not import too many files from Synchronize, which causes a failure. You can import directories selectively
When importing source code, we sometimes import the compiled source code (as.s or. “S” and “File Type Options” in C/C++. S support, then Close, as shown
Then we Add some directories again, click Project, select Add and Remove Projec Files, select the corresponding directory Add Tree, at the same time, we can also select Remove Tree to delete the corresponding directory source, the operation is as follows
Third, other
Before really started to read the Android source code, it is best to learn some grammar knowledge of C/C + +, because the source of the core parts are written in C/C + +, if you don’t know about some basic grammar, will see more of the fog, here I recommend to you two books the C standard library Chinese version and the Chinese version 2 C + + standard library, Other learning sites are also good:
- Microsoft’s official
- Novice tutorial
- W3Cschool
I have just started to read The Android source code myself, and I am going to sort out some reading notes. I have set up a project on Github, and I will display the notes as an issue, which is convenient for everyone to communicate. In addition, I will copy some of the source code I read into the Android Studio project, keeping the original directory structure. It is convenient for you to find the corresponding source code and project address when you look at the notes
About me
- foxleezh
- My blog
- github
- Email address – [email protected]