Final AOSP for Hammerhead

Final AOSP for Hammerhead

How to Build the Latest AOSP Image for the Nexus 5

This guide will walk you through all of the steps required to build a working Android Open Source Project (AOSP) image for the Nexus 5 (code named ‘Hammerhead’). The Android Open Source Project is actually where you want to start on your AOSP journey.

The newest build available for Hammerhead is M4B30Z, Android android-6.0.1_r77 Marshmallow. The guide you’re reading now was put together with Debian Stretch, which I explain how to install here.
If you followed that guide, everything explained in this guide will work perfectly.

I’ve already configured the environmental variables in the .bashrc file to work for the Debian Stretch system from the previous tutorial.
The .bashrc is available here https://github.com/andDevW/debiandroid/blob/master/CONFIGURATION_FILES/.bashrc.

First we need to establish a build environment.
Update APT and then use APT to install some packages

$ apt-get update
$ 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

Make sure that you set both Java and Javac to openjdk-7:

$ sudo update-alternatives --config java                                                                                                                                           
$ sudo update-alternatives --config javac

Next make a ~/bin directory and add it to your path.

$ mkdir ~/bin
$ PATH=~/bin:$PATH

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

Next go to the folder where you downloaded repo and run a check on the SHA-256 Checksum:

$ cd ~/bin
$ echo "e147f0392686c40cfd7d5e6f332c6ee74c4eab4d5 repo" | sha256sum -c

Initialize a Repo Client

	$ cd ~
	$ mkdir ~/aosp
	$ cd ~/aosp

	$ git config --global user.name "Your Name"
	$ git config --global user.email "you@example.com"

Verify that your identity is correct and tell repo whether or not you’d like color display in your account.

Head over to https://source.android.com/source/build-numbers and in Chromium hit Ctrl + F to open the Find Bar to search the page. In the Find Bar type Nexus 5 And hit Enter. Start at the top of the page and work down, being careful not to confuse the Nexus 5X with the Nexus 5. Determine the branch that is closest to the top and copy it. Now switch to the command line and paste in the branch that you just copied:

	$ android-6.0.1_r77

Make a note of the corresponding Build number in the first column: M4B30Z
Open a new tab in Chromium and navigate to Android’s Driver Binaries page https://developers.google.com/android/drivers#hammerhead

and in Chromium hit Ctrl + F

to open the Find Bar to search the page. In the Find Bar type M4B30Z And hit Enter. Scroll down to Nexus 5 (GSM/LTE) binaries for Android 6.0.1 (M4B30Z)

Switch back to the command line and open a new terminal In i3: hit Mod(Windows key) + Enter

Switch into the new terminal and Download each of the three proprietary binaries:
Broadcom, LG and Qualcomm:

cd ~/Downloads
mkdir M4B30Z_N5
cd M4B30Z_N5

wget https://dl.google.com/dl/android/aosp/broadcom-hammerhead-m4b30z-21cf8433.tgz  
wget https://dl.google.com/dl/android/aosp/lge-hammerhead-m4b30z-520d054d.tgz  
wget https://dl.google.com/dl/android/aosp/qcom-hammerhead-m4b30z-d6c0fe26.tgz  

When the downloads are finished it’s time to verify their corresponding checksums.

While still in the Downloads/M4B30Z_N5 folder, enter the following into your terminal:

$ ls -a

The proprietary binary package names should read:

.
..
broadcom-hammerhead-m4b30z-21cf8433.tgz
lge-hammerhead-m4b30z-520d054d.tgz
qcom-hammerhead-m4b30z-d6c0fe26.tgz

We need to veryify each of the files before we can use them to build the image.

$ echo '     ' | CHECKSUM_TYPE -c

Copy the SHA-256 Checksum for the Broadcom binary and paste it into the command line in between the two quotation marks. Notice that the CHECKSUM_TYPE is sha256sum, and enter sha256sum before the ‘-c’. It’s extremely important to make sure that there is no space between either of the end quotes:

$ echo ‘1bd8bc00f3f411262802744af5f63d047332a9212bc6c38b25400704b885103e’ sha256sum -c

Next, copy the Broadcomm binary’s filename:

https://dl.google.com/dl/android/aosp/broadcom-hammerhead-m4b30z-21cf8433.tgz

And paste it into the command line, immediately after the checksum, still in-between the two quote marks. The end result should look exactly like this:

$ echo ‘1bd8bc00f3f411262802744af5f63d047332a9212bc6c38b25400704b885103e broadcom-hammerhead-m4b30z-21cf8433.tgz’ sha256sum -c

Hit Enter, and if your file is intact you should see:

broadcom-hammerhead-m4b30z-21cf8433.tgz: OK

If you don’t see that message, retry the download and re-run the check.

Perform the same check for the remaining two binaries:

lge-hammerhead-m4b30z-520d054d.tgz  
$ echo '     ' | sha256sum -c
$ echo '294b2ffd81a0d0104eb942e60a5944948dc9a581e5adfc9f0f5b470843f11c63 ' | sha256sum -c
$ echo '294b2ffd81a0d0104eb942e60a5944948dc9a581e5adfc9f0f5b470843f11c63 lge-hammerhead-m4b30z-520d054d.tgz' | sha256sum -c

qcom-hammerhead-m4b30z-d6c0fe26.tgz
$ echo '     ' | sha256sum -c
$ echo 'f8c29461e279b311958f9476ef78b9ab654aeb9903f5c2912f11d5d4bcfd021d ' | sha256sum -c
$ echo 'f8c29461e279b311958f9476ef78b9ab654aeb9903f5c2912f11d5d4bcfd021d qcom-hammerhead-m4b30z-d6c0fe26.tgz' | sha256sum -c

When all three binaries are verified, copy this line:

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

and switch back to the command line where you have already entered the branch.
Paste the line you just copied in front of the branch you pasted before.

*If you lost track of your other terminal window, just cd into the ~/aosp folder

$ cd ~/aosp ** If you are ever unsure of which folder you are in, just enter the following command:

$ pwd This will print working directory,  which is your current location.

Paste this:

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

And then paste the branch behind it:

$  repo init -u https://android.googlesource.com/platform/manifest -b android-6.0.1_r77

It should look like this:

$ repo init -u https://android.googlesource.com/platform/manifest -b android-6.0.1_r77

Hit Enter.

Now pull down the Android source tree by running:

$ repo sync

Downloading everything should a good deal of time, so be patient.

When the work tree is done syncing, setup ccache.

$ cd ~/aosp
$ export USE_CCACHE=1
$ prebuilts/misc/linux-x86/ccache/ccache -M 100G

Or, if you need to use less space:

$ prebuilts/misc/linux-x86/ccache/ccache -M 50G

Now use the echo command to add a line to your .bashrc:

$ echo 'export USE_CCACHE=1' >> ~/.bashrc

Then reload your .bashrc file:

$  .  ~/.bashrc

Next, you need to uncompress each of the archives. While still in the root of ~/aosp:

$ tar -xvzf ~/Downloads/M4B30Z_N5/broadcom-hammerhead-m4b30z-21cf8433.tgz
$ tar -xvzf ~/Downloads/M4B30Z_N5/lge-hammerhead-m4b30z-520d054d.tgz
$ tar -xvzf ~/Downloads/M4B30Z_N5/qcom-hammerhead-m4b30z-d6c0fe26.tgz

Now run each of the self-extracting scripts, still in ~/aosp: For each of the scripts:

$  ./extract-broadcom-hammerhead.sh
$  ./extract-lge-hammerhead.sh
$  ./extract-qcom-hammerhead.sh

After running each of the scripts, press Enter once, and then hold the Space bar until you reach the confirmation: Type “I ACCEPT” if you agree to the terms of the license: At which point you must type “I ACCEPT” and then hit Enter. Done correctly, each will display:
Files extracted successfully.

Now clean up:

$ make clobber

Initialize the environment:

$ . build/envsetup.sh

Choose which target to build with lunch:

$ lunch aosp_hammerhead-userdebug

There are other options that can be built, namely the eng and user buildtypes, which can alternatively be built:

$ lunch aosp_hammerhead-eng
$ lunch aosp_hammerhead-user

Another method of selecting a buildtype is running lunch with no arguments:

$ lunch And then choosing a target from the menu.

Build the code with make: Pick a -jN argument that works best for you. I’m going with -j8 because that seems to work for me. If you’re using a powerful Intel CPU, do some research and figure out how many hardware threads you have available. Take that number of threads and multiply it by either 1 or 2, and use the

$ make -j8

Now wait. Building can take hours. When make is done, if everything works you’ll be presented with a success message telling you precisely how long the build took:

#### make completed successfully (02:06:05 (hh:mm:ss)) ####

FLASH NEXUS 5

Activate Developer options:
Under System Settings > About phone
Scroll to the bottom and hit ‘Build number’ around 7 times, opening Developer options.

Navigate back to Settings > Developer options
And at the top of the window flip the switch to the ON position.
Scroll down to Debugging, and turn USB debugging ON.

Plug your Nexus 5 into your PC with a USB cable.
In your Nexus notifications touch USB for charging
Select File transfers.
Now change directory to your AOSP directory root:

$ cd ~/aosp

Use ADB to confirm that your phone is connected properly:

$ sudo adb devices

In your Nexus 5 there should be a prompt ‘Allow USB debugging?’
Select ‘Always allow from this computer’ and then hit OK.

$ sudo adb devices
$ sudo adb reboot-bootloader
$ sudo fastboot oem unlock

Use your Nexus 5 volume buttons to select and highlight ‘Yes’
Now use your Nexus 5 power button to unlock the bootloader.

$ export ANDROID_PRODUCT_OUT=~/aosp/out/target/product/hammerhead/
$ export PATH=${PATH}:~/aosp/out/host/linux-x86/bin/

$  fastboot flashall -w  
$ fastboot oem lock

*In this case, with the Nexus 5 appearing to be at the end of its life, this might be the last branch released.
More info about guaranteed Android version updates is available from Google Support.