I wanted to document how I go about building AOSP because I regularly wipe my computer to install a new OS, try something out and then revert back to Ubuntu after awhile. Also, when I was doing this for the very first time I struggled to find good documentation on anything but the very basics.
N.B. In the past I built some ROMs for the Nexus 9 so that will be my goal here but the target device won’t have any bearing on the initial steps and only really comes into play in the final stages, so this should work for anyone.
First start by doing a clean install of Ubuntu 14.04.3 LTS downloaded from ‘here’ (if you don’t have it already). This is not the latest and greatest version of Ubuntu but the official AOSP documentation at least mentions Ubuntu 14.04 so it gives me a little more confidence that I won’t run into any trouble.
Once the OS up and running you need to install some pre-requisites:
Installing Java 7
Sadly Android is not ‘down with’ Java 8 yet. Load up a terminal by hitting the windows/start key, typing terminal and choosing it from the search results, or hit the alt+F2 keys together to start a new terminal.
Once it’s loaded up you should probably right click the icon in your launcher and choose to keep it there because you will be using this a lot.
In the terminal, run the following commands:
sudo apt-get updatesudo apt-get install openjdk-7-jdk
Installing Required Libraries
Once complete, you need to install a bunch of libraries by running the below command which you can copy and paste into your terminal (sometimes this will require ‘ctrl+shift+v’ rather than ‘ctrl+v’ to paste depending on your setup)
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 ccache \ libgl1-mesa-dev libxml2-utils xsltproc unzip
Due to limitations placed on regular users in Ubuntu around accessing USB devices directly, we need to create a rules file to allow us to do so by running this command, replacing ‘username’ with your actual username:
wget -S -O — http://source.android.com/source/51-android.rules | sed “s/&amp;amp;lt;username&amp;amp;gt;/$USER/” | sudo tee &amp;amp;gt;/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control — reload-rules
Now that the pre-requisites are installed you can go ahead and pull the source down from the repositories. To do this you need a tool called ‘repo’ which is basically a nice wrapper and some extra functionality on top of Git. To get this set up you need to:
Create a directory to store the repo tool in your home (~) directory:
Add this new directory to your PATH so that when you run commands from the terminal, the system knows what you want it to do. The most common way of doing this is to add a command to your .bashrc file. This is a file that is read each time you open up a new terminal and in here you can setup aliases, run commands etc. Open the ~/.bashrc file with your editor of choice. If you’re comfortable with vim then use that ‘vim ~/.bashrc’ or simply run ‘gedit ~/.bashrc’ if you don’t want to install and learn to use vim. When the file is open, add the following line to the very bottom on a new line:
Save and close the file.
Download the repo tool using Curl:
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
Make sure the repo tool is executable:
chmod a+x ~/bin/repo
We now need to make a directory to hold all of the source code that we will pull down onto our computer using the repo tool. I have named mine ‘AOSP’ but feel free to call it whatever you wish; Just remember to replace any use of AOSP in the commands below with the name/path of your folder if you do choose a different name.
Make the directory:
This will make the folder AOSP in your home directory.
Change directories so that you’re inside your new folder.
‘Pulling’ The Code
You now have a decision to make and it will depend on what device you are building for and what version of Android you would like to work with. I will be pulling the latest code for the Nexus 9 which at this time is Android Marshmallow — branch name = android-6.0.0_r2.
Check out this page to find out which version you should be using:
The bit you need is under the ‘Branch’ column and will be used in the next command.
Let’s initialize our working directory (‘AOSP’ in my case) to the branch we want to download.
repo init -u https://android.googlesource.com/platform/manifest -b android-6.0.0_r2
Now you need to pull the source code down onto your local machine. When I ran this most recently I had a lot of odd SSL network errors that kept stopping my download/pull, so I suggest you run it with the -f flag as below so that the download will recover from any errors and continue on. Also, depending on what processor you have you can multi-thread this download to speed things up a little using the -j command followed by a number to indicate the amount of threads you wish to use. I will be using -j5.
Before we run our ‘sync’ command we need to tell the server who we are. To set our email address and name run the following:
git config — global user.name YourNameHeregit config — global user.email yourEmailAddressHere
Obviously replacing ‘YourNameHere’ with your name and ‘yourEmailAddresHere’ with your email address. You will not be able to sync the source code without doing this (or at least I couldn’t).
repo sync -f -j5
This may take quite awhile depending on your processing power and internet connection so be patient.
Once you have finished sync’ing you should set up what is called ‘ccache’. This will help speed up future builds (sadly not the first) so it’s very handy to have.
Edit your ~/.bashrc file again using your preferred method as mentioned earlier. Add a new line at the bottom of the file and type:
Now load this bashrc into your current terminal:
Then set the cache size to 50GB by running:
prebuilts/misc/linux-x86/ccache/ccache -M 50G
from your working directory
Some last minute setup that you should always run before doing a build to tell the build system what target you want to build for i.e. what device, and also so you don’t spend a long time building for the wrong target, or a generic build which I have done accidentally many times.
and choose your target by typing the number and hitting enter. For me it’s ‘aosp_flounder-userdebug’ as I am targeting the Nexus 9 and this is it’s code name, so I enter ’16’ and hit enter.
Let’s Finally Build The Code!!
Now you can finally start building the code. This again can take a long time but thankfully it will only do so the very first time or when you intentionally force a clean build.
The -j flag can be used here again to help speed things up but it depends on your processor, memory and hard drive type (more memory, a better processor and an SSD will help things immensely).
For me the command I run is:
And now begins the long, long wait. You may need to leave it running overnight the first time. My first build took roughly 5Hrs & 36m.
When it eventually builds (and there should be no errors as you haven’t changed anything), your build .img files will be located at out/target/product/flounder (or whatever your product is named, out/target/product/shamu for Nexus 6, out/target/product/hammerhead for Nexus 5 etc.).