Android source download compilation

Posted by bobocheez on Sun, 05 May 2019 10:08:04 +0200

Four processes for Android source code compilation:

1. Source code download, 2. Build compiler environment, 3. Compile source code, 4. Run
My environment here is Ubuntu 16.04 (64 bits), android version 8.1.0, space at least 150G (note)

Source download

As we all know, because Google is walled, we use domestic mirrors here to download, we choose here. Tsinghua Mirror Source

Download Repo tools, AOSP uses Repo tools to manage source code, is a complementary Google code base management tool to Git.

mkdir ~/bin
PATH=~/bin:$PATH
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo
chmod a+x ~/bin/repo

Create a source storage folder, where all downloads and compilations will be placed.

mkdir aosp
cd aosp

Configure git

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

To run repo init to initialize the repository, you must specify a Web address that specifies where the code libraries contained in the Android source code will be located in the working directory.

repo init -u https://android.googlesource.com/platform/manifest

To detect branches other than master, use - b to specify the corresponding branch. To see Branch list

repo init -u https://android.googlesource.com/platform/manifest -b android-8.1.0_r52

After initializing the warehouse, you can formally synchronize the code to the local place.

repo sync

It may interrupt several times in the middle, so use this command to continue synchronization. Six or seven hours should be synchronized. Make sure that the code is synchronized at night and there is no error. Otherwise, the compilation will have some puzzling problems.

Build a compilation environment

Install the dependency libraries required for compilation

sudo apt update

sudo apt 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 openjdk-8-jdk

Initialize the compilation environment

source build/envsetup.sh

Select compilation target

lunch 

Enter the corresponding number. Here we enter 2.

All compilation targets are in the form of BUILD-BUILDTYPE, where BUILD is the code for a specific combination of functions. BUILDTYPE is one of the following types:

Compilation type Explain
user Limited authority; applicable to production environment
userdebug Similar to "user" but with root privileges and debuggability; preferred compilation type for debugging
eng Development configuration with additional debugging tools

Because we compiled version 8.1 here, the default is jack, and Jack is the default Android compilation tool chain for Android 6.0 - 8.1. You also need to set up JACK_SERVER_VM_ARGUMENTS, or you may report Out of memory error.

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"

Start compiling source code

Using make instruction to compile code, parallel tasks can be processed by using - jN parameter. The number of tasks N is usually between 1 and 2 times of the number of hardware threads on the computer used for compilation.

make -j8 

Running Android Simulator

emulator

If an error is reported, configure the environment

export PATH=$PATH:/aosp/out/host/linux-x86/bin
export ANDROID_PRODUCT_OUT=/aosp/out/target/product/generic
emulator

Switching Android Version

rm -rf .repo/manifest*
repo init repo init -u https://android.googlesource.com/platform/manifest -b android-9.0.0_r22
repo sync

Topics: Android git Google curl