Build AOSP Nougat 7.1

This guide assumes you run Ubuntu (we used Ubuntu 14.04 LTS), but it should work in a similar way on any Linux-based system. To use these instructions, you should be familiar with Android development. Follow the steps below to build Android Nougat on an unlocked Xperia device. The instructions will guide you through each step from how to prepare your environment and install all necessary tools to downloading and configuring the code, before you can finally build AOSP images and flash them on your device.

Prepare your Java environment
To prevent errors generated by having the wrong version of Java installed on your computer, we will start by removing any conflicting Java version and adding the correct version. Java 8 is needed to build Android 7.x.x.

  1. On your computer: In a terminal window, enter the following command:
    sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*
  2. A guide will appear on the screen. Follow the instructions to remove Java.
  3. Once Java is removed, install the correct version of Java by entering the following commands in a terminal window:
    sudo apt-get update
    sudo apt-get install openjdk-8-jdk
    java -version

    You should now see something similar to the following in your terminal window:

    java version "1.8.0_91"
    OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~14.04-b14)
    OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

Install the necessary tools to make an Android build
To be able to build the images that you will later flash on your device, you need to install a set of software packages and libraries which give you the tools to compile source code into binary files that can run on your device.

  1. In a terminal window, enter the following commands, all at once:
    sudo apt-get install bison g++-multilib git gperf libxml2-utils make zlib1g-dev:i386 zip

Now you have the tools you need to compile and build a flashable AOSP image. Follow the steps below to learn how to do this.

Download Repo tool and set PATH
In order to access and use the source code available on the Sonyxperiadev GitHub, you need to install the Repo tool that is provided by Google.

  1. In a terminal window, enter the following commands to download and install Repo, and set the right access rules for it:
    mkdir ~/bin
    curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    chmod a+x ~/bin/repo
  2. Open the bashrc file included in the repo tool:
    sudo nano ~/.bashrc
  3. To set the right path for your local bin folder, paste the following code to a new line at the very bottom of the bashrc file, and then save the file using Ctrl+X:
    export PATH=~/bin:$PATH
  4. Reload bash variables to include the new path:
    source ~/.bashrc

Now you have all the tools you need and can move on to initializing the AOSP code and Xperia device configurations. Learn how to do this below!

Initialise the AOSP tree
The next step is to create a folder on your computer, and then download the Android source code to it. Follow the steps below to do this.

  1. In a terminal window, enter the following commands:
    mkdir ~/android
    cd ~/android
    repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.1_r55

    Note! The downloaded data is around 20 GB, and for a successful build you need 100 GB free hard disk space. Depending on your Internet connection, it can take quite a long time to download the source code. When the download has finished you have the basic AOSP source code on your computer.

  2. Check on List of devices and resources the supported kernel versions for your device.
  3. Depending on the kernel version clone the local_manifests from GitHub using the following commands:
    Kernel 4.4

    cd .repo
    git clone https://github.com/sonyxperiadev/local_manifests
    cd local_manifests
    git checkout n-mr1_4.4
    

    Kernel 3.10

    cd .repo
    git clone https://github.com/sonyxperiadev/local_manifests
    cd local_manifests
    git checkout n-mr1_3.10
    cd ../..
    

    Note! These repos include a precompiled kernel. If you want to change or improve the kernel, you can recompile a new kernel. The new kernel binary should be placed in the corresponding device repository along with new kernel modules for wireless hardware. For more information, see the page how to rebuild kernels for flagship Xperia devices.

  4. To download the code into the device repos created above, run the command:
  5. repo sync
    Cherry-pick the upstream patches
    ./repo_update.sh
    

    Next two steps are only for the following devices:

    • Xperia Tablet Z
    • Xperia ZL
    • Xperia Z
    • Xperia Z Ultra
    • Xperia Z1 Compact
    • Xperia Z2 Tablet
    • Xperia Z2
    • Xperia Z3 Tablet Compact
    • Xperia Z3 Compact
    • Xperia Z3
    • Xperia Z4 Tablet WiFi
    • Xperia Z4 Tablet
    • Xperia Z3+
    • Xperia Z5 Compact
    • Xperia Z5
    • Xperia Z5 Premium

  6. Find and download the software binaries for the device you want to use on Sony’s AOSP for Xperia devices
  7. In the root of your Android code tree, unzip the software binaries file you downloaded.

Build AOSP images that can be flashed
Now you are ready to build AOSP images that can be flashed to a device. To do this, you have to point out your specific device and then start building the images.

  1. Enter the following commands:
    source build/envsetup.sh && lunch
  2. When prompted, pick the number corresponding to your device in the list displayed and press enter.
  3. To start the build, type:
    make –j <insert the cpu thread number of your computer>

    This step will take a long time. It may take up to several hours, so go grab a coffee or play a game while you wait. When it’s done, AOSP images that can be flashed to a device will be ready.

Flash AOSP image to your device
Before you flash the images that you built in the previous step, you must make sure your device is unlocked through Sony’s unlock boot loader service. Then you can flash the AOSP images on your device by following these steps:

  1. On your device: Connect the device to your computer in Fastboot mode, by pressing volume up while inserting the USB cable. When the device is in Fastboot mode, the LED on the device will be illuminated in blue.
  2. On your computer: Flash the boot, system and userdata images by entering the following commands in a terminal window:
    fastboot –S 256M flash boot out/target/product/<device>/boot.img
    fastboot –S 256M flash system out/target/product/<device>/system.img
    fastboot –S 256M flash userdata out/target/product/<device>/userdata.img

    Note!It’s not necessary to re-flash the userdata every time you flash your device, but sometimes the new software is incompatible with previous content, which might result in a device that doesn’t boot. If you experience this, try to re-flash only the userdata again.

  3. Now when you disconnect your device from the computer and start it, it will be running AOSP.

Flash vendor image to your device
Vendor image is supported by the following devices:

  • Xperia X
  • Xperia X Compact
  • Xperia X Performance
  • Xperia XZ Premium
  • Xperia XZ1
  • Xperia XZ1 Compact
  • Xperia XZ
  • Xperia XZs

  1. Download the vendor image corresponding your device from List of devices and resources.
  2. Unpack the vendor zip file to obtain the vendor image.
    unzip SW_binaries_for_Xperia_AOSP_N_MR1_<kernel version>_<release version>_<platform>.zip

    Note: Kernel version must match the version used when code was initialized. Release version should be equivalent to the latest downloaded image version, and platform should match your device.

  3. On your device: Connect the device to your computer in Fastboot mode, by pressing volume up while inserting the USB cable. When the device is in Fastboot mode, the LED on the device will be illuminated in blue.
  4. On your computer: Flash the vendor image by entering the following commands in a terminal window:
    fastboot flash oem SW_binaries_for_Xperia_AOSP_N_MR1_<kernel version>_<release version>_<platform>.img

    Note: Kernel version must match the version used when code was initialized. Release version should be equivalent to the latest downloaded image version, and platform should match your device.

We hope this tutorial will help you get started building your own version of Android for Xperia devices. Feel free to contribute your work back to our repositories on our Sonyxperiadev GitHub account. We will review and merge your code as soon as possible.

Build AOSP Nougat 7.0

This guide assumes you run Ubuntu (we used Ubuntu 14.04 LTS), but it should work in a similar way on any Linux-based system. To use these instructions, you should be familiar with Android development. Follow the steps below to build Android Nougat on an unlocked Xperia device. The instructions will guide you through each step from how to prepare your environment and install all necessary tools to downloading and configuring the code, before you can finally build AOSP images and flash them on your device.

Prepare your Java environment
To prevent errors generated by having the wrong version of Java installed on your computer, we will start by removing any conflicting Java version and adding the correct version. Java 8 is needed to build Android 7.x.x.

  1. On your computer: In a terminal window, enter the following command:
    sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*
  2. A guide will appear on the screen. Follow the instructions to remove Java.
  3. Once Java is removed, install the correct version of Java by entering the following commands in a terminal window:
    sudo apt-get update
    sudo apt-get install openjdk-8-jdk
    java -version

    You should now see something similar to the following in your terminal window:

    java version "1.8.0_91"
    OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~14.04-b14)
    OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

Install the necessary tools to make an Android build
To be able to build the images that you will later flash on your device, you need to install a set of software packages and libraries which give you the tools to compile source code into binary files that can run on your device.

  1. In a terminal window, enter the following commands, all at once:
    sudo apt-get install bison g++-multilib git gperf libxml2-utils make zlib1g-dev:i386 zip

Now you have the tools you need to compile and build a flashable AOSP image. Follow the steps below to learn how to do this.

Download Repo tool and set PATH
In order to access and use the source code available on the Sonyxperiadev GitHub, you need to install the Repo tool that is provided by Google.

  1. In a terminal window, enter the following commands to download and install Repo, and set the right access rules for it:
    mkdir ~/bin
    curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    chmod a+x ~/bin/repo
  2. Open the bashrc file included in the repo tool:
    sudo nano ~/.bashrc
  3. To set the right path for your local bin folder, paste the following code to a new line at the very bottom of the bashrc file, and then save the file using Ctrl+X:
    export PATH=~/bin:$PATH
  4. Reload bash variables to include the new path:
    source ~/.bashrc

Now you have all the tools you need and can move on to initializing the AOSP code and Xperia device configurations. Learn how to do this below!

Initialise the AOSP tree
The next step is to create a folder on your computer, and then download the Android source code to it. Follow the steps below to do this.

  1. In a terminal window, enter the following commands:
    mkdir ~/android
    cd ~/android
    repo init -u https://android.googlesource.com/platform/manifest -b android-7.0.0_r34

    Note! The downloaded data is around 20 GB, and for a successful build you need 100 GB free hard disk space. Depending on your Internet connection, it can take quite a long time to download the source code. When the download has finished you have the basic AOSP source code on your computer.

  2. Clone the local_manifests from GitHub using the following commands:
    cd .repo
    git clone https://github.com/sonyxperiadev/local_manifests
    cd local_manifests
    git checkout n-mr0
    cd ../..
    

    Note! These repos include a precompiled kernel. If you want to change or improve the kernel, you can recompile a new kernel. The new kernel binary should be placed in the corresponding device repository along with new kernel modules for wireless hardware. For more information, see the page how to rebuild kernels for flagship Xperia devices.

  3. Find and download the software binaries for the device you want to use on Sony’s AOSP for Xperia devices
  4. In the root of your Android code tree, unzip the software binaries file you downloaded.
  5. To download the code into the device repos created above, run the command:
  6. repo sync
    

Add necessary patches from the AOSP upstream branch
In order for certain functions to work on the hardware in Xperia devices, you need to add a set of patches from the AOSP master branch. This is done by executing the following commands:

  1. In a terminal window, enter:
    cd external/toybox
    git fetch https://android.googlesource.com/platform/external/toybox refs/changes/74/265074/1 && git cherry-pick FETCH_HEAD
    git cherry-pick d3e8dd1bf56afc2277960472a46907d419e4b3da
    git cherry-pick 1c028ca33dc059a9d8f18daafcd77b5950268f41
    git cherry-pick cb49c305e3c78179b19d6f174ae73309544292b8
    cd ../../hardware/qcom/audio
    git cherry-pick 1d2e4743ffcbd27ed2e96c1c8ac1e186a717fa45
    git cherry-pick a8d7c9257c3c9514f5c35d3dbd987703e12c82cd
    git cherry-pick 6b4b127cad4dce4db30c5119d8fd6bbc0af3924c
    git cherry-pick 9ebf582bd7f0b701f969081646d27c4a77347402
    git cherry-pick 536daa591913c5eaf3fea47c90a5cf4bc24ce321
    git cherry-pick 152b09a14f3b2c17063468b3f5b5ddda195802ec
    git cherry-pick 6ebefdd83d29132f6ca5a5d6b45cac71871dd037
    git cherry-pick 56631b51985194269339dbf53316f414b3d21050
    git cherry-pick 2d809e0d104497ca6b7a1e1be4efdf7856070aa5
    git fetch https://android.googlesource.com/platform/hardware/qcom/audio refs/changes/35/274235/6 && git cherry-pick FETCH_HEAD
    cd ../bt
    git revert --no-edit c7dc913784965e4ce705c2045f0a8b43fcd1db1c
    cd ../display
    git revert --no-edit b7d1a389b00370fc9d2a7db1268ce26271ead7e2
    git revert --no-edit f026d04dde743a0524235ae57e2ce8ac5364d44b
    git revert --no-edit 3261eb2236252f9f2510c008fad451411a780b3b
    git fetch https://android.googlesource.com/platform/hardware/qcom/display refs/changes/72/265072/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/hardware/qcom/display refs/changes/73/265073/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/hardware/qcom/display refs/changes/54/274454/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/hardware/qcom/display refs/changes/55/274455/1 && git cherry-pick FETCH_HEAD
    cd ../gps
    git revert --no-edit 53bf15aab71461f81e27e6f5176afcd1a29af7d4
    git revert --no-edit 486ab751599b7f8b5a2f2711d22867ad54fdc79b
    cd ../media
    git revert --no-edit 9e8b76d32ece15e79ebf4b02ede869d89807eec6
    cd ../keymaster
    git revert --no-edit 583ecf5ed2a4be0d05229b8c6726680c3836be8b
    git fetch https://android.googlesource.com/platform/hardware/qcom/keymaster refs/changes/70/212570/5 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/hardware/qcom/keymaster refs/changes/80/212580/2 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/hardware/qcom/keymaster refs/changes/61/213261/1 && git cherry-pick FETCH_HEAD
    cd ../../../system/core
    git fetch https://android.googlesource.com/platform/system/core refs/changes/52/269652/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/system/core refs/changes/58/327458/1 && git cherry-pick FETCH_HEAD
    cd ../../packages/apps/Nfc
    git revert --no-edit 988c3fff5470a1de3a880bd07fa438cc47e283c8
    cd ../Music
    git cherry-pick 6036ce6127022880a3d9c99bd15db4c968f3e6a3
    cd ../../../frameworks/av
    git fetch https://android.googlesource.com/platform/frameworks/av refs/changes/71/316171/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/frameworks/av refs/changes/72/316172/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/frameworks/av refs/changes/73/316173/1 && git cherry-pick FETCH_HEAD
    cd ../..
    

Note! This information will be updated continuously for new Android versions and new devices, so you should always check the AOSP section on Developer World for the latest patches.

Build AOSP images that can be flashed
Now you are ready to build AOSP images that can be flashed to a device. To do this, you have to point out your specific device and then start building the images.

  1. Enter the following commands:
    source build/envsetup.sh && lunch
  2. When prompted, pick the number corresponding to your device in the list displayed and press enter.
  3. To start the build, type:
    make –j <insert the cpu thread number of your computer>

    This step will take a long time. It may take up to several hours, so go grab a coffee or play a game while you wait. When it’s done, AOSP images that can be flashed to a device will be ready.

Flash AOSP image to your device
Before you flash the images that you built in the previous step, you must make sure your device is unlocked through Sony’s unlock boot loader service. Then you can flash the AOSP images on your device by following these steps:

  1. On your device: Connect the device to your computer in Fastboot mode, by pressing volume up while inserting the USB cable. When the device is in Fastboot mode, the LED on the device will be illuminated in blue.
  2. On your computer: Flash the boot, system and userdata images by entering the following commands in a terminal window:
    fastboot –S 256M flash boot out/target/product/<device>/boot.img
    fastboot –S 256M flash system out/target/product/<device>/system.img
    fastboot –S 256M flash userdata out/target/product/<device>/userdata.img

    Note!It’s not necessary to re-flash the userdata every time you flash your device, but sometimes the new software is incompatible with previous content, which might result in a device that doesn’t boot. If you experience this, try to re-flash only the userdata again.

  3. Now when you disconnect your device from the computer and start it, it will be running AOSP.

We hope this tutorial will help you get started building your own version of Android for Xperia devices. Feel free to contribute your work back to our repositories on our Sonyxperiadev GitHub account. We will review and merge your code as soon as possible.