Build AOSP Lollipop 5.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 5.x 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 7 is needed to build Android 5.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-7-jdk
    java -version

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

    java version "1.7.0_65"
    OpenJDK Runtime Environment (IcedTea 2.5.3)
    OpenJDK 64-Bit Server VM (build 24.65-b04, 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 initialising 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-5.1.1_r30

    Note! The downloaded data is around 20 GB, and for a succesful 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. Create and open a file where you will later add Sony’s repos containing the device configurations for Xperia devices, by entering the following commands:
    mkdir .repo/local_manifests/
    nano .repo/local_manifests/sony.xml
  3. Add repos for Xperia devices by copying and pasting the following text into the file, and then save the file using Ctrl+X:
    <?xml version="1.0" encoding="UTF-8"?>
    <manifest>
    <remote name="sony" fetch="git://github.com/sonyxperiadev/" />
    <project path="device/qcom/sepolicy" name="device-qcom-sepolicy" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/amami" name="device-sony-amami" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/aries" name="device-sony-aries" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/castor" name="device-sony-castor" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/castor_windy" name="device-sony-castor_windy" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/common" name="device-sony-common" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/common-kernel" name="vendor-sony-kernel" groups="device" remote="sony" revision="aosp/LA.BF64.1.1_rb1.27" />
    <project path="device/sony/eagle" name="device-sony-eagle" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/flamingo" name="device-sony-flamingo" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/honami" name="device-sony-honami" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/ivy" name="device-sony-ivy" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/karin" name="device-sony-karin" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/karin_windy" name="device-sony-karin_windy" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/kitakami" name="device-sony-kitakami" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/leo" name="device-sony-leo" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/rhine" name="device-sony-rhine" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/scorpion" name="device-sony-scorpion" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/scorpion_windy" name="device-sony-scorpion_windy" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/seagull" name="device-sony-seagull" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/shinano" name="device-sony-shinano" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/sirius" name="device-sony-sirius" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/sumire" name="device-sony-sumire" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/suzuran" name="device-sony-suzuran" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/tianchi" name="device-sony-tianchi" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/togari" name="device-sony-togari" groups="device" remote="sony" revision="l-mr1" />
    <project path="device/sony/yukon" name="device-sony-yukon" groups="device" remote="sony" revision="l-mr1" />
    <project path="hardware/qcom/camera" name="camera" groups="device" remote="sony" revision="aosp/AU_LINUX_ANDROID_LA.BF64.1.1_RB1.05.00.02.172.061" />
    <project path="kernel/sony/msm" name="kernel" groups="device" remote="sony" revision="aosp/LA.BF64.1.1_rb1.27" />
    <project path="vendor/qcom/opensource/dataservices" name="vendor-qcom-opensource-dataservices" groups="device" remote="sony" revision="master" />
    <project path="vendor/sony-oss/thermanager" name="thermanager" groups="device" remote="sony" revision="master" />
    <project path="vendor/sony-oss/mkqcdtbootimg" name="mkqcdtbootimg" groups="device" remote="sony" revision="master" />
    <project path="vendor/sony-oss/macaddrsetup" name="macaddrsetup" groups="device" remote="sony" revision="master" />
    <project path="vendor/sony-oss/timekeep" name="timekeep" groups="device" remote="sony" revision="master" />
    </manifest>

    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. Find the software binaries for the device you want to use on Sony’s AOSP for Xperia devices
  5. In the root of your Android code tree, unzip the software binaries file you downloaded in step 4.
  6. You should now have a number of new directories in your vendor To make sure, enter the following command in a terminal window:
    ls vendor/sony

    This should give you the output:

    amami aries castor eagle flamingo honami ivy karin karin_windy kitakami leo rhine scorpion seagull shinano sirius sumire suzuran tianchi togari yukon
  7. To download the code into the device repos created above, run the command
    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:
    repo sync
    cd hardware/qcom/bt
    git cherry-pick 5a6037f1c8b5ff0cf263c9e63777444ba239a056
    cd ../audio
    git fetch https://android.googlesource.com/platform/hardware/qcom/audio refs/changes/41/166941/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/hardware/qcom/audio refs/changes/40/166940/1 && git cherry-pick FETCH_HEAD
    cd ../display
    git revert ab05b00fefd34a761dfaf1ccaf8ad14d325873f4
    cd ../../../external/libnfc-nci/
    git fetch https://android.googlesource.com/platform/external/libnfc-nci refs/changes/42/103142/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/external/libnfc-nci refs/changes/23/103123/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/external/libnfc-nci refs/changes/51/97051/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/external/libnfc-nci refs/changes/66/204666/3 && git cherry-pick FETCH_HEAD
    cd ../../external/libxml2
    git fetch https://android.googlesource.com/platform/external/libxml2 refs/changes/20/201520/1 && git cherry-pick FETCH_HEAD
    cd ../../hardware/libhardware/
    git fetch https://android.googlesource.com/platform/hardware/libhardware refs/changes/21/103221/2 && git cherry-pick FETCH_HEAD
    cd ../broadcom/libbt/
    git fetch https://android.googlesource.com/platform/hardware/broadcom/libbt refs/changes/13/154813/1 && git cherry-pick FETCH_HEAD

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.

Build AOSP Lollipop 5.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 4.4 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 7 is needed to build Android 5.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-7-jdk
    java -version

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

    java version "1.7.0_65"
    OpenJDK Runtime Environment (IcedTea 2.5.3)
    OpenJDK 64-Bit Server VM (build 24.65-b04, 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 git gnupg ccache lzop flex bison gperf build-essential zip curl zlib1g-dev zlib1g-dev:i386 libc6-dev lib32bz2-1.0 lib32ncurses5-dev x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 lib32z1-dev libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc libreadline6-dev lib32readline-gplv2-dev libncurses5-dev bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev lib32bz2-dev squashfs-tools pngcrush schedtool dpkg-dev
    sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so

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 initialising the AOSP code and Xperia device configurations. Learn how to do this below!

Initialise the AOSP tree (updated 22 December, 2014 for Android 5.0.2)
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-5.0.2_r3
    repo sync

    Note! The downloaded data is around 20 GB, and for a succesful 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. Create and open a file where you will later add Sony’s repos containing the device configurations for Xperia devices, by entering the following commands:
    mkdir .repo/local_manifests/
    nano .repo/local_manifests/sony.xml
  3. Add repos for Xperia devices by copying and pasting the following text into the file, and then save the file using Ctrl+X:
    <?xml version="1.0" encoding="UTF-8"?>
    <manifest>
    <remote name="sony" fetch="git://github.com/sonyxperiadev/" />
    <project path="device/sony/amami" name="device-sony-amami" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/aries" name="device-sony-aries" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/castor" name="device-sony-castor" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/eagle" name="device-sony-eagle" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/flamingo" name="device-sony-flamingo" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/honami" name="device-sony-honami" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/leo" name="device-sony-leo" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/rhine" name="device-sony-rhine" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/scorpion" name="device-sony-scorpion" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/seagull" name="device-sony-seagull" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/shinano" name="device-sony-shinano" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/sirius" name="device-sony-sirius" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/tianchi" name="device-sony-tianchi" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/togari" name="device-sony-togari" groups="device" remote="sony" revision="l-mr0" />
    <project path="device/sony/yukon" name="device-sony-yukon" groups="device" remote="sony" revision="l-mr0" />
    <project path="kernel/sony/msm" name="kernel" groups="device" remote="sony" revision="aosp/LNX.LA.3.5.2.2-03010-8x74.0" />
    <project path="vendor/sony/system/thermanager" name="thermanager" groups="device" remote="sony" revision="master" />
    <project path="vendor/sony/system/mkqcdtbootimg" name="mkqcdtbootimg" groups="device" remote="sony" revision="master" />
    <project path="vendor/sony/system/macaddrsetup" name="macaddrsetup" groups="device" remote="sony" revision="master" />
    <project path="vendor/sony/system/timekeep" name="timekeep" groups="device" remote="sony" revision="master" />
    </manifest>

    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. Find the software binaries for the device you want to use on Sony’s AOSP for Xperia devices
  5. In the root of your Android code tree, unzip the software binaries file you downloaded in step 4.
  6. You should now have a number of new directories in your vendor To make sure, enter the following command in a terminal window:
    ls vendor/sonyThis should give you the output:  amami aries castor eagle flamingo honami leo rhine scorpion seagull shinano sirius tianchi togari yukon
  7. To download the code into the device repos created above, run the command
    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:
    repo sync -cd -j32
    cd hardware/qcom/bt
    git cherry-pick 5a6037f1c8b5ff0cf263c9e63777444ba239a056
    cd ../display
    git revert 0fdae193307fb17bb537598ab62682edd5138b72
    git cherry-pick e9e1e3a16144a2410e592f67bab8e24c60df52ea
    cd ../../../external/libnfc-nci/
    git fetch https://android.googlesource.com/platform/external/libnfc-nci refs/changes/42/103142/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/external/libnfc-nci refs/changes/23/103123/1 && git cherry-pick FETCH_HEAD
    git fetch https://android.googlesource.com/platform/external/libnfc-nci refs/changes/51/97051/1 && git cherry-pick FETCH_HEAD
    cd ../../hardware/libhardware/
    git fetch https://android.googlesource.com/platform/hardware/libhardware refs/changes/21/103221/2 && git cherry-pick FETCH_HEAD

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 flash boot out/target/product/<device>/boot.img
    fastboot flash system out/target/product/<device>/system.img
    fastboot flash userdata out/target/product/<device>/userdata.img

    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.