Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Build AOSP Android Pie on Sony Xperia Phones

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Building AOSP Android Pie on Sony Xperia phones gives you a clean, stock Android system directly from source, free from manufacturer skins or carrier bloatware. Sony provides official AOSP support and unlockable bootloaders for many Xperia devices, making them ideal for developers and enthusiasts. The process requires setting up a Linux build environment, downloading roughly 60GB of source code, and compiling the system before flashing it via fastboot.

What You'll Need

  • A supported Sony Xperia device (such as Xperia XZ, XZ1, XZ2 series, or XA2).
  • A computer running a 64-bit Linux distribution (Ubuntu 18.04 or 20.04 are standard for Android Pie builds).
  • At least 16GB of RAM; 32GB or more is recommended to prevent compilation errors.
  • Approximately 150GB of free disk space for the source code, build cache, and output.
  • A reliable internet connection for downloading source repositories.
  • A USB cable compatible with your Xperia device.
  • The Sony Device Specific Binaries (proprietary blobs) for your exact model.

Step-by-Step

  1. Unlock the Bootloader. Go to the Sony Developer World website and use the IMEI number of your device to request a bootloader unlock key. Enable OEM unlocking in Developer Options on the phone, then boot to fastboot mode (hold Volume Up while connecting the USB cable). Run fastboot oem unlock YOUR_KEY using the code provided by Sony. This wipes all user data on the device.
  2. Install Build Dependencies. On your Linux machine, open a terminal and install the required packages using the package manager. For Ubuntu, this includes openjdk-8-jdk, git, python, and various build tools like make and gcc. Android Pie specifically requires Java 8.
  3. Install Repo Tool. Download the Google Repo tool, which manages the multiple Git repositories used in AOSP. Ensure it is executable and placed in a directory listed in your system PATH, such as /usr/bin or ~/bin.
  4. Initialize the Source Repository. Create a blank directory for the source code. Navigate into it and run the repo init command pointing to the AOSP manifest for Android Pie (branch android-9.0.0_rxx). Sony maintains specific manifests for their devices which you may need to reference from Sony Developer World.
  5. Download the Source Code. Run repo sync to download the source trees. This process downloads several gigabytes of data and can take over an hour depending on your connection speed.
  6. Download Device Binaries. Visit the Sony Developer World AOSP binaries page. Download the proprietary binaries for your specific Xperia model that correspond to Android Pie. Extract these files into the root of your source directory and execute the setup scripts to place the blobs in the vendor tree.
  7. Configure the Build Environment. In the source root, run source build/envsetup.sh. This sets up environment variables and shell functions required for the build process.
  8. Select the Target Device. Run lunch and select the target that matches your Xperia device codename (for example, aosp_kugo-userdebug). Ensure you select a userdebug or eng build for development purposes.
  9. Start the Build. Run make -jN, where N is the number of threads you want to use. A common formula is twice the number of CPU cores you have. The compilation can take several hours.
  10. Flash the System. Once the build completes, locate the generated .img files in the out/target/product/[codename]/ directory. Boot your Xperia into fastboot mode and flash the images. The core command is fastboot flash system system.img. You may also need to flash boot.img, userdata.img, and vendor.img depending on the specific device requirements. Reboot the device when finished.

Common Mistakes

Developers often fail to extract the proprietary binaries correctly, resulting in a build that compiles but lacks hardware functionality like Wi-Fi or audio. Always verify the binary version matches the AOSP branch. Another frequent error is using the wrong Java version; Android Pie builds strictly require OpenJDK 8, and using Java 11 or 14 will cause build failures. Additionally, attempting to build with insufficient RAM often leads to obscure compilation crashes or the process being killed by the operating system.

Tips

Use the ccache tool to speed up subsequent builds by caching previous compilations. Set the maximum cache size to roughly 50GB. If you are only making small changes, you can use make bootimage to compile just the kernel, which is much faster than a full system build. Always check the Sony Developer World forums for the latest device-specific manifest branches, as AOSP manifests can change.

Troubleshooting

If the build fails with Java-related errors, verify that java -version reports 1.8.0. If the build crashes with a signal 11 or out-of-memory error, reduce the number of parallel jobs (the -j flag) or add swap space to your system. If the device boots but specific hardware features like the camera or GPS do not work, re-download the proprietary binaries and ensure they are placed in the correct vendor directory structure before building again.

Frequently Asked Questions

Does this work on carrier-locked Xperia phones?

No. The bootloader must be unlocked to flash a custom system image, and carrier-locked devices usually prevent OEM unlocking.

Will building AOSP void my warranty?

Yes. Unlocking the bootloader and flashing unofficial software generally voids the manufacturer warranty.

Can I use Windows Subsystem for Linux (WSL) to build?

It is not recommended. WSL 1 has filesystem issues that break the build process, and WSL 2 has USB passthrough limitations that make flashing difficult.

Do I need to root the phone first?

No. An unlocked bootloader is sufficient to flash the system images. Root access is granted by the userdebug build type.

How long does the compilation take?

On a modern high-end desktop with 32GB of RAM and an SSD, a full build takes roughly 1 to 3 hours. Older machines may take significantly longer.