Category Archives: Android

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.

AOSP: Architecture

Previously I wrote an article for getting your own copy of the AOSP source, AOSP: Download, I thought before we go a head and build a ROM it would be worth having a quick run through of the AOSP architecture so we can understand where we should be making our changes if/when we want to. 

Below is the high level architecture diagram of the AOSP provided by the AOSP website.

AOSP Architecture
AOSP Architecture

As you can see we have multiple layers within the system, they are:

Application Framework Used primarily by developers to create Android Apps. 
Binder IPC This layer allows for Inter-Process Communication (IPC). That is it allows Apps to cross process boundaries, similar to COM in Windows. 
System Services A collection of co-operating services which should be thought of as modular focused components. There are two groups of services, as shown above, System and Media services.
Hardware Abstraction Layer (HAL) As the name suggests this is an abstraction layer between Android and the underlying Hardware. The HAL defines an interface that must be implemented by hardware vendors.
Linux Kernel Android is built on top of the Linux Kernel and as with most distributions Android patches the Kernel to meet its needs.

This is a very quick introduction to the different layers of the AOSP and I’ll go into more detail for each layer in future posts and as we go along but I think this gives you a good idea of what to expect. Next we’ll build a ROM and run it in an emulator.

Android is a trademark of Google LLC.

AOSP: Download

I wrote a Bitcoin Node setup post recently (hopefully you had a go?) and thought I’d show you how to setup an Android Open Source Project (AOSP) development environment and how to download the AOSP. 

This tutorial will be for a Ubuntu 18.04 machine only. Other systems should be ok, such as ElementaryOS or newer versions of Ubuntu, but I’ve not tried any other. Feel free to get in touch if there is an OS you’d like me to demonstrate or if you’re having any issues

First things first, open a terminal using ctrl+tab+t and type the following:

sudo apt-get update

Followed by:

sudo apt-get upgrade

Once they are done we will have an up to date machine and should be good to get stuck into setting up for AOSP.

Our plan is to get the official AOSP source on our machines, if you want to check it out now you can visit https://android.googlesource.com/ for more on what we’re going to download.

If you popped by the link you’ll see there are a lot of repos available to you. Don’t manually download each one, we’ll get to that using the repo tool.

We need to install some packages to get our machine ready, so run:

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

Let’s install repo then:

sudo apt-get install curl
mkdir ~/bin
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo

Now that we have the repo tool we need to make it executable

chmod a+x ~/bin/repo

Incase you run into any problems I had to update my repo so it called python3 instead of python. To do this navigate to ~/bin, open repo, and run edit the first line from

#!/usr/bin/env python

to

#!/usr/bin/env python3

This tool will allow us to pull from multiple git repos to create our Android™ distribution.

We tell it the repos we want to pull using a manifest.xml file, which describes the projects to pull and their locations.

Now we want to get git, run the following:

sudo apt-get install git

Moving on to the reason we are here getting ourselves a copy of the AOSP:

First, lets create our directory.

mkdir -p ~/android/android-9.0.0_r50

Next up, navigate to the new directory

cd ~/android/android-9.0.0_r50

Then download the version of the source you want, see below for more details

repo init -u https://android.googlesource.com/platform/manifest.git -b android-9.0.0_r50

Then finally sync it, this will take a while.

repo sync

Same as above and worth considering is using the -j flag with your sync, it allows you to sync repos in parallel. By default it will do 4 parallel downloads.

To get a different version of the AOSP visit https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds and pick the one you’re after. Simply update ‘android-9.0.0_r50’ above with the Tag of the build you choose.

Great, so now we have our Android™ repos downloaded. 

We’ll leave it at that for this post but I’ll be putting another out soon where we will build and create our own Android™ Distribution to run on an emulator.

 

Android is a trademark of Google LLC.