AOSP: Build a ROM

Continuing with the AOSP theme, this week we’re going to build ourselves an Android ROM and run it in an emulator.

I recommend you check out my two previous AOSP posts, AOSP: Download and AOSP: Architecture, if you plan on following along. Particularly AOSP: Download as you’ll need to make sure your machine is setup.

Before we build there are some changes I should mention. Before Android 7.0 (Nougat) was released the build was done using GNU Make. Unfortunately, due to the size and complexity of Android this build system became slow, error prone, unscalable, and difficult to test. So from Android 7.0 the build is now done by the Soong build system (prior to Android 7.0 I hadn’t used Soong). Which itself makes use of Kati and Ninja

Kati is an clone of the GNU make tool with the goal of speeding up Android builds. The developers say it currently doesn’t do that but it does convert our Makefile to a Ninja file.

Ninja on the other hand is a small build system which focuses on speed.  The developers of this tool say Ninja differs from other build systems in two ways. The first is it is designed to have the input files generated by a higher level build system and to run build as fast as possible.

Given one of the reasons AOSP changed to this system was they were concerned about speed thats a good statement to make. If you’d like to learn more about this then check out the AOSP Build System article which goes into more details about this (certainly required if you want to understand the system indepth).

Lets build ourselves an AOSP ROM. Make sure you’re in a terminal and navigate to the directory you downloaded your AOSP to.

First we need to initialise our environment, this (as the name indicates) sets up our build environment for Android by defining a series of commands. In the root directory of the AOSP source, run:

. build/envsetup.sh

You should see the following output

envsetup.sh – output

Now we can run hmm to get the list of commands we can use. Feel free to run that now to see what are available. The main ones that you’ll use are as follows:

Command Description
lunch Displays the product-name and build_variants to build. You need to select the one you want to build.
m Runs builds from the top of the tree. You can use this to build the whole source or provide arguements to build specific targets.
mma Builds all the modules in the current directory, including their dependencies.
mmma Builds all the modules in the supplied directories, including their dependencies.
croot Changes directory to the AOSP source root.

Now we can run lunch and select the target we want to build. 

All of the build targets will be shown as BUILD-BUILDTYPEBUILD is the codename for the target product and BUILDTYPE is the target build variant.

The buildtype will be one of the following:

Buildtype Use
user Limited access; suited for production
userdebug Like user but with root access and debug capability; preferred for debugging
eng Development configuration with additional debugging tools

So lets make our selection, run:

lunch

You should get a lunch menu of BUILD-BUILDTYPE configurations to pick from. I will select aosp_arm-eng as this will give us a useful build for the emulator. 

Please note this will NOT run on your device so if you’re looking at other sources do no try to flash this. 

You can now build so run:

m

m can run with parallel tasks using the -jN argument, where N will be the task count. You don’t have to provide the argument and the build system will assign it for you for your system if you don’t.

This will take a while so grab a coffee or something and come back to it in a bit.

So long as nothing when wrong you should now be ready to run you newly built Android distribution in an emulator by running:

emulator

Congratulations, you’ve just built the AOSP and ran it. In the future we’ll take a look at where we need to look to modify the different layers depending on what it is we are looking to do and how we can build a distribution to run on a commercial device.