Download and read Android source code

1. How to download AOSP

The source code download is the beginning of our analysis of the source code. The Android source code can be downloaded in full or individually.

1. Full download

The official document https://source.android.com/source/downloading, just follow the above step by step, but because of the need to bypass the wall, it cannot be directly accessed in China, and the source code of the entire Android project is huge, even after the wall is downloaded, it is very slow, so it is better to use the domestic mirror.

I recommend Tsinghua University’s open source mirror, the address is https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/, there is also a complete tutorial on it, I will not copy and paste, but one thing to note is that you must prepare a relatively large disk, at least 60 G, not counting post-compilation.

In fact, we don’t need all the source codes when we analyze the source codes, because AOSP includes not only the system source codes, but also some tool codes, such as aapt, adb, etc. We don’t need these at all, and even if it’s the system source codes, we don’t need to read all of them.

2. Single download

The official address is https://android.googlesource.com/. For example, if we want to download the code under the platform/frameworks/base/ directory, we can git clone https://android.googlesource.com/platform/frameworks/base, but this will still encounter the problem of overcoming the wall. Of course, we can also use mirroring.

The mirror address is https://aosp.tuna.tsinghua.edu.cn/. For example, if we want to download the platform/frameworks/base/ directory, we can use git clone https://aosp.tuna.tsinghua.edu.cn/platform/frameworks/base. If you have enough bandwidth, you can download the single source code you want in a few minutes.

If you want to download a single file, or search for file names and codes, you can visit http://androidxref.com/, here are some Android source codes

Directory Structure

First, the previous picture, the architecture diagram of the entire Android project
insert image description here

We all know that the Android system is roughly divided into these four layers from top to bottom, so we will use these four layers as the basis to explain the directory structure of AOSP:

  • The first layer: the application layer (applications) corresponds to platform/packages/apps in the root directory
  • The second layer: the application framework layer (application framework) corresponds to platform/frameworks in the root directory
  • The third layer: the runtime layer includes the runtime (libraries) and the android runtime environment (android runtime)
  • There are many directories corresponding to libraries, among which the libc library corresponds to platform/bionic
  • Android runtime environment, Core Libraries corresponds to platform/libcore in the root directory , Dalvik Virtual Machine corresponds to platform/dalvik in the root directory , but now it is ART, 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 must be compatible with various chips, the following list:
  • The goldfish project contains the kernel sources for the emulated platform.
  • The msm project contains source code for the ADP1, ADP2, Nexus One, Nexus 4, Nexus 5, Nexus 6, Nexus 5X, Nexus 6P, Nexus 7 (2013), Pixel, and Pixel XL, and can be used as a starting point for using the Qualcomm MSM chipset.
  • The omap project is used for PandaBoard and Galaxy Nexus and can be used as a starting point for using TI OMAP chipsets.
  • 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 used on Xoom, Nexus 7 (2012), Nexus 9 and can be used as a starting point for using the NVIDIA Tegra chipset.
  • The exynos project contains the kernel source code for the Nexus 10 and can be used as a starting point for using the Samsung Exynos chipset.
  • The x86_64 project contains kernel sources for the Nexus Player and can be used as a starting point for using the Intel x86_64 chipset.
  • The hikey-linaro project is for the HiKey reference board and can be used as a starting point for using the HiSilicon 620 chipset.
  • There is also a hardware abstraction layer (HAL) in the middle of the third and fourth layers, corresponding to platform/hardware in the root directory

The current directory I downloaded 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

2. How to read AOSP

When we download the source code, we will feel at a loss because there are too many AOSP source codes. Here we need to clarify some questions:

  • What source code to read
  • The order and method of reading source code
  • what tool to use to read

In the following, I will expand on these three questions one by one.

1. Which source code to read

This question is more personalized, because different people are engaged in different jobs. Some people are engaged in application development and may be interested in the Java layer; some people are engaged in 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 varies from person to person, but there is one thing, you can’t look at the source code blindly and aimlessly, because if you do this, you will eventually be submerged in the sea of ​​AOSP. After watching everything for a year and a half, you still feel that you haven’t seen it through. When people ask you about the source code, they can tell you a thing or two, but when you go deeper, you don’t know why.

So for AOSP source code, it’s not about the many, but about the essence. Don’t try to understand all the source codes. You just need to study the part you are interested in, because even Google engineers can’t read all of AOSP.

For me, I am engaged in application layer development, and I will mainly understand the source code of the following aspects:

  • Android system startup process, application startup process, four major components startup process, which will be included in the system startup chapter
  • Commonly used services in the system are ActivityManagerService, WindowManagerService, etc., which will be included in the system service chapter
  • Communication mechanism, mainly Binder and Handler, which will be included in the communication chapter
  • The creation, operation, and destruction of processes and threads will be included in the process chapter
  • View's drawing and display process, event distribution mechanism, which will be included in the graphics drawing chapter
  • Android virtual machine ART operating mechanism, class loading mechanism, Java annotations, Java reflection, which will be included in the virtual machine chapter
  • Android's optimization algorithm for Java collections, which will be included in the Java Basics

2. The order and method of reading the source code

2.1 Reading order

Reading source code is a process that accumulates over time, and it cannot be accomplished overnight. After we list the source code we are interested in, we need to make a reading plan and read what to read first. This also varies from person to person. It depends on your own interests. If you want to read the most, then you will be at the top.

I have been talking about interest, because interest is the best teacher. Only when you are interested in something, will you have the motivation to learn and research, and you will not feel tired. If you gnaw on something that you are not interested in at the beginning, it will be boring and unfocused in the end. The degree of understanding is not deep, and you may lose confidence and give up reading in the end.

Of course, if you're interested in several things, there are some principles:

  • Things are all about priority, just like trees take root in the earth, the earth comes first, then there are trees, the basic things first look at
  • Watch things that are related to each other together, don’t watch the system start for a while, and then suddenly watch the event distribution or something

2.2 Reading method

The Android system covers a wide range, from upper-level applications, to Framework, to Libraries and even hardware, from Java layer to C++, just like a building with dozens of floors, each floor has stairs and elevators, all 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 have different reading methods, and some are suitable for reading from bottom to top, such as the system startup process. I start reading from the beginning of the event, starting from init.cpp, then to the zygote process, to the Java virtual machine, and finally to Luncher;

Some are suitable for reading from top to bottom, such as the start of Activity, I start reading from the startActivity method, then to ActivityThread, and then to ActivityManagerService;

Some are suitable for reading from the middle at both ends, such as Binder. I see the C++ layer from the Java layer, but I can’t see the driver.

There is still a good way here, that is, it is more appropriate to start from the place where the event is triggered.

3. What tools do you use to read

The Android source code reading artifact is of course Source Insight
insert image description here

Benefits of Source Insight:

  • Support method jump, class jump, and support C++ very well
  • Support file search, java, c++, xml all support, and support content search
  • Support one-click import, configure the path at any time
  • And most importantly, it doesn’t get stuck when there are many imported files

Let me talk about how to use Source Insight

3.1 Download and install Source Insight

Baidu by itself, and Source Insight can also be configured with the same Darcula theme as Android Studio, which needs to be downloaded separately.

3.2 Import AOSP source code

I haven't downloaded the complete AOSP source code yet, but I just downloaded a few important source codes first. Open Source Insight, select Project -> New Project, take a name such as AOSP, and click OK

Select the source code directory you want to view, click OK

Select which directories need to import the source code, click Add Tree

After the import is successful, there will be many files listed below, click Close

3.3 View source code

Now entering the project is still blank, you need to open the toolbar, and then you can view the source code

On the left is the method and member variable search, on the right Project File is the search class name, and Project Symbol is the content search

There are also some shortcut keys, such as Ctrl + left key to jump, Alt +, back, Alt +. is forward, Ctrl + G is to jump to the specified line, Ctrl + F search content, sometimes we encounter methods that cannot jump, then we need to click Project, select Synchronize Files, global association, as shown in the figure

Here we should pay attention not to import too many files, too many will cause Synchronize to fail, we can selectively import some directories

When we import the source code, sometimes some compiled source code (ending with .s or .S) cannot be imported. At this time, we need to click Options, select File Type Options, add .s and .S support in C/C++, and then Close, as shown in the figure

Then we re-add some directories, click Project, select Add and Remove Project Files, and select the corresponding directory Add Tree. At the same time, we can also select Remove Tree to delete the source code of the corresponding directory. The operation is as follows

3. Others

Before actually starting to read the Android source code, it is best to understand some C/C++ grammatical knowledge, because the core part of the source code is written in C/C++. If you don’t know much about some basic grammar, you will be confused. Here I recommend two books "C Standard Library Chinese Version" and "C++ Standard Library Chinese Version 2", and some other learning websites are also good :

Guess you like

Origin blog.csdn.net/shanshui911587154/article/details/129669609