Recently because of the need to work, the Android 5.1 source download and compilation, I hope this article can give the same Android development compatriots a little help.

Ubuntu version

Ubuntu 14.04 is recommended. We started with Ubuntu 16.04, but there were bugs in compiling the source code due to many system library problems, so we switched back to Ubuntu 14.04.

Update the Ubuntu source

For many known reasons, if the native Ubuntu source is used, the speed of downloading software with apt-get will be slow, so in general, some domestic sources will be used instead. I used the Ubuntu source of Tsinghua University, and my personal test speed is very fast. Modify the Ubuntu source file with the following command.

$ sudo vim /etc/apt/source.listCopy the code

Replace the source for Tsinghua University with source.list and run the following command

$ sudo apt-get updateCopy the code
Download the repo

Repo is a code versioning tool that consists of a series of Python scripts that encapsulate a series of Git commands for unified management of multiple Git repositories. Because Android source code references a lot of open source projects, each sub-project is a Git repository, each Git repository has a lot of branch versions, in order to facilitate the unified management of the git repository of each sub-project, need an upper tool batch processing, so repO was born.

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

With these three commands, you downloaded repo into the ~/bin folder. (The second command may not download due to GFW problems when executing, you can use Proxychains to achieve wall surfer, see the updated part of this article for details)

Create a working directory
$ mkdir android-source
$ cd android-sourceCopy the code

Initialize the warehouse

$ repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifestCopy the code

If you are prompted that you cannot connect to gerrit.googlesource.com, you can edit ~/bin/repo and replace the REPO_URL line with the following:

REPO_URL = 'https://gerrit-google.tuna.tsinghua.edu.cn/git-repo'Copy the code

If you need a specific Android version (list) :

$repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest - b android - 4.0.1 _r1Copy the code

Synchronize the source tree (just execute this command later to synchronize):

$ repo syncCopy the code

P.S. You can also download the script, which will automatically execute the repo sync command if the download is interrupted.

Environment set up

Different from developing Android app, openJDK is required to compile Android source code. Openjdk7 is recommended

$ sudo apt-get install openjdk-7-jdkCopy the code

Enter the password after the system to install, and then configure the JDK environment variables to execute

$ sudo vim /etc/profileCopy the code

Add at the end of the file

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
export PATH=$PATH:$HOME/bin:$JAVA_HOME/binCopy the code

After saving, finally run the following command for it to take effect

$ source /etc/profileCopy the code

Once you’ve installed openJDK, you’ll need to download some of the packages you rely on for compilation

$ 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 unzipCopy the code
Setting up ccache (Optional)

You can choose whether or not to use the ccache compiler, which caches C and C++ to speed up the compilation. If you often use make clean or generate ROMs of different architectures, it is recommended to enable ccache. Enter the following command in the source root directory

$ export USE_CCACHE=1
$ export CCACHE_DIR=//.ccache
$ prebuilts/misc/linux-x86/ccache/ccache -M 50GCopy the code
compile

After setting up the environment, you can start compiling, but before compiling, you need to set up some Shell functions by executing the following commands in the Android source code root directory

$ source build/envsetup.shCopy the code

One last thing you need to do before compiling the Android source is to set the target for the build, which devices the Android source will be compiled for. You can use the following command to view all the targets currently supported by Android

$ lunchCopy the code

In the list of output, directly input the previous serial number can select the corresponding target. Finally, you can compile the entire Android source code in the root directory directly execute make command, if the reader’s machine is multi-core CPU, you can specify the number of CPU cores used during compilation. For example, executing the following code takes advantage of four CPU cores.

$ make -j4Copy the code

After compiling the Android source, an Out directory is generated, and all the compiled object files are in the corresponding subdirectories of that directory. Img, system.img and userdata.img. Google has published the latest way to compile the Android source code on its official website. Readers can click here, but need to go to the Internet. For scientific online methods click here. Please indicate the source of the article when reprinting. thank you