Android Tutorials

Howto build Android from Source Code

Today I got an email, someone asked me if I can provide an easy guide which explains how to compile Android OS from the source, well no problem, here we go. I hope I can help with this small guide, there no pictures in it because I think it might confuses more than it helps, let’s keep the focus on the steps instead of some pictures.
The Android teacher. Are you read? Picture: Seeklogo

The Guide will explain in short how you compile/build Android OS from a Linux machine (I used Ubuntu in this case) but it will work on other distro too. Our focus are on 64-Bit because I see no future for 32-Bit and I not use it since several years – yep, let it die! Another fantastic guide is posted on XDA which explains how you build the OS with low hardware/network resources – but I assume everyone has a machine which is able to compile the source with less problems.

$ in front of the line means that this command needs to be run under a terminal window, I mention it because not everyone is a skilled user and you might want to start with my guide.

Set up your environment

There basically two ways, one is for the lazy folks and the other for nerds.

Automatically process

Go ahead and grab a repo with some useful scripts in it to make your life more comfortable:
$ sudo apt-get install git-core
$ git clone
$ cd scripts
$ ls
$ bash setup/<script-name>

Run the script corresponding to your Linux Distribution, for example: – For Arch based distros – For Ubuntu 16.04 or higher based distros

You can use other scripts provided by the mentioned repo to build it on LinuxMint, Ubuntu LTS,… it’s depending if there scripts provided for your distro.

The nerd way

// We are going to install Java 8 JDK
$ sudo apt-get update
$ sudo apt-get install openjdk-8-jdk
$ sudo apt-get install openjdk-8-jre

// Now we are getting the build tools
$ 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

Configure Git and the Android Repo

Git is an open source version control system which is incredibly robust for
tracking changes across repositories. Our Repo is Google’s tool for working with Git
in the context of the Android OS. In case you have any problems with the below mentioned commands, try running them as root: $ sudo -s.

// This step is only needed in case you run the nerd way explained above

$ curl > repo
$ chmod a+x repo
$ sudo install repo /usr/local/bin
$ rm repo

Then run these commands to setup the initial git setup:
$ git config --global "Your Name"
$ git config --global ""

Downloading the Source

First, create a folder for your source code:
$ mkdir ~/<folder-name> (eg. mkdir ~/DU or ~/PN-Layers)
$ cd ~/<folder-name>

When you go to build your ROM, you must download its source (makes sense?). All, if not most, ROMs will have their source code available on e.g. GitHub. To properly download the source, follow these steps:

  • Go to your ROM’s GitHub repository eg.
  • Search for a manifest usually called manifest.xml or android_manifest.xml.
  • Go into the repo and make sure you are in the right branch you can see it located right under the Commits tab.
  • Go into the README and search for a repo init command, if one exists, copy
    and paste it into the terminal and hit enter.
  • If one does not exist, you can make one with this command:
    repo init -u .git -b.

Basic initial example:
$ repo init -u -b n7x
After the repo has been initialized, run this command to download the source:
$ repo sync --force-sync -j$( nproc --all )
This process can take a while depending on your internet connection! Grab a coffee

Compile it!

It’s time to build the your first ROM! This process could take
15 minutes or in the worst case several hours depending on the speed of your PC. Usually it takes 15-50 minutes for most users.

Before you compile, you may also consider setting up ccache if you can spare
around 50 GB of your HDD/SSD space – if not, skip this process. Ccache is a compiler cache, which keeps previously compiled code stored so it can be easily reused. This speeds up build times dramatically! There exist alternatives to ccache but I only mention this because it’s easier for beginners to use.

Open your terminal, bashrc or an equivalent and type:
$ nano ~/.bashrc
Append export USE_CCACHE=1 to the end of this file then hit ctrl-X / Y, and hit enter.
$ source ~/.bashrc

After this step, run the following command if you used the nerd method of setup as explained above above:
$ prebuilts/misc/linux-x86/ccache/ccache -M 50G.
Run this command if you used the automatic method of setup above
$ ccache -M 50G

We are going to load up the compilation commands via:
$ . build/
Now tell the compiler which device you want to make:
$ breakfast OR lunch
$ mka bacon

Some ROMs may use their own bacon command, read their manifest as they will usually outline it. Other ROMs may not use mka at all, use make -j$( nproc --all ) instead.

If you get an error here, make sure that you have downloaded all of the
proper vendor files from your ROM’s repository, these are mostly the common errors because something is missing.

Once you tell it mka bacon, the computer will start the building process. You
will know that it is done when you see a message saying make completed and
telling you where your flashable zip is located.

Whenever you build again, make sure you run a clean build every time by placing
this command in between the other two:
$ make clobber

That’s it!

You successfully compiled your first ROM from scratch The benefit is obvious, you get fixes/features faster since once there on GitHub you can immediately build them, overall it also secures you a bit more because you get faster the security updates, but be aware that you might lose your warranty because flashing a ROM might requires root or another custom recovery which ends up loosing your warranty!

Once you’re done this and you get better on building the ROM you might want to contribute to the projects, start with small commits and fixes everything helps the community, learning by doing – reading is here the key combined with trying things yourself.

If there is interest I could post a guide how to compile Android under Windows.