If you feel helpful, welcome to pay attention to, appreciate, forward reading time: 2616 words 6 minutes

Writing in the front

Very early like their own build an Android source code reading environment, toss about a few times fruitless, put aside. Recently, I had some spare time, and I thought it was time to have a deeper understanding of the Android system, so I tried again, but I did not expect to succeed, and I used AndroidStudio to read, or the familiar interface, or the familiar shortcut keys, haha, you also try?

First of all, there is a study before using AndroidStudio to read the source code, should know, if you want to use AS to read Android source code almost divided into such four steps

  • Step 1: Download the source code
  • Step 2: Compile source code
  • Step 3: Import the source code with AS
  • Read the source code

Those of you who have tried it know that compiling source code can be a frustrating process. It may take you longer to compile than to download. However, after trying many times, I also built a perfect environment for AS to read the source code. Here’s how you can import AS without compiling the Android source code.

Android source code reading environment construction

1. Download the initialization source code library

AOSP (Android Open Source Project) initialization package download address: mirrors.tuna.tsinghua.edu.cn/aosp-monthl… Copy the url to the browser can be downloaded, thunderbolt seems not to use, at least my useless. Another important thing to note is that do not close your computer during the download. I downloaded 18 gigabytes and I had to start all over again because I closed my computer.

Of course source code download, in addition to directly download the initialization package, you can also install the REPo tool, and then use the REPo synchronization. Either way, you can’t break it in the middle. More information about the source code download can view the following two sites: a mirror station: open source software, tsinghua university * mirrors.tuna.tsinghua.edu.cn/help/AOSP/ * Google’s official website: source.android.com/source/down…

2. Download the REPO tool

It will take you hours to download the latest initialization package, and the Internet speed is not good, even more than 10 hours. In this process, I can install the necessary repo tools to synchronize and check out the code, and execute the following code from the command line (if I have a MAC, Windows, you can Google the corresponding tutorial, there should be many).

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
Copy the code

3. Synchronize and check out the source code project

When the download is complete, you will get a 37GB tar file named aosp-latest. Unzip it and you will find a hidden.repo file inside.

Then use the command line to go to the unzipped root directory and run repo sync to synchronize to the latest branch and check out its master branch.

Of course, you can also use repo sync-l to check out the source code items in the REPo without synchronizing. The effect after detection looks like this:

4, the key step – put in the compile file

In fact, the reason why we need to compile is the same AS our Android project, and the open source Android project we pulled down from Github also does not have configuration files such AS IML, so we need to compile by ourselves to associate the files in this project with AS.

So the reason we don’t compile here is not that we don’t really compile. Instead, a friend whose ID is Difcareer has helped us compile the AS configuration files required by each version. We just need to download it and put it in the root directory of the project. As configuration file git address: github.com/difcareer/A…

Note: If you forget the checked version of your source code, you can view it as follows: Open the version_defaults.mk file according to the directory ‘/build/core/version_defaults.mk’ and find the value of the PLATFORM_SDK_VERSION keyword to find the corresponding version in the table below.

5. Import the source project into AS

Now that you’ve done your homework, you can import the AOSP directory just like you would an Android project. Go to the AOSP directory and select the recognized. Ipr file, then click Open.

Here are two things to note:

  • The first time you import the configuration File, you may not see the other directories. You need to go to File->Invalidate Cashes/Restart… Let AS restart to compile the project.

    This compiled state lasts about 10 minutes or so. Then switch the right file directory to Project mode:

  • If a cyclic task Scanning file to index is displayed, run…. Don’t move

    Solutions are as follows (Open Module Setting --> Modules --> Find the Gen folder --> Resources)

6. Ok, import is successful

Then we can freely rise in the Android source ocean, let’s have a look!

Finally, thank you to Mingo of Code GG, the big brother of Android phone development for 8 years. In the source code reading environment and source code reading methods and skills have given me a lot of help. Here I post mingo’s answer on the planet to share:

Recommended reading

series

  • “Do.006” Android Field Project (1) — I want to say “Let’s Do it”
  • “Do.007” Android Project (2) — Use Github for version management
  • “Do.008” Android Actual Combat Project (3) — Git branch management model
  • “Do.009” Android project (4)
  • “Do.014” Android Field Project (5) — Gradle configuration release is separated from debug environment

other

  • “Do.011” Do you actively choose or passively accept
  • “Do.002” Youdao Cloud Note – good but not safe
  • How hard is “DO.013” to be original in China?