Noob's guide to building AOSP from scratch.

Search This thread

codevalley

Member
Jun 17, 2010
19
28
So, I have been an Android developer since 2009 (HTC dream), and have been actively "consuming" XDA, custom ROMs and other tweaks. Surprisingly, never tried to build my own ROM from scratch.
Recently, something made me want to build the ROM, so that I can make some changes to the way SystemUI behaves (specifically putting some app shortcuts to my Pixel phone(s), like camera etc. which are now removed in Android 10). So, after a week's struggle I got to where I wanted to reach. (90% time spent in getting the first successful flash. 1% feature development. 9% feature polishing).

Here is my guide to all beginners. (It is pretty simple, if you know the steps).

System setup
I have always been a Windows user (and I love my Surface(s)), but you cannot build Android on Windows machines (as clearly called out in source.android.com). I still tried to install Ubuntu shell from Microsoft store, and build (Spoiler alert: Does not work).

Next is Mac. Android can be built in Mac, I got it build in Mac. But, it is not easy. Especially with setting up the environment, having the right version of MacOS (doesn't work on Catalina yet). And also, challenges with filesystem format (Android building only works on case sensitive file system, so you have to create such a partition). Android building needs at least 160GB of disk space (so unless you are super rich and have 512GB+ Macbook with top specs, it is going to be hard).

My choice machine hence became, my two desktops (i7 4 core, 16GB, 1TB SSD, Ubuntu 18.04 and Xeon 12 core, 32GB, 512GB disk, with Ubuntu 18.04).
There is a reason why I specifically talk about these two machines. To build Android fast (cold clean build in less than 4 hours), you need
  • Fast processors, and more cores
  • Lots of RAM
  • A SSD disk (with 200GB space)
If you are missing any of the above 3, you will build times will go up. I have found for hot build, both machines did a decent job (2-3 mins if you are working on single module), but SSD was more important than cores, and RAM.

Setting up your Ubuntu machine. {ETA 30 mins}
Android has official (and clearly laid out) steps here.
But for Ubuntu these are pretty much the steps.
Code:
$sudo apt-get 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 libgl1-mesa-dev libxml2-utils xsltproc unzip
And the guide doesn't mention this, but you need Python2.7, so get this.
Code:
# refreshing the repositories
sudo apt update
# its wise to keep the system up to date!
# you can skip the following line if you not
# want to update all your software
sudo apt upgrade
# installing python 2.7 and pip for it
sudo apt install python2.7 python-pip
# installing python-pip for 3.6
sudo apt install python3-pip
Also install adb.
Code:
sudo apt install android-tools-adb android-tools-fastboot
If you have come till here, you're ready to build for different devices.

Getting the code ready to build {ETA 5 hours - 1 day}
Most of this is also mentioned in the AOSP official website, but some stuff are tricky, I will try to highlight those steps here.

We are going to build the ROM for Pixel 3 (Android 10 - QP1A.191105.003 )

Download and explode the code {ETA 2-3 hours, depending on internet speed}
Here we are talking about downloading at least 20GB of code (text heavy content) over the internet. Going to be excruciatingly slow.
Also, we will be downloading code for specific device model, so if you want to do it for a newer model, you will have to go through the grind again.
Although, technically it might be possible to have the same folder contain code for multiple devices, it is too risky IMO, something goes wrong, you lose everything.
Recommended folder structure would be
aosp --> device 1
aosp --> device 2
......
aosp --> device n
With each folder containing over 150GB of contents (after downloading, building etc), so in practical sense, n could be only 3-4 at max.

Setting up repo.
Repo is a tool that Google uses to checkout and manage the AOSP code in your local machine. Once you download the codebase, you can use the command to resync, update, code base.
Code:
mkdir ~/bin
PATH=~/bin:$PATH
You should persist this folder in your PATH variable all the times.

Code:
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
This sets up repo in your machine.

One final step before you actually start the long download, setup your git details.
Code:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Now download the code. Like I previously suggested I would do this.
Code:
mkdir ~/aosp
cd ~/aosp
mkdir pixel3
cd pixel3

Now, let's start getting the code home.
Code:
repo init -u https://android.googlesource.com/platform/manifest -b android-10.0.0_r10 --depth=1
Here we have done a bunch of things.
  • We have chosen a particular Android build tag to download (branch). You can follow the link to choose which branch you want to checkout, based on your test phone and Android version you want to build.
  • We have asked to only download the latest version of the branch and not all of the branch (--depth), this considerably reduces our download time.
Now that we have decided what to download, let's download the code with this command.
Code:
repo sync -qc -j4
This command is going to take a while to download over 20GB of code. In the meanwhile, let's see what we did here.
  • -q Asks the download to be silent (which means it will show just overall progress)
  • -c Makes sure we are only downloading current branch
  • -j[x] This the tricky one. Let's talk about this.
With -j we are asking repo to spawn multiple downloads (parallelly), to speed up the process. We will see this flag going forward in other places also. We should keep the value of x to number of cores we have in our machine. To find how many cores you have run
Code:
nproc --all
. Note that I have had situations where I put a very high value for n (higher than my cores as well), and eventually ran my JVM out of RAM to run the command (in parallel). So, the trade off here is to restrict it to the core number.

***Key step: Download radio drivers.***
Most tutorials miss this or mention it very subtly. But, without this step the ROM you flash won't boot to the home screen (you will be in the boot loop).
Go to the driver binaries page, and download the right zip files for the Android build version (android-10.0.0_r10) and device (Pixel 3) you chose earlier in the repo command.
You will be downloading two zip files (one vendor image zip and one radio drivers zip), both zips will have on shell script file each (.sh), just put those two files in your repo folder (~aosp/pixel3) and run the scripts. It will download the required proprietary files (after asking you to accept the terms). Do not miss this step.. I lost 3 days trying to find the reason for my ROM not booting up, this was it.

Let's build our code
Now things are more definitive.

Code:
source build/envsetup.sh
This command basically sets up your environment, adding necessary commands to path etc.

Code:
lunch aosp_blueline-userdebug
You can read more about this command here.
Basically this sets up the right parameters to build for your specific model. The param can derived based on aosp_[device code]-[userdebug | eng | user].

Once you have run the above two commands, you can *finally* build your codebase.
Code:
m droid -j4
m basically makes and builds the whole codebase.
Code:
droid
refers to the defaults target configuration (optional). -jN is to specify parallelism (equal to number of cores you have).

This command could take anywhere between 4-12 hours for the first run. But, if you followed all steps above, you should have a green message in the end saying this
Code:
[COLOR="SeaGreen"]#### build completed successfully (2:03:04 (hh:mm:ss)) ####[/COLOR]

Flashing your phone
Now, you're 50% safe when your build has finished successfully. Now, next 50% depends if you're able to flash it and get the phone booting.
This part most of you should know, so I am keeping it brief.

Enter fastboot
Code:
adb reboot bootloader

Unlock your bootloader
Code:
fastboot flashing unlock

Flash your Build
From the root folder of your repository (~/aosp/pixel3)
Code:
fastboot flashall -w

In a few minutes your device should be booting to the freshing baked ROM that you made.

What next?
  • You can just repeat
    Code:
    m droid -j4
    to repeat builds
  • You can also go to a specific module folder and execute
    Code:
    mm
    to only build that module
  • You can use adb sync to update specific modules without flashing again (this never worked for me, always bricked my device)
  • Use *fastboot flashall* without [-w] flag to flash over existing ROM without losing user data.
  • You can clean up the whole builds and rebuild everything from scratch. Run
    Code:
    make clobber
    to clean your build, and use
    Code:
    m
    to build again
  • You could face adb issues (device not detected) in Ubuntu. I am not going into details of how to fix that

This has been pretty much my journey so far with AOSP. I am comfortable making changes to modules and building them again.
 

mimiQ

Member
Nov 29, 2019
17
14
Aw man, thanks for posting this. Never thought building rom itself would take this much effort & resources. Rom devs are serioulsy awesome ppl. :good:
 
  • Like
Reactions: whyc and Ktnxd

Chezbel

Senior Member
Jan 16, 2011
1,980
1,668
Taipei
Thanks very much for creating this. I didn't try flashing the result yet, but the build finished without any problems.
 

pvlub

New member
Nov 29, 2019
2
0
thank you very much for your post, I also want to modify little bit in code aosp and test this changes. Could you plaese provide advice about how I can open code (Android studio?), do some changes and test it by emulator?
 

FcukBloat

Senior Member
Jun 18, 2012
6,284
3,012
thanks for the great guide :eek:
but following it gapps will not be included in the build, correct?
do you guys know how to include open gapps?

tia!
 

rorlig

New member
Mar 4, 2020
1
0
hi everything worked in this guide in terms of the build. While flashing the device all steps succeed but during boot the pixel is stuck at the google loading screen. Any suggestions. I have been stuck on the screen for > 30 minutes.
 

pvlub

New member
Nov 29, 2019
2
0
hi everything worked in this guide in terms of the build. While flashing the device all steps succeed but during boot the pixel is stuck at the google loading screen. Any suggestions. I have been stuck on the screen for > 30 minutes.
Maybe you did not download appropriate .sh scripts for your device or didn't run them successfully before building the code. These scripts additionally download files and without them you could have these problems which you mentioned.
 

rcmpayne

Member
Sep 3, 2014
28
4
I built and loaded AOSP Andorid 9 for PIxel 2 using the eng build vs the userdebug and its works however, when I start installing and granting Google services and such it works but i get a lot of crashes. do you have Google working and not crashing all the time?


```
$ adb root
$ adb remount

$ adb shell
$ cd /system/priv-app
$ mkdir GoogleServicesFramework
$ mkdir Phonesky
$ mkdir PrebuiltGmsCorePi

$ cp /sdcard/GoogleServicesFramework.apk GoogleServicesFramework/GoogleServicesFramework.apk
$ cp /sdcard/Phonesky.apk Phonesky/Phonesky.apk
$ cp /sdcard/PrebuiltGmsCorePi.apk PrebuiltGmsCorePi/PrebuiltGmsCorePi.apk

$ chmod 755 GoogleServicesFramework
$ chmod 755 Phonesky
$ chmod 755 PrebuiltGmsCorePi

$ chmod 644 GoogleServicesFramework/GoogleServicesFramework.apk
$ chmod 644 Phonesky/Phonesky.apk
$ chmod 644 PrebuiltGmsCorePi/PrebuiltGmsCorePi.apk

:: Need to add permissions for the three apps above
::If a device fails to boot, you need to logcat and grep for " - not in privapp-permissions whitelist" and add any missing items in the xml

$ adb push C:\Users\username\Desktop\PIxel2_9.0.0_eng_build\privapp-permissions-platform.xml /etc/permissions/privapp-permissions-platform.xml
```
 

juampapo546

Senior Member
May 8, 2018
174
56
Buenos Aires
Moto G6 Plus
Hello, I'm interested on the Mac os part. I've been building pixel experience on Ubuntu form am external HDD but because it's a 2011 iMac I have USB 2.0 and r/w speeds are really low slowering the whole process. On the internal drive I have a 500gb SSD that I'd like to use for compiling but partitioning is not an option, could you help me setting up enviroment?
PD: I tried setting it up with brew but I am missing dependencies I can't (don't know how) install them with brew, all guides are for Ubuntu or for Mac is but old.
Thank you in advance!
 

mumphus

New member
Dec 19, 2020
2
0
I am having issue, I did this and got:

#### build completed successfully (17:26:44 (hh:mm:ss)) ####

flashing claims to have succeeded but when the phone reboots it just goes back to fastboot mode and says "no valid slot too boot to"

The last few lines of output when doing "fastboot flashall -w" are:
Erase successful, but not automatically formatting.
File system type raw not supported.
Erasing 'metadata' OKAY [ 0.007s]
Erase successful, but not automatically formatting.
File system type raw not supported.
Rebooting OKAY [ 0.000s]
Finished. Total time: 82.933s

Is the filesystem raw not supported normal? Other than that I am really not sure why this isn't working.
 

mrc99

Member
May 29, 2016
13
1
Great guide. I am working on creating a custom rom myself. I've been wondering if it's possible to prevent system apps from being included in the build. There are a few apps that I use f-droid apps in their place (example K9 mail for stock email app) and don't want to see them re-appear when the ROM is updated. If this is not possible, can they be removed from the build before flashing?
 

Avinash2608

Member
May 30, 2020
6
1
Hi.. I'm Building AOSP 10 for POCO F1(beryllium). i dont know which command should i choose in lunch cause my device isnt listed.. They have only for Pixel Devices.. Pls guide through it
 
  • Like
Reactions: charan66

charan66

Member
Jul 5, 2018
18
1
***Key step: Download radio drivers.***
Most tutorials miss this or mention it very subtly. But, without this step the ROM you flash won't boot to the home screen (you will be in the boot loop).
Go to the driver binaries page, and download the right zip files for the Android build version (android-10.0.0_r10) and device (Pixel 3) you chose earlier in the repo command.
You will be downloading two zip files (one vendor image zip and one radio drivers zip), both zips will have on shell script file each (.sh), just put those two files in your repo folder (~aosp/pixel3) and run the scripts. It will download the required proprietary files (after asking you to accept the terms). Do not miss this step.. I lost 3 days trying to find the reason for my ROM not booting up, this was it.***




How can I get this 2 zip files for my Samsung device (SM-A715F). Thank you
 

Top Liked Posts

  • There are no posts matching your filters.
  • 25
    So, I have been an Android developer since 2009 (HTC dream), and have been actively "consuming" XDA, custom ROMs and other tweaks. Surprisingly, never tried to build my own ROM from scratch.
    Recently, something made me want to build the ROM, so that I can make some changes to the way SystemUI behaves (specifically putting some app shortcuts to my Pixel phone(s), like camera etc. which are now removed in Android 10). So, after a week's struggle I got to where I wanted to reach. (90% time spent in getting the first successful flash. 1% feature development. 9% feature polishing).

    Here is my guide to all beginners. (It is pretty simple, if you know the steps).

    System setup
    I have always been a Windows user (and I love my Surface(s)), but you cannot build Android on Windows machines (as clearly called out in source.android.com). I still tried to install Ubuntu shell from Microsoft store, and build (Spoiler alert: Does not work).

    Next is Mac. Android can be built in Mac, I got it build in Mac. But, it is not easy. Especially with setting up the environment, having the right version of MacOS (doesn't work on Catalina yet). And also, challenges with filesystem format (Android building only works on case sensitive file system, so you have to create such a partition). Android building needs at least 160GB of disk space (so unless you are super rich and have 512GB+ Macbook with top specs, it is going to be hard).

    My choice machine hence became, my two desktops (i7 4 core, 16GB, 1TB SSD, Ubuntu 18.04 and Xeon 12 core, 32GB, 512GB disk, with Ubuntu 18.04).
    There is a reason why I specifically talk about these two machines. To build Android fast (cold clean build in less than 4 hours), you need
    • Fast processors, and more cores
    • Lots of RAM
    • A SSD disk (with 200GB space)
    If you are missing any of the above 3, you will build times will go up. I have found for hot build, both machines did a decent job (2-3 mins if you are working on single module), but SSD was more important than cores, and RAM.

    Setting up your Ubuntu machine. {ETA 30 mins}
    Android has official (and clearly laid out) steps here.
    But for Ubuntu these are pretty much the steps.
    Code:
    $sudo apt-get 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 libgl1-mesa-dev libxml2-utils xsltproc unzip
    And the guide doesn't mention this, but you need Python2.7, so get this.
    Code:
    # refreshing the repositories
    sudo apt update
    # its wise to keep the system up to date!
    # you can skip the following line if you not
    # want to update all your software
    sudo apt upgrade
    # installing python 2.7 and pip for it
    sudo apt install python2.7 python-pip
    # installing python-pip for 3.6
    sudo apt install python3-pip
    Also install adb.
    Code:
    sudo apt install android-tools-adb android-tools-fastboot
    If you have come till here, you're ready to build for different devices.

    Getting the code ready to build {ETA 5 hours - 1 day}
    Most of this is also mentioned in the AOSP official website, but some stuff are tricky, I will try to highlight those steps here.

    We are going to build the ROM for Pixel 3 (Android 10 - QP1A.191105.003 )

    Download and explode the code {ETA 2-3 hours, depending on internet speed}
    Here we are talking about downloading at least 20GB of code (text heavy content) over the internet. Going to be excruciatingly slow.
    Also, we will be downloading code for specific device model, so if you want to do it for a newer model, you will have to go through the grind again.
    Although, technically it might be possible to have the same folder contain code for multiple devices, it is too risky IMO, something goes wrong, you lose everything.
    Recommended folder structure would be
    aosp --> device 1
    aosp --> device 2
    ......
    aosp --> device n
    With each folder containing over 150GB of contents (after downloading, building etc), so in practical sense, n could be only 3-4 at max.

    Setting up repo.
    Repo is a tool that Google uses to checkout and manage the AOSP code in your local machine. Once you download the codebase, you can use the command to resync, update, code base.
    Code:
    mkdir ~/bin
    PATH=~/bin:$PATH
    You should persist this folder in your PATH variable all the times.

    Code:
    curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    chmod a+x ~/bin/repo
    This sets up repo in your machine.

    One final step before you actually start the long download, setup your git details.
    Code:
    git config --global user.name "Your Name"
    git config --global user.email "you@example.com"

    Now download the code. Like I previously suggested I would do this.
    Code:
    mkdir ~/aosp
    cd ~/aosp
    mkdir pixel3
    cd pixel3

    Now, let's start getting the code home.
    Code:
    repo init -u https://android.googlesource.com/platform/manifest -b android-10.0.0_r10 --depth=1
    Here we have done a bunch of things.
    • We have chosen a particular Android build tag to download (branch). You can follow the link to choose which branch you want to checkout, based on your test phone and Android version you want to build.
    • We have asked to only download the latest version of the branch and not all of the branch (--depth), this considerably reduces our download time.
    Now that we have decided what to download, let's download the code with this command.
    Code:
    repo sync -qc -j4
    This command is going to take a while to download over 20GB of code. In the meanwhile, let's see what we did here.
    • -q Asks the download to be silent (which means it will show just overall progress)
    • -c Makes sure we are only downloading current branch
    • -j[x] This the tricky one. Let's talk about this.
    With -j we are asking repo to spawn multiple downloads (parallelly), to speed up the process. We will see this flag going forward in other places also. We should keep the value of x to number of cores we have in our machine. To find how many cores you have run
    Code:
    nproc --all
    . Note that I have had situations where I put a very high value for n (higher than my cores as well), and eventually ran my JVM out of RAM to run the command (in parallel). So, the trade off here is to restrict it to the core number.

    ***Key step: Download radio drivers.***
    Most tutorials miss this or mention it very subtly. But, without this step the ROM you flash won't boot to the home screen (you will be in the boot loop).
    Go to the driver binaries page, and download the right zip files for the Android build version (android-10.0.0_r10) and device (Pixel 3) you chose earlier in the repo command.
    You will be downloading two zip files (one vendor image zip and one radio drivers zip), both zips will have on shell script file each (.sh), just put those two files in your repo folder (~aosp/pixel3) and run the scripts. It will download the required proprietary files (after asking you to accept the terms). Do not miss this step.. I lost 3 days trying to find the reason for my ROM not booting up, this was it.

    Let's build our code
    Now things are more definitive.

    Code:
    source build/envsetup.sh
    This command basically sets up your environment, adding necessary commands to path etc.

    Code:
    lunch aosp_blueline-userdebug
    You can read more about this command here.
    Basically this sets up the right parameters to build for your specific model. The param can derived based on aosp_[device code]-[userdebug | eng | user].

    Once you have run the above two commands, you can *finally* build your codebase.
    Code:
    m droid -j4
    m basically makes and builds the whole codebase.
    Code:
    droid
    refers to the defaults target configuration (optional). -jN is to specify parallelism (equal to number of cores you have).

    This command could take anywhere between 4-12 hours for the first run. But, if you followed all steps above, you should have a green message in the end saying this
    Code:
    [COLOR="SeaGreen"]#### build completed successfully (2:03:04 (hh:mm:ss)) ####[/COLOR]

    Flashing your phone
    Now, you're 50% safe when your build has finished successfully. Now, next 50% depends if you're able to flash it and get the phone booting.
    This part most of you should know, so I am keeping it brief.

    Enter fastboot
    Code:
    adb reboot bootloader

    Unlock your bootloader
    Code:
    fastboot flashing unlock

    Flash your Build
    From the root folder of your repository (~/aosp/pixel3)
    Code:
    fastboot flashall -w

    In a few minutes your device should be booting to the freshing baked ROM that you made.

    What next?
    • You can just repeat
      Code:
      m droid -j4
      to repeat builds
    • You can also go to a specific module folder and execute
      Code:
      mm
      to only build that module
    • You can use adb sync to update specific modules without flashing again (this never worked for me, always bricked my device)
    • Use *fastboot flashall* without [-w] flag to flash over existing ROM without losing user data.
    • You can clean up the whole builds and rebuild everything from scratch. Run
      Code:
      make clobber
      to clean your build, and use
      Code:
      m
      to build again
    • You could face adb issues (device not detected) in Ubuntu. I am not going into details of how to fix that

    This has been pretty much my journey so far with AOSP. I am comfortable making changes to modules and building them again.
    2
    Aw man, thanks for posting this. Never thought building rom itself would take this much effort & resources. Rom devs are serioulsy awesome ppl. :good:
    1
    Hi.. I'm Building AOSP 10 for POCO F1(beryllium). i dont know which command should i choose in lunch cause my device isnt listed.. They have only for Pixel Devices.. Pls guide through it