Downloading And Building AOSP Android 11 Custom ROM From Scratch lionguest studios blog post

How to Download and Build AOSP Android 11 custom ROM from scratch

Android Open Source Project (AOSP) is a project maintained by Google from where we can get the source code for the Android operating system. Since it is an open-source project anyone can download the android source code, compile it onto their machine, and can easily make some modifications to the source code. Currently, the latest version of Android is Android R (also known as Android 11 or Red Velvet Cake). You can get the source code for any version of android from the AOSP like Android R, Q, P, etc.

Download and Build AOSP Android 11 custom ROM from AOSP on Linux from scratch

In this blog post, we will see how we can download the source code for Android 11, how to build or compile it and run it on the emulator or on the physical device using the Android Open Source Project.

There might be few reasons why you may need to download the android 11 source:

  1. To Flash custom ROM on to your phone: If your phone’s manufacturer is not providing the latest android released for your phone and you are eager to use and test the latest feature provided by the android platform then in that case you may need to download the android source code.
  2. Contributing to Android Open Source Project (AOSP): If you are a developer and looking to contribute code to the AOSP, then in that case you get the android source code.
  3. Creating your own version of android: In case, if you want to make your own version of Android like Cyanogenmod or something like that then also you can get the source code.

System Requirements:

To download and build the AOSP project you should have a 64-bit version of either Linux (18.04 or greater) or Mac OS installed. Here, RAM plays a very crucial role when it comes to building / compiling the android source code. 

According to the official docs, it is recommended that you should have at least 16GB of RAM. If you want to build an older version of android then 8GB may work for you. I tried android 10 (android-10.0.0_r20) with 8GB of RAM but I faced “java heap out of memory error” but it got fixed when I increased the Java heap size.

But when I was building for Android 11 (android-11.0.0_r17) with 8GB of RAM at that time also I faced the same memory-related error. So, I tried many different things but the issue still exists. Then I increased the RAM to 16GB and the build was successful.

  1. OS: A 64-bit Linux or Mac, Windows is not supported.
  2. Ram: 16 GB preferred 
  3. Free Disk Space: 250 – 300GB free space

The Open JDK comes prebuilt with AOSP, so no need to install it separately.

Installing Required packages:

Just copy the below command and paste it into your terminal. This will install all these packages which are required for working with android source code.

Installing required packed for AOSP
Installing required packed for AOSP
sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
sudo apt update
sudo apt upgrade

Downloading the Android 11 Source Code

The process of downloading the Android Open Source Project (AOSP) source code takes quite a while to complete, so make sure that you have a good internet connection so that you can finish it in less time. On average it should take you around 7-10 hrs to download the android source code. 

Let’s first create a directory with the name aosp11 inside which you can download the android 11 source code.

//Created a new directory and cd into it
mkdir aosp11 && cd aosp11
Creating new directory to download AOSP
Creating new directory to download AOSP

You are now inside the aosp11 directory. Now you have to configure the name and email address associate with your git account by running the following commands.  Here, replace words inside the quotation with your real git accounts details.

git config --global "Your Name"
git config --global "[email protected]"
Configring git credentials
Configring git credentials

Let’s now download the repo tool. From your home directory run the following commands one by one.

cd ..  //Currently, you are in aosp11 dir, this will take you back to home dir 
mkdir ~/bin
curl > ~/bin/repo
chmod a+x ~/bin/repo

To get the Android 11 source code we first have to initialize the repo tool inside our current working directory. This will create a .repo subdirectory inside our current working directory. 

Here, you can even specify the branch which you want to download using the -b tag (this is optional). If you don’t specify the branch then it will by default take the master branch. 

But in this case, we want to get the Android 11 source code so we will specify the android 11 branch using the -b tag and initialize the repo.

Now to get the tag for android 11, we have to visit the source code tags and builds page available on android official docs. This is where you choose which android version to download. In this case, we will download android 11, you can choose whatever version of android you want to by choosing the appropriate tag from the above page.

Source code tags and build official page
Source code tags and build official page
repo init -u -b android-11.0.0_r17

The android-11.0.0_r17 is the latest version of Android 11 available for download. In the future, if some other versions of android are released (android S or android 12, etc) then this is the command where you have to make changes by selecting the appropriate tag and then initialize the repo with the above command. 

Initializing the AOSP directory using repo tool
Initializing the AOSP directory using repo tool

During repo init, if you face any python related issues like /usr/bin/env: ‘python’: No such file or directory then to fix it run the following command.

sudo ln -s /usr/bin/python3 /usr/bin/python

After some time, you should see a message “Enable color display in this user account?” Select y and press enter.

Repo initialized successfully
Repo initialized successfully

You should see another message “repo has been initialized in /home/user/aosp11” which confirms that you have initialized the repo.
Now is the time to download the AOSP Android 11 source code locally to your working directory. To download run the following command. Make sure you are inside the asop11 directory and then run this command.

repo sync 

For me, it took 7hrs to download the Android 11 source code. For you, it might take even more, so sit back and relax till it completes. In the end, you should see the message which is shown in the image below.

Repo Sync finished downloading the source code
Repo Sync finished downloading the source code

This says that you have successfully downloaded the entire android 11 operating system’s source code and is now ready for compilation.

Compiling or Building the Android 11 Source Code

This is the crucial part when it comes to building the android source code as you might face some error during this process. But no worries, I will also provide solutions to these common errors.

First, you have to initialize the environment.

source build/

Then you have to choose a target device (using lunch command) on which you will be installing the operating system like mobile, watch etc. In this case we will be building android 11 for a 64-bit android mobile device.

Choosing appropriate platform to build AOSP
Choosing appropriate platform to build AOSP

Now, we will select the second option aosp_arm64-eng from the list which is a 64bit version of android 11 for a mobile device. You can even choose option1 for 32-bit devices. After selecting the 2nd option it will show you the exact details related to that build.

To start the compilation or building process run the below command.

m -j4

The ‘m’ stands for “make” which will compile the source code into an appropriate machine code. The ‘j4’ represents no. of CPU cores (here, 4 means 4 cores) which you want to allocated for building. You can even increase it to a higher number depending upon how many cores your computer has. To check for no. of the core run nproc in the terminal.

During this process, your system might get hanged or become unresponsive which is completely normal so don’t panic, leave it as it is. Depending upon your system configuration this process will take you around 10-15hrs to build the android open source project code.

Common errors during AOSP building process and their solution

Below are some of the common errors which you might face while building the android open source project and solutions to fix those errors.

  • cannot open shared object file: No such file or directory:

Solution: Run this command sudo apt-get install libncurses5 and then try to recompile the source code.

  • Exception in thread “main” java.lang.OutOfMemoryError: Java heap space:
ninja failed due to out of memory error
Ninja failed due to out of memory error

Solution: Run this command export _JAVA_OPTIONS=-Xmx8g this will increase your heap size to 8GB. and recompile the source code. If you still face the error then run the make clean command and then again try recompiling the source code. This should solve your problem.

Finally, if you get the below message then congratulations you have made it. You now have successfully build the android 11 custom rom and now is the time to flash it onto your phone or emulator.

AOSP building process completed ssuccessfully
AOSP building process completed successfully

Flashing AOSP android 11 custom rom on emulator

In order to start the emulator make sure that the virtualization option is enabled on bios. The emulator is already been added to your path while building the aosp project. So, just the below command and it will flash the android 11 custom rom on the emulator.


Flashing AOSP android 11 custom rom on a physical device

In order to perform this process you first have to unlock your phone’s bootloader by going inside “Developers Options” inside settings. Note that this will void your phone’s warranty, do it on your own risk. If you are unable to see the “Developers Options” then click on “Build Number” inside settings under About phone for 5 times to enable this option. After that install the below two packages.

sudo apt install adb
sudo apt install fastboot

Now connect your phone to your computer and run the below commands.

Note that this will wipe out your data completely and flash the android 11 ROM on your device. There is no guarantee that this will successfully flash your device. And if it fails while flashing then your device will be of no use anymore. Make sure that you don’t your primary phone for flashing.

adb reboot bootloader
fastboot flashall -w

That’s all it is. Thanks for the read. If you like the content then support us on Patreon. Your support will surely help us in writing more of such content.

To read more such blogs about Open Source related stuff visit our blogs page on LionGuest Studios.

3 thoughts on “How to Download and Build AOSP Android 11 custom ROM from scratch”

  1. Pingback: AOSP: Can’t Run Emulator – Ask Android Questions

Leave a Comment

Your email address will not be published. Required fields are marked *