To become a qualified Android programmer or senior Android engineer, it is necessary to know how the Android framework layer works. To know how it works, you need to read the Android source code. The best way to read the Android source code is to download and compile it yourself. Why read Android source code? First, interview bonus. Second, you can write code that knows how it works. Third, the depth of learning. As the saying goes, know what you are and know what you are. The following record is based on 7.1.1 system source code downloaded to the brush process.

The basic concept

  • AOSP: The Android Open Source Project, the Google Open Source Project. The source code downloaded through Google is AOSP code, which does not include the Family bucket application developed by Google. Ps: This as an Android programmer must know a professional word, otherwise you appear very Low, very unprofessional.
  • Repo: Repo is a tool that makes it easier to use Git in an Android environment; in other words, Repo is used to manage each repository managed by Git. For more details, see here.

To prepare

  • Compilation system: UbuntuKylin 16.04 LTS.
  • Phone: Nexus 6.
  • Driver: Nexus 6 driver.
  • Android: Android 7.1.1.

Note: Is there any system source code and driver supporting this phone from Android official website?

Setting up the Compilation Environment

1. Install the JDK

If Ubuntu >= 15.04, you can run the following command to install OpenJDK, which is used here. I was going to compile with Ubuntu LTS 14.04, but it was a bit troublesome to install OpenJDK, so I gave up using Ubuntu LTS 14.04.

sudo apt-get update
sudo apt-get install openjdk-8-jdk
Copy the code

If you are not compiling the source code for Android 7.1.1, you can refer to the corresponding JDK version of Android in the figure to install it.




Android VERSION Corresponding to the JDK

2. Major software packages

  • Python: Python 2.6-2.7.
  • Git: Git 1.7 or later.

Download the source code

Here I download Google official AOSP, download the official need for scientific Internet access and ensure your network stability. For the specific AOSP of Tsinghua University open Source software mirror station, please refer to the Settings here.

  • Install the Repo

1. Make sure there is a bin/ directory in the home directory and that the directory is included in the path:

$ mkdir ~/bin
$ PATH=~/bin:$PATH
Copy the code

2. Download the Repo tool and make sure it is executable:

$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
Copy the code
  • Initialize the Repo client

After installing Repo, set up your client to access the Android source code base: 1. Create an empty directory to hold your working files.

$mkdir AndroidSource // AndroidSource this is my own name to store source code directory name $CD AndroidSourceCopy the code
  1. Configure Git with your real name and email address. Skip this step if you have Git configured.
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
Copy the code
  1. Run repo init to initialize. The following -b checks out the corresponding branches. For a list of branches, see source code markup and compiled versions.
Repo init -u https://android.googlesource.com/platform/manifest - b android - 7.1.1 _r57Copy the code

After successful initialization, the client directory should now contain a.repo directory where listings and other files will be stored.

Download the Android source tree

Then download the Android source tree from the code base specified in the default listing to the working directory by running the following command:

$ repo sync
Copy the code

Make sure the network is stable when downloading. Make sure the computer is not set to sleep and can be left to download at night. About 30 gigabytes after downloading. Download source code problems will not be too much, most of the problems for the network, the real problem is the compilation of the source code, it is simply a nightmare.

Ready to compile

Download proprietary binaries

I need to brush the compiled source code to Nexus6, so I need to download the corresponding driver for Nexus6. To find the driver binary file corresponding to the system version and phone model, you can find here.


Binary driver file

Then download the three packages and decompress them to get three SH files. Copy the three sh files to the Android build directory.

$ ./extract-broadcom-shamu.sh
$ ./extract-moto-shamu.sh
$ ./extract-qcom-shamu.sh
Copy the code

After running the above command, the declaration message will be displayed. Keep pressing Enter. When you see the following message, type: I ACCEPT. Don’t press too fast, it’s easy to skip input accept…




PNG.

Jack configuration

Jack is an Android toolchain for compiling Java source code into Android Dex bytecode. To use Jack, you simply compile the source tree or your project using the standard Makefile command without doing anything else. Android 8.1 is the last version to use Jack.

Following the March 14, 2017 announcement, the Jack toolchain has been deprecated. Jack is the default Android build tool chain for Android 6.0 — 8.1.

If you compile the Master branch of AOSP and Android O and later, Android provides disabled support. See here for details.

When compiling the 7.1.1 source code, I did not check how to turn off Jack compilation. Therefore, it is necessary to check whether Jack-Server has been installed and started.

  • Set the VM size of Jack-Server according to the RAM of the machine. My machine has 16G memory, so set around 8G VM. Modify the prebuilts/ SDK /tools/jack-admin file, find the variable JACK_SERVER_VM_ARGUMENTS location and add the argument -xmx8000m

  • Go to prebuilts/ SDK /tools and run the following command:

$ ./jack-admin start-server 
Copy the code
  • If you are prompted that it is not installed, run the following command and then run the command above to start Jack-server again.
$./jack-admin install-server jack-launcher. Jar Jack-server-4.8.alphaCopy the code

Ensure that the Jack-server service is started. Otherwise in the compilation time will encounter jack-server many pits…

Set up the environment

Use the envsetup.sh script to initialize the environment.

$ source build/envsetup.sh
Copy the code

Use Lunch to select the target to compile.

$ lunch
Copy the code

After running the above command, all current Build types are listed, and then Type 23. Here I choose 23.aoSP_shamu-userDEBUG. This Build Type can be debug, so I suggest you choose UserDebug. For details, see parsing here.

Compile the code

Make-j8 //-j8 is the number of parallel tasks, based on the number of kernel threads on the machine CPU.Copy the code

Problems encountered during compilation

  • M4 is not installed.
FAILED: / bin/bash - c "prebuilts/misc/Linux x86 / flex/flex - 2.5.39 -o/home/Chris/rom7.0 / out/host/Linux x86 / obj/STATIC_LIBRARIES/libaidl - common_intermediates/aidl_language_l CPP System /tools/aidl/ aidl_language_L. ll" flex-2.5.39: exec of /usr/bin/m4 failed [0% 8/49773] Yacc: aidl <= system/tools/aidl/aidl_language_y.yy FAILED: /bin/bash -c "prebuilts/misc/linux-x86/bison/bison -d - defines = / home/Chris/rom7.0 / out/host/Linux x86 / obj/STATIC_LIBRARIES libaidl - common_intermediates/aidl_language_y. H - o / home/Chris/rom7.0 / out/host/Linux x86 / obj/STATIC_LIBRARIES/libaidl - common_intermediates/aidl_language_y CPP system/tools/aidl/aidl_language_y.yy" [ 0% 8/49773] host C++: ijar <= build/tools/ijar/classfile.cc ninja: build stopped: subcommand failed. build/core/ninja.mk:151: recipe for target 'ninja_wrapper' failed make: *** [ninja_wrapper] Error 1Copy the code

Solutions:

sudo apt-get install m4
Copy the code
  • Error reported while compiling 1%, bison not installed.


Compilation error 1

Solutions:

1. sudo apt-get install bison
2. sudo apt-get install  g++-multilib gcc-multilib lib32ncurses5-dev lib32readline6-dev lib32z1-dev
Copy the code
  • During the compilation process, there was a problem reported by Jack-server, whether it was installed or not, and whether it was not started. At that time, I was in a hurry to solve the problem, so there was no corresponding screenshot. Therefore, I also wrote in the above to ensure that Jack-server is installed and started first, otherwise in the later compilation of 7.1.1 source code will report an error. During the compilation process, my machine did not report the error that Jack-server was out of memory temporarily. If you compile using a virtual machine, you should have at least 8GB of memory, or you will get an out of memory compilation error.


Jack – Server error. The PNG

PNG.

More than the above problems are encountered when compiling, some are not recorded…

When the compilation failed, I would manually delete the out directory again to avoid further problems and then re-run the > Setup environment step above.

  • After several days of efforts and persistence, finally compiled successfully, excited ~ ~


    PNG message after successful compilation

Use fastBoot to brush

First of all, make sure that the UNLOCK unlock works properly. Specific how to unlock Google. Please back up your important personal data before swiping. The swiping machine will erase all data. Then, connect to the computer. Next, execute the following command in the source code root directory (where you downloaded the source code) :

$adb reboot bootloader $fastboot -w flashallCopy the code


Start swiping. PNG

The brush is complete. PNG

End

The whole process ends here, but this is just a new beginning for reading the source code! Then there’s how to import AOSP into Android Studio for reading and debugging and how to read it. Although the process is hard, but it is worth it, work hard together ~ ~

More links to compile error: ichrisking. Making. IO / 2017/08/25 /…