Since most of the development in recently happening on latest Android versions like Android 9, 10 and recent. We have updated this post to use only on Ubuntu 18.04 and Latest versions of Ubuntu.
Install Repo
Repo is a tool written by Android on top of Git to work with multiple git repositories. You can install repo as,
$ sudo snap install git-repo
Alternatively, you can install latest repo as,
$ export REPO=$(mktemp /tmp/repo.XXXXXXXXX)
$ curl -o ${REPO} https://storage.googleapis.com/git-repo-downloads/repo
$ gpg --recv-key 8BB9AD793E8E6153AF0F9A4416530D5E920F5C65
$ curl -s https://storage.googleapis.com/git-repo-downloads/repo.asc | gpg --verify - ${REPO} && install -m 755 ${REPO} ~/bin/repo
And then add “~/bin” to your path as,
$ export PATH=$PATH:~/bin
Install Prerequisite Packages
$ sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
Set Developer Credentials to be used by Git
$ git config --global user.name $USER_NAME
$ git config --global user.email $USER_EMAIL
Decide on AOSP Branch to download
Visit https://android.googlesource.com/platform/manifest and decide on which branch of AOSP you want to download and look for the respective branch in it. The latest Android version is Android 12, so we can download the stable version of it with branch name “android12-release”
$ mkdir aosp_workspace
$ cd aosp_workspace
$ repo init -u https://android.googlesource.com/platform/manifest -b android12-release
Start Downloading
$ repo sync
Above command will take few hours almost 100 GB of internet bandwidth, so just wait for everything to finish and you will have the AOSP source code in your local machine.