Introduction to the

As we all know, Android is Open Source, AOSP (Android Open Source Project) is the abbreviation of Android Open Source Project. As an Android developer, mastering the working mechanism of Android system is the only way to grow in technology. The first step is to compile Android system by yourself.

The preparatory work

  • A device that can unlock the BL lock (BootLoader) and the manufacturer provides the hardware driver. It is recommended to use Google son mobile phone (Nexus and Pixel series), which can unlock the BL lock. Google will officially provide the hardware driver, and the AOSP will provide the configuration of the corresponding model
  • A hard drive with at least 300GB of free space (Android11 source -150GB, compile product -150GB)
  • Linux or MacOS (Windows can use WSL)
  • Need at least 8GB of memory, too little memory will cause build.ninja file to fail to be generated (16GB is officially required, 8GB is ok)

Here is the official Google’s recommendations: source.android.com/setup/build…

Environment set up

Reference: source.android.com/source/init…

The main is to download a variety of compilation tools, such as JDK, GCC, g++, etc., as well as a variety of dynamic libraries and auxiliary tools

Note: Some of the environment installation in this document is wrong, some necessary library installation is missing, and errors may be reported during compilation. Please refer to the environment installation below. If there is still dependency loss during compilation, you can continue to compile after installation

Install the JDK

Take Ubuntu as an example:

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

Note: AOSP compilation now requires JDK version >=9

Install additional packages

sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip libncurses5
Copy the code

Note: libnCurses5 is missing from the official documentation, which will cause the libnCurses.so. 5 library to be found during compilation

Download the source code

Android source code is composed of a lot of Git repositories, in order to unify the management of so many Git repositories, Google out a tool, called Repo

Reference: source.android.com/source/down…

Because of Google access problems in China, it is recommended to use the image to download the source code, the following provides several image addresses:

  • Tsinghua university,

Mirrors.tuna.tsinghua.edu.cn/git/AOSP/pl…

  • USTC

git://mirrors.ustc.edu.cn/aosp/platform/manifest

Can be specified when branch of repo init: source.android.com/setup/start… Here you can find the devices supported by the corresponding system branch. For example, my device is Pixel2. You can see from this table that the code under the Android-11.0.0_r25 branch supports my device, so you can execute the following command:

Repo init -u https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/manifest - b android - 11.0.0 _r25Copy the code

Then start synchronizing:

Repo sync-j8 #j8 stands for using eight threadsCopy the code

AOSP code download is a long process that requires patience

Download the driver

In developers.google.com/android/dri… Here you can find drivers for Nexus and Pixel. Note that each driver has a code number corresponding to the build number of the AOSP source code you downloaded

Unzip them and you get two shell files

Copy them to the root directory where you downloaded the AOSP source code

Note: There are many tutorials on the web that say to use bash instead of ZSH on the terminal. I have tested ZSH without any problems. If there are problems during compilation, you can try switching shells

  1. Start by switching the shell to the aOSP source root
  2. Execute two extracted driver shells and remember to agree the License

  1. Execute source build/envsetup.sh, which writes some environment variables to the shell
  2. So let me make clean
  3. Use the lunch command to select the build target

Here is the command rules: source.android.com/setup/build…

lunch aosp_walleye-userdebug
Copy the code

The following parameters can be found here:Source.android.com/setup/build…

You can also add no parameters after lunch, which will prompt you to select a target

A message will pop up when the assignment is complete

Begin to compile

Build part of the document here: source.android.com/setup/build… If it is the first time to compile, we will use the m command directly

M-j8 # Enable 8-thread compilationCopy the code

Matters needing attention:

  • Now I’m going to use itmakeCommands will promptCalling make directly is no longer supportedThen exit the compilation, so usemReplace themake
  • You cannot compile using the root account

flash

  1. First, unlock the PHONE’s BL lock (each model is different, there will be a corresponding tutorial online) and enter the FastBoot mode \
  2. You can use make Fastboot in the aOSP directory or download it directly from the Internet: Developer.android.com/studio/rele…
  3. Go to the directory of the image generated after compilation…. / aosp/out/target/product/walleye (this is your model code, each machine is not the same)
  4. Execute the command
fastboot flashall -w
Copy the code
  1. When you reboot, you can see that the Android system we compiled is running on the phone
Fastboot rebootCopy the code

Q&A

The SDK cannot be found on MacOS

Go here github.com/phracker/Ma… Download the corresponding version of the SDK, and then put it in/Applications/Xcode. The app/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs directory, then recompile

In addition, you can also view it in Finder

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs

Which version of SDK exists in this directory? After confirming, modify…. / aosp/build/soong/cc/config/x86_darwin_host. Go file, in this array darwinSupportedSdkVersions plus you are using the version of the SDK

Save and recompile, this method may not support the current compilation script you use SDK, may compile error, so the first method is recommended

too many open files

In Linux, the number of open files is limited. You can run the following command to set the maximum number of open files

# ulimit-a Displays the current limit
ulimit -n 2048
Copy the code