Building AOSP, fastbooting on a device

Soham Bhattacharya
7 min readNov 28, 2018

--

Android is beautiful. It’s open source; anyone can customize, anyone can contribute. Not just you can create beautiful applications which you deploy on Playstore but you can also have your own customized ROM. Custom ROMS are available for a range of devices like mobile, tablet or smartwatch. In this tutorial, we are focussed primarily on building a custom Android ROM on Nexus 6.

This tutorial will cover up almost everything on building AOSP along with the errors you can encounter. I’m gonna cover up the following:

  1. Setting up the environment
  2. Downloading the Android Source code
  3. Customizing and building our own Android image
  4. Setting up a device for fastbooting

System Requirements

Reference: https://source.android.com/setup/build/requirements

1. Ubuntu 16.04 LTS version (64 bit environment)
2. At least 250GB of free disk space to checkout the code and an extra 150GB to build it.

Note: If you have the latest version of Ubuntu like 18.04, some software versions might not work. For example, you can get an issue with make version. System may ask for a previous version like 3.81 or 3.81 in which case you will have to downgrade. Safer side go with Ubuntu 16.04.

3. Download these packages and install:

Note: If unable to do this step, there is no issue as system will prompt you to install these packages with a command later when you try to establish the environment.

Establishing a Build Environment

Reference: https://source.android.com/setup/build/initializing

  1. Installing necessary software packages and updates.

sudo apt-get update

2. Install open-jdk

sudo apt-get install openjdk-8-jdk

Downloading the source:

Reference: https://source.android.com/setup/build/downloading

  1. Installing Repo

// this will create a bin folder under home folder

mkdir ~/bin

// add bin folder path to PATH variable

PATH=~/bin:$PATH

// Downloading repo and make it executable

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

2. Initialise a Repo client

// Create an empty directory under home folder. This will have the Android source code and working files.

mkdir ~/WORKING_DIRECTORY
cd ~/WORKING_DIRECTORY

3. Run repo init to bring down the latest version of Repo with all its most recent bug fixes.

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

4. To check out a branch other than “master”, specify it with -b. This will ensure which android source tree you will be downloading.

repo init -u https://android.googlesource.com/platform/manifest -b android4.0.1_r1

Note: Make sure in the above command, you must replace android-4.0.1_r1 with the android version of your bootable device. Check branch and supported devices in this link and choose an appropriate branch.

For example, if you have a Nexus 6 with OS version 7.1.1, you will select android-7.1.1_r57.

5. Download Android source tree

This step will usually take hours depending on the internet speed and download size can be around >50 GB.

repo sync

After downloading, the below image shows the WORKING_DIRECTORY folder.

WORKING_DIRECTORY folder might look like this

Preparing to Build

Reference: https://source.android.com/setup/build/building

  1. Download and install device drivers

AOSP contains sources which can be run on different devices and to do this on a concrete device, you need additional hardware-related proprietary libraries. For example, now we are building Android version 7.1.1 and you should find libraries for your device with version 7.1.1. As we are building AOSP for Nexus 6, we will use Google’s Nexus driver.

Nexus 6 binaries for Android 7.1.1

Download them and extract them. Run them from checked-out folder which will create vendor / folder in WORKING_DIRECTORY or you can do the same from console which would be easy like this:

curl https://dl.google.com/dl/android/aosp/broadcom-shamu-n6f26q-beef18a1.tgz | tar -xvzf — ; bash ./extract-broadcom-shamu.sh

curl https://dl.google.com/dl/android/aosp/moto-shamu-n6f26q-f4c92bd8.tgz | tar -xvzf — ; bash ./extract-moto-shamu.sh

curl https://dl.google.com/dl/android/aosp/qcom-shamu-n6f26q-268fd13c.tgz | tar -xvzf — ; bash ./extract-qcom-shamu.sh

Note: Carefully change the value and link above of your device. Check that some folders would be create under vendor / folder like below.

folders inside vendor /

2. Now let’s do some customization of our own which will reflect in the custom ROM.

How about adding something to the settings menu of the Android OS. We can add a device info which resembles the custom ROM.

So move to the following folder: /packages/apps/Settings/res/xml/
and open the file which goes by the name: device_info_settings.xml .

Go to the bottom of XML file and add the following code which is below the comment “Customized setting”:

<!– Detailed build version –>

<Preference android:key=”build_number” android:title=”@string/build_number” …

<!–Customized setting: Custom ROM name –>
<Preference android:key=”rom_name”
android:enabled=”false”
android:shouldDisableView=”false”
android:title=”Soham ROM”
android:summary=”My custom ROM”/>

3. Build AOSP

// Clean up. Remove any existing output of previous build.

make clobber

// Set up environment

source build/envsetup.sh

// Choose a target. In this case, it is Nexus 6 which is aosp_shamu-userdebug.

lunch

Note: This will show a list of targets to build. Choose an appropriate target. Choosing 23. Refer to this.

lunch

// Get number of processes per user. This will give a value.

nproc
12

// Accordingly change below.

make -j12

This will take 1–2 hours or more depending on the number of processes. A successful message will be shown at the end.

Note: If you want to run in an emulator, then you should select either aosp_arm-eng or aosp_x86-eng from lunch menu. Go for aosp_x86 as it quite faster than the other one. Then, simply enter below command.

emulator

Fastbooting

Reference: https://source.android.com/setup/build/running.html#flashing-a-device

Note: Installing this custom ROM will erase all data from the device. If the device you are flashing on is a personal one, make sure you have taken a backup on a google account.

  1. Make sure you have downloaded Google USB drivers and Android platform tools from SDK Manager of Android Studio. They should be updated. You can check it by finding the versions of adb and fastboot.

adb version

// this will show the devices connected in bootable state

fastboot devices

2. Make sure your phone’s USB debugging is enabled and OEM is unlocked.

Note: If OEM unlocking is grayed out, see the message below it and take action accordingly.

3. Find the combination of keys and enter phone into fastboot mode. For example for Nexus 6, Press and hold Volume Down, then press and hold Power.

4. Unlock the bootloader

fastboot flashing unlock

5. Flashing on a device

fastboot flashall -w

Workstation:~/WORK$ fastboot flashall -w
(bootloader) slot-count: not found
(bootloader) slot-suffixes: not found
(bootloader) slot-suffixes: not found
(bootloader) current-slot: not found
(bootloader) slot-count: not found
(bootloader) slot-suffixes: not found
(bootloader) slot-count: not found
(bootloader) slot-suffixes: not found
target reported max download size of 536870912 bytes
(bootloader) has-slot:boot: not found
(bootloader) has-slot:recovery: not found
(bootloader) has-slot:system: not found
(bootloader) slot-count: not found
(bootloader) slot-suffixes: not found
(bootloader) slot-suffixes: not found
wiping userdata…
Creating filesystem with parameters:
Size: 59751923712
Block size: 4096
Blocks per group: 32768
Inodes per group: 8192
Inode size: 256
Journal blocks: 32768
Label:
Blocks: 14587872
Block groups: 446
Reserved block group size: 1024
Created filesystem with 11/3653632 inodes and 275392/14587872 blocks
wiping cache…
Creating filesystem with parameters:
Size: 268435456
Block size: 4096
Blocks per group: 32768
Inodes per group: 8192
Inode size: 256
Journal blocks: 1024
Label:
Blocks: 65536
Block groups: 2
Reserved block group size: 15
Created filesystem with 11/16384 inodes and 2089/65536 blocks
— — — — — — — — — — — — — — — — — — — — — —
Bootloader Version…: moto-apq8084–72.04
Baseband Version…..: D4.01–9625–05.45+FSG-9625–02.117
Serial Number……..: ZX1G322QCD
— — — — — — — — — — — — — — — — — — — — — —
checking product…
OKAY [ 0.002s]
sending ‘boot’ (8511 KB)…
OKAY [ 0.309s]
writing ‘boot’…
OKAY [ 0.264s]
sending ‘recovery’ (13901 KB)…
OKAY [ 0.464s]
writing ‘recovery’…
OKAY [ 0.181s]
sending sparse ‘system’ 1/2 (507878 KB)…
OKAY [ 20.670s]
writing ‘system’ 1/2…
OKAY [ 7.564s]
sending sparse ‘system’ 2/2 (466319 KB)…
OKAY [ 18.874s]
writing ‘system’ 2/2…
OKAY [ 6.035s]
erasing ‘userdata’…
OKAY [ 5.206s]
sending ‘userdata’ (141067 KB)…
OKAY [ 5.526s]
writing ‘userdata’…
OKAY [ 2.104s]
erasing ‘cache’…
OKAY [ 0.035s]
sending ‘cache’ (6248 KB)…
OKAY [ 0.233s]
writing ‘cache’…
OKAY [ 0.095s]
rebooting…

finished. total time: 67.722s

The -w option wipes the /data partition on the device; this is useful for your first time flashing a particular device but is otherwise unnecessary. This will flash the image you have build into the device.

It will create folders and files under out / folder.

out folder

Go to settings and you will see your customized ROM settings.

Woh! The ROM has my name!

Unlocking the bootloader will make your device less secure. Make sure that you re-lock your bootloader when the process is complete.

fastboot flashing lock

Note: If you haven’t downloaded the device drivers mentioned before, the phone will go into a bootlock where a google icon will come and go and you cant proceed.

If things go wrong, restoring the phone back to initial state can be done by downloading the appropriate factory image for your device and install it by flash-all command. Make sure your phone is in fastboot mode and is unlocked. Instructions given here.

Don’t worry! I’ll make an easy tutorial on it soon. Till then, Adios!

--

--

Soham Bhattacharya

Leading teams to build products from scratch | Deloitte | AI, Space & Blockchain enthusiast