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 user.name "Your Name"
git config --global user.email "you@example.com"
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
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/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 https://android.googlesource.com/platform/manifest -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/envsetup.sh

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.

lunch
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.

  • libncurses.so.5: 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.

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.

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

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

  2. You should see another message “repo has been initialized in /home/user/aosp11” which confirms that you have initialized the repo.
    Mine says /home/user/Desktop
    so, does it matter, Should i do this step again while in aospll directory?

    1. The location where you have initialized the repo that directory will be shown there. No, it doesn’t matter where you have initialized the repo and no need to do this process again.

  3. hi, i started the “repo sync” but ran out of HDD space, where have the files been downloading as both “aosp11” and “bin” folders are empty. thanks

  4. Hey! I am looking to make the android 11 custom rom for the raspberry pi4.
    If possible can you tell me how can we make a “.img” file for the OS, Also will this work on the raspberry pi 4.

  5. i have Manjaro linux and windows dual boot. Manjaro has only 100 gb of allocated space, so can I download the aosp into the HDD which is an ntfs format and build from there? Also can I use Manjaro for building or should I use only Ubuntu?

  6. so the requirement in source.android.com said I must install Ubuntu 18.04. So can it run in Windows? because i have Ubuntu 18.04 in Windows and its just a terminal not using Virtualbox or other thing.

  7. Thanks very much Sahil for making this post. It’s very helpful!!
    How long does it usually take to flash the custom ROM onto an emulator?

  8. Machiraju Srinvias

    Getting following error.
    [ 0% 6/12551] //hardware/interfaces/audio/5.0:android.hardware.audio@5.0 header-abi-linker android.hardware.audio@5.0.so.lsdump
    FAILED: out/soong/.intermediates/hardware/interfaces/audio/5.0/android.hardware.audio@5.0/android_vendor.30_arm64_armv8-a_shared/android.hardware.audi
    o@5.0.so.lsdump
    prebuilts/clang-tools/linux-x86/bin/header-abi-linker -o ‘out/soong/.intermediates/hardware/interfaces/audio/5.0/android.hardware.audio@5.0/android_ve
    ndor.30_arm64_armv8-a_shared/android.hardware.audio@5.0.so.lsdump’ -so out/soong/.intermediates/hardware/interfaces/audio/5.0/android.hardware.audio@5
    .0/android_vendor.30_arm64_armv8-a_shared/android.hardware.audio@5.0.so -arch arm64 -Isystem/libhidl/base/include -Isystem/libhidl/transport/include
    -Isystem/libhwbinder/include -Isystem/core/base/include -Iexternal/fmtlib/include -Isystem/core/libutils/include -Isystem/core/libbacktrace/include -I
    system/core/liblog/include_vndk -Isystem/core/libsystem/include -Isystem/core/libcutils/include_vndk -Isystem/core/libprocessgroup/include -Isystem/co
    re/libcutils/include -Iout/soong/.intermediates/system/libhidl/transport/manager/1.0/android.hidl.manager@1.0_genc++_headers/gen -Iout/soong/.intermed
    iates/system/libhidl/transport/manager/1.1/android.hidl.manager@1.1_genc++_headers/gen -Iout/soong/.intermediates/system/libhidl/transport/manager/1.2
    /android.hidl.manager@1.2_genc++_headers/gen -Iout/soong/.intermediates/system/libhidl/transport/base/1.0/android.hidl.base@1.0_genc++_headers/gen -Io
    ut/soong/.intermediates/system/libhidl/transport/safe_union/1.0/android.hidl.safe_union@1.0_genc++_headers/gen -Iout/soong/.intermediates/hardware/int
    erfaces/audio/common/5.0/android.hardware.audio.common@5.0_genc++_headers/gen -Iout/soong/.intermediates/hardware/interfaces/audio/effect/5.0/android.
    hardware.audio.effect@5.0_genc++_headers/gen -Iout/soong/.intermediates/hardware/interfaces/audio/5.0/android.hardware.audio@5.0_genc++_headers/gen @’
    out/soong/.intermediates/hardware/interfaces/audio/5.0/android.hardware.audio@5.0/android_vendor.30_arm64_armv8-a_shared/android.hardware.audio@5.0.so
    .lsdump’.rsp
    Failed to parse the shared library (.so file): out/soong/.intermediates/hardware/interfaces/audio/5.0/android.hardware.audio@5.0/android_vendor.30_arm
    64_armv8-a_shared/android.hardware.audio@5.0.so
    Failed to link and dump elements
    12:23:34 ninja failed with: exit status 1

  9. Hi, Sahil…. It was a wonderful tutorial and You’re works are great and i follow them everytime!!!
    I have successfully completed the build but i couldnt understand how to access my build and when i entered ’emulator’ Command, the emulator shows up but it is blank…I installed android studio so that i can start my build there but idk how to do it. Can u pls tell me how to solve this or pls make a video explaining it.. Thank you

  10. Pingback: Google's New Fuchsia OS: Download And Build From Source Code | LionGuest Studios

  11. adb reboot bootloader -> adb server version (41) doesn’t match this client (39); killing…
    * daemon started successfully
    error: no devices/emulators found
    fastboot flashall -w ->

    after adding this commands it gave me and phone is connected. What should i do now , can something help me with this?

  12. can you make video for the process with Flashing AOSP android 11 custom rom on a physical device cause there are a lot of problems in linux, different errors

  13. Today is 2021. Why Android project still doesn’t have a Dockerfile in its repo to make a build? I wonder why people like having pain to configure all that and then describe how to overcome those gotchas?

Comments are closed.