Sarthak Roy
Sarthak Roy's Dev Blog

Sarthak Roy's Dev Blog

Getting Started with AOSP:
Setup build environment, Sync the source and Build

Getting Started with AOSP: Setup build environment, Sync the source and Build

Sarthak Roy's photo
Sarthak Roy
Aug 14, 2022

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Android (Android Open Source Project) is an open source mobile operating system which runs the Linux kernel underneath. It is primarily designed for touchscreen mobile devices such as smartphones and tablets. As its a open source operating system, anyone can build it for any device.

Today we will start with Setting up our Build Environment, sync the AOSP source and then build AOSP.

Setup the Environment

Make sure you are running a Linux Operating System (Ubuntu is preferred). I will be using Ubuntu as an example.

First we will Install the following packages in our Ubuntu-based system, which will be needed to build AOSP.

sudo apt update
sudo apt install -y bc bison build-essential ccache curl flex g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev liblz4-tool libncurses5 libncurses5-dev libsdl1.2-dev libssl-dev libwxgtk3.0-gtk3-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev python ssh python2 patchelf binutils

Lets setup our git credentials in our build environment.

git config --global user.email "username@client.com"
git config --global user.name "Your Name"

Lets install repo now. Read more about repo over here

mkdir -p ~/.bin
PATH="${HOME}/.bin:${PATH}"
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo
chmod a+rx ~/.bin/repo

Sync the source

As we are done with setting up our build environment for building AOSP, Lets sync the AOSP sources.

First lets make a new folder named aosp using mkdir and navigate into it using cd

mkdir aosp
cd aosp

Now we have to initiate the AOSP manifest into the current folder.

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

here,

BRANCH -> The AOSP branch you want to sync. It is mostly the Android version and its tag. Suppose you want to sync Android 12.1 version r2 tag. You have to put android-12.1.0_r2. You can check all the available tags here.

Alternatively, you can add --depth=1 option before -u in repo init if you dont want commit history of the source code.

After you have initiated the AOSP manifest, the next step is to sync the source which can be done by a repo command.

repo sync

Alternatively, you can add options with repo sync command. Few commonly used options are:

--force-sync -> This option force syncs the source over the current one and nuke any changes made in the source.

-jx -> It mentions the number of threads to be used in syncing the source. x signifies the number of threads. Example: -j4 means the sync will use 4 threads.

Building AOSP

If you have followed the following steps, I am sure you have successfully synced AOSP source code. Now you can build AOSP for your Google Pixel devices or any other device (needs external device trees to be cloned).

After you are done with your device trees, Just follow the steps to start building AOSP.

Initialize the AOSP environment with the envsetup.sh script.

. build/envsetup.sh

or

source build/envsetup.sh

Then we have tolunch the device.

lunch aosp_device-buildtype

here,

device -> the device codename

buildtype -> the build type: user for production builds, userdebug for debuggable builds which are like user type, eng for its solely for debugging and not recommended for production.

Example:

Suppose I want to build for Google Pixel 6a userdebug build, so my lunch command will be:

lunch aosp_bluejay-userdebug

Here,

bluejay -> The device codename of Pixel 6a

userdebug -> Build type

Then, we can finally start compilation of AOSP for our device by running the command:

make otapackage

The will make a flashable AOSP zip for your device. After the compilation is done, you will find your zip at out/target/product/devicecodename directory.

Thanks for the read, Stay Tuned for some more interesting posts.

Share this