preface

AOSP contains the entire source code of the Android system. To understand the Android system, you must be familiar with AOSP. For example, Android system startup process, Binder mechanism, Handler mechanism, and so on, and familiar with these knowledge need to be deep into AOSP, today from the COMPILATION of AOSP, step by step in-depth understanding.

Environmental requirements

parameter value
system Ubuntu16.04
The hard disk 250G(the more the better)
memory 16G(the bigger the better)
Number of CPU cores Eight nuclear
The compiled Version of Android 6.0
Java version openjdk7

Install the Jdk

If you are using OpenJDK7, since Ubuntu 16.04 does not have OpenJDK7 source, you need to add the source first and then install OpenJDK7 by following the command:

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-7-jdk

Copy the code

Note that you can only use openjdk7 if you are compiling Android6.0. For details, see here

Install dependencies

sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev
sudo apt-get install git-core gnupg flex bison gperf build-essential  
sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib
sudo apt-get install libc6-dev-i386
sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev
sudo apt-get install lib32z-dev ccache
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
Copy the code

Download the source code

The Source code of Android is managed by using two familiar code management tools, Git and Repo. Git is a tool developed in Python to integrate Git repositories. When managing the Source code of Android, Using a Repo tends to simplify our code management efforts.

Download the repo tool

  • Google Source (requires scientific Internet access)
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
Copy the code

Students who cannot climb over the wall can use the mirror image of Tsinghua University

  • Tsinghua source
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o ~/bin/repo
chmod +x ~/bin/repo

Copy the code

Sometimes the REPo also needs to be updated, and the source address needs to be configured into the environment variable in order to use the Tsinghua source update.

export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

Copy the code

The source ~/.bashrc environment variable takes effect by configuring the above contents to the ~/.bashrc file.

Initializing the warehouse

mkdir ~/aosp
cd aosp
Copy the code
Repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest - b android - the 6.0.1 _r79Copy the code

You can refer to this list if you want to initialize a particular Version of Android, but be aware of the JDK version, which may not be ready soon.

Synchronization code

repo sync
Copy the code

At this point, the code starts to download, which usually takes a few hours. If something goes wrong, repeat the above command.

Compile the source code

Set up the environment

source build/envsetup.sh
Copy the code

The envsetup.sh script imports some useful commands, and you can see all the command tools available in envsetup with the following command

hmm
Copy the code
- lunch: lunch <product_name>-<build_variant> - tapas: tapas [<App1> <App2> ...]  [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user] - croot: Changes directory to the top of the tree. - m: Makes from the top of the tree. - mm: Builds all of the modulesin the current directory, but not their dependencies.
- mmm:     Builds all of the modules in the supplied directories, but not their dependencies.
           To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma:     Builds all of the modules in the current directory, and their dependencies.
- mmma:    Builds all of the modules in the supplied directories, and their dependencies.
- cgrep:   Greps on all local C/C++ files.
- ggrep:   Greps on all local Gradle files.
- jgrep:   Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- mangrep: Greps on all local AndroidManifest.xml files.
- sepgrep: Greps on all local sepolicy files.
- sgrep:   Greps on all local source files.
- godir:   Go to the directory containing a file.

Copy the code

Here’s a brief introduction to these commands:

  • lunch

    Select the type to compile, execute the lunch command directly, and the types to compile are listed. Compile types fall into the following types

Compile type usage
user Limited permission, no debug, no root; Suitable for production environment
userdebug Similar to “user”, but with root permissions and debugging capabilities; Is the preferred compilation type for debugging
eng Development configuration with additional debugging tools
  • m

    Compile all modules with make

  • mm

    To compile individual modules, you need to switch to the directory where the module resides

  • mmm

    Compile individual modules without switching to the directory where the module resides

  • croot

    Go back to the root directory of the source code

Begin to compile

make -j 8
Copy the code

The -j parameter can set the number of concurrent tasks. The value is generally related to the number of cores in the system

After more than two hours of compilation, you can see the compilation success prompt.

View the production image file:

cfp@cfp:~/aosp/out/target/product/generic$ ls -l *.img
-rw-r--r-- 1 cfp cfp   69206016 Oct  1 16:50 cache.img
-rw-rw-r-- 1 cfp cfp     899484 Sep 22 20:40 ramdisk.img
-rw-r--r-- 1 cfp cfp 1610612736 Oct  1 16:48 system.img
-rw-r--r-- 1 cfp cfp  576716800 Sep 22 20:40 userdata.img
-rw------- 1 cfp cfp  576716800 Oct  1 16:52 userdata-qemu.img

Copy the code

Start the emulator and run our compiled system image.


emulator
Copy the code

So you can see what we’ve compiled.

If you want to start the Emulator but do not want to display the interface, use the following command:

emulator -no-window -noaudio > /dev/null 2>&1 &
Copy the code

Separately compiled modules

Sometimes when we modify the source code, we may only change a certain module. Do we need to compile the whole system? How can Google do such a stupid thing?

For example, if we want to recompile the Launcher2 application, simply execute the following command

mmm /packages/apps/Launcher2/
Copy the code

After that, repackage the system.img file

make snod
Copy the code

This command is used to quickly build an image file. However, it is not used in all cases. It does not check for dependencies.

The difference between make clean and clobber

Both of these commands clean up compiled production files, but Clobber is more stringent. How strict?

make clean
Copy the code

Is equivalent to

rm -rf $OUT
cfp@cfp:~$ echo $OUT
/home/cfp/aosp/out/target/product/generic

Copy the code

while

make clobber
Copy the code

Command equivalent

rm -rf out/
Copy the code

Error resolution

  • Error 1

    build/core/host_shared_library_internal.mk:51: recipe for target 'out/host/linux-x86/obj/lib/libart.so' failed
    Copy the code

    Solution:

    In art/build/Android.common_build.mk, find WITHOUT_HOST_CLANG and turn clang off# Host.
    ART_HOST_CLANG := false
    ifneq ($(WITHOUT_HOST_CLANG),true)
      # By default, host builds use clang for better warnings.
      ART_HOST_CLANG := false
    
    Copy the code