Build AOSP 8.1.0 on Nexus 6p - An Incomplete Guide

Although Google has posted a perfect tutorial on Building AOSP from the source, I decide to lay down my steps on building android on my Nexus 6p. These are a few unexpected bugs, errors and surprises as well.

  1. Establishing the environment
    1. 64-bit Ubuntu 14.04 LTS. You can also try other ubuntu distributions with required build tools, as well as MacOS.
    2. Installing JDK:
      1. openjdk-8-jre-headless_8u45-b14-1_amd64.deb
      2. openjdk-8-jre_8u45-b14-1_amd64.deb
      3. openjdk-8-jdk_8u45-b14-1_amd64.deb
      4. download the .deb packages since OpenJDK 8 is not natively supported on Ubuntu 14.04 LTS.
      5. sudo apt-get update
      6. sudo dkpg -i xxx.deb | run this command on each of the above downloaded packages
      7. if there is any missing dependency packages reported, run: sudo apt-get -f install
      8. Repeat and loop the above two steps to make sure OpenJDK is installed.
    3. Installing other required packages:
      1. 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 ccache libgl1-mesa-dev libxml2-utils xsltproc unzip
    4. Configuring USB access for your devices, in case your are not using an emulator
      1. plug your android in, go to System-About Phone-Build Number, tap it for seven times and you will see the hidden developer options in the parent menu, allow USB-debugging in developer options
      2. sudo apt-get install android-tools-adb
      3. sudo adb devices (allow this action on your phone to get efficient permission)
      4. change 51_android_rules if the above step does not work
    5. Using a separate output directory:
      1. add the following line in your bash profile to make sure this still applies after a reboot:
        1. export OUT_DIR_COMMON_BASE=<path-to-your-out-directory>
  2. Downloading the source
    1. Installing repo:
      1. Make sure you have a bin/ directory in your home directory and that it is included in your path:
        1. mkdir ~/bin
        2. PATH=~/bin:$PATH
      2. Download the Repo tool and ensure that it is executable:
        1. curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo 
        2. chmod a+x ~/bin/repo
           
      3. Initializing a Repo client
        1. Creating an working directory. This would be the directory with all source code and pre-built binaries. Name it carefully.
          1. mkdir WORKING+DIR && cd WORKING_DIR
        2. Configuring Git:
          1. git config --global user.name "xxx"
          2. git config --global user.email "xxx"
        3. repo init -u https://android.googlesource.com/platform/manifest
          1. Download the source. REMINDER: DON'T download master branch if you do not know your phone's build number. For example, I want to build the latest stable release of Nexus 6p. The most recent build number would be android-8.1.0_r18 on 03/26/2018:
            1. repo init -u https://android.googlesource.com/platform/manifest -b android-8.1.0_r18
        4. A successful initialization will end with a message stating that Repo is initialized in your working directory. Your client directory should now contain a .repo directory where files such as the manifest will be kept.
      4. Downloading the Android Source Tree by: repo sync
    2. Obtain proprietary binaries
      1. Although Android is free and open-source, there are some hardware-related proprietary libraries required. As for a Google/Nexus phone, you can easily find those binaries here with build number OPM5.171019.017 (android 8.1.0_r18).
      2. Unzip those binaries, move those two shell script into your working directory and run them. After signing the agreement you would be all set.
      3. run "make clobber" to ensure a clean source tree without other outputs
  3. Building AOSP from the scratch
    1. Set up the environment
      1. source build/envsetup.sh
      2. lunch
      3. choose a target, for my case, it would be 28. angler_user_debug
      4. make -j16, I am using a i7-7700 with 4 cores and 8 threads. Hence any argument between 16-32 should be fine for make
    2. Running builds on your devices
      1. make adb fastboot if you haven't had them installed before
      2. unlock your device if it is not already unlocked
        1. fastboot flashing unlock
      3. flash the built images!
        1. sudo adb reboot bootloader
        2. fastboot flashall -w
      4. So far, my Nexus 6p works just fine on unmodified AOSP build version but stuck at boot screen if source code is modified even slightly.
    3. In case something goes wrong and there should be a backup plan:
      1. Restoring devices to factory state
        1. Download the factory image here.
        2. Make sure your phone is connected by "fastboot devices"
        3. Unzip the image and run the ./flashall.sh inside the directory
  4. Other errors:
    1. Previously I was using a crappy laptop so most of the time buildings didn't go smoothly as it is now on a desktop. And below are some errors I hope you would not encounter.
    2. Jack server out of memory error, try increase heap size:
      1. export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
        ./prebuilts/sdk/tools/jack-admin kill-server
        ./prebuilts/sdk/tools/jack-admin start-server
    3. system image too large error: Try a swap file to increase memory space or change the settings in a make file to pre-install less Google service packages