Building the Android Open Source Project

Working on the Android platform is a task addressed by Google and OEMs mostly. In this tutorial, you’ll get insights into building the AOSP. By Antonio Roa-Valverde.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

Using the Repo Tool

The Android codebase contains a massive amount of source code organized in different Git repositories. Repo is a tool built on top of Git to make the development workflow easier. It’s a Python script that takes care of interacting with the revision control system, helping you manage multiple Git repositories.

Repo does this using a manifest file that links to the current revision of each Git repository. If you’ve worked with Git submodules before, this is similar.

Note: It’s important to understand that the manifest file is not the Android manifest you use when developing Android apps. The name here is an unfortunate coincidence.

In this tutorial, you’ll use Repo to download the Android sources.

Create a new folder named bin inside /home/aosp. Then add /home/aosp/bin to the PATH environment variable:

PATH=/home/aosp/bin:$PATH

This ensures that Repo can run from any path in your terminal.

Run the following command to download Repo to the container:

curl https://storage.googleapis.com/git-repo-downloads/repo > /home/aosp/bin/repo

This uses curl to do an HTTP request to the given URL, then redirects the output with > to /home/aosp/bin/repo. Basically, you download the Repo script and copy it to the bin folder.

Then, run the following command:

chmod +x /home/aosp/bin/repo

This makes Repo executable, as +x indicates.

To ensure everything worked, run the following command:

repo help

This shows the following output, indicating that Repo is ready to use:

Repo help output without init

Check that the output in your terminal matches that in the screenshot, to make sure everything is in place.

Great, your container is now ready to build the AOSP!

Next, you’ll focus on getting the AOSP source tree.

Happy expression

Getting the Sources

Before downloading the Android sources with Repo, you need to set up your user name and email address in Git. Without them, Repo will stop and won’t download anything.

Use this command to quickly check if they’re available:

git config -l

If they’re configured, you should see something like the following in your terminal:

user.email=you@email.com
user.name=you

If this isn’t the case, run the below to set up your username:

git config --global user.name "Your name"

Next, run the below to set up your email address:

git config --global user.email "you@email.com"

All set, now you will initialize a folder that will hold all the source files for AOSP and also the tracked history of those source files.

Ready to jump in? Then move to the next section!

Initializing the Source Folder

Then, in /home/aosp, create a new folder named source to hold the sources. Switch your current path to /home/aosp/source, then run the following command:

repo init -u https://android.googlesource.com/platform/manifest

Here, init installs Repo in the current directory. It also initializes a .repo folder, which contains information about the Git repositories that the manifest points to. You pass URL information with -u param, in this case passing in the URL for AOSP manifest file. This is how Repo gets information from the master branch.

Get an overview of all the branches by visiting the Android source code tags and builds site. In this tutorial, you’ll use the master branch.

Note: If you want to check out a different branch, specify it by passing an additional -b argument, as in the example below:
repo init -u https://android.googlesource.com/platform/manifest -b android-10.0.0_r33

Get an overview of all the branches by visiting the Android source code tags and builds site. In this tutorial, you’ll use the master branch.

repo init -u https://android.googlesource.com/platform/manifest -b android-10.0.0_r33

Once the previous command finishes, check that you’ve successfully set up Repo by running this command again:

repo help

This time, this command shows the following screen:

Repo help output, now initialized

This is a list of the different commands available with Repo. Check that you get the same output in your terminal.

For more information about Repo’s different options, visit the Repo Command Reference site.

Downloading the Source

Finally, run the following command to download the AOSP source tree to the working directory. Make sure you have a reliable Internet connection and enough space on your drive. Since this process could take between one to two hours, it would be really annoying having to restart it!

repo sync

While you’re waiting is a perfect time to prepare a tasty cup of tea. :]

After a successful sync, your working directory will contain the folders shown below:

AOSP source tree

Now that you have everything in place, it’s time to actually build the AOSP.

Building the AOSP

The first step to build the AOSP based on the sources you just downloaded is to set up the build environment. Run this command from /home/aosp/source:

source build/envsetup.sh

This will make a set of useful commands — which you’ll need for the next steps — available in your terminal.

Then, type lunch in your terminal and execute it. This command shows the list of target device types that you can build.

List of available lunch options

The default option is aosp_arm-eng, which builds the Android images for a device of type phone using the ARM architecture.

You could build this, but when you run the emulator on your computer, you’ll notice that it’s very slow. A better option is to build the target aosp_x86_64-eng.

Select this target by entering the associated index from the list of options. In this case, enter 23 and press Return. The terminal will show the following output:

The output generated by executing the lunch command after selecting target API and type of emulator

Note: For a good overview of the different build targets, please refer to the Google documentation about build layers in AOSP.

Running the Build

At this point, the environment is ready to trigger the build. To do so, type m and press Enter. This starts the build process, which takes between two and three hours, depending on your computer.

Are you ready for a second cup of tea? :]

Once the build process completes, you’ll find the result in /home/aosp/source/out/target/product.

To start the emulator, simply execute emulator. This command is only available because you executed source build/envsetup.sh and then selected a build target with lunch earlier. If you closed the terminal for any reason after the build completed, you’ll need to run those commands again.

AOSP animation

Great job, you just built the AOSP for the first time!

However, your quest doesn’t end here. In the next section, you’ll learn how to modify the Android boot animation.