[Sony Xperia XZs Guides, News, & Discussion] amirzaim:
WARNING: This guide is only for experienced users only. I will not responsible for any damages that you had been done during modifying your android device!

Prerequisite before setup for custom ROM building:

1. An unlocked Xperia device, which have its support for Pie ROM. See the list of device here

2. Ubuntu 18.04 OS (either on actual installation on HDD or installed on virtualbox only)

3. A powerful PC/laptop

4. 100Mbps internet connection

5. A brain

NOTE: If your device is not unlocked yet, please follow the official guide given by Sony here.

Steps for building the custom ROM:

1. Install OpenJDK JDK version

sudo apt-get install openjdk-8-jdk

2. Install all of these stuffs (you might need to copy these into text editor and save it as script file:

sudo apt install -y bc build-essential ccache curl g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev liblz4-tool libncurses5-dev libsdl1.2-dev libssl-dev libwxgtk3.0-dev libxml2 libxml2-utils lzop m4 openjdk-8-jdk pngcrush repo rsync schedtool squashfs-tools xsltproc zip zlib1g-dev bison gperf libxml2-utils make zlib1g-dev:i386 liblz4-tool libncurses5 lunch unzip clang

3. Download repo tool (you might be prompted to set up git account access when running curl command):

mkdir ~/bin
curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

4. Open up .bashrc file:

nano ~/.bashrc

5. Put these line at the end of the file and save:

export PATH=~/bin:$PATH
export LC_ALL=C
export USE_CCACHE=1

Then, configure cache limit to 20GB (you have to declare very specific because running ccache -M 20G only straight from the terminal won't work)

prebuilts/misc/linux-x86/ccache/ccache -M 20G

6. Reload bash variables:

source ~/.bashrc

7. Initialize repo tree:

mkdir ~/android
cd ~/android
repo init -u https://android.googlesource.com/platform/manifest -b android-9.0.0_r35

8. Clone the local manifests from GitHub:

cd .repo
git clone https://github.com/sonyxperiadev/local_manifests
cd local_manifests
git checkout android-9.0.0_r35
cd ../..

9. Downloading the 30-40GB of source codes!:

repo sync

10. Cherry-pick the upstream patches:

./repo_update.sh

11. Initializing device type for ROM building:

source build/envsetup.sh && lunch

or

source build/envsetup.sh && lunch aosp_[device_model]-userdebug

12. Building the custom ROM ( it will take at about 4-5 hours):

make -j[number of CPU cores]

13. Flashing your AOSP images (ROM images can be found at android/out/target/product/<device>/ directory):

fastboot erase cache
fastboot erase userdata
fastboot flash boot boot.img
fastboot flash system system.img
fastboot flash cache cache.img
fastboot flash userdata userdata.img

14. Flashing OEM images (Download the vendor image corresponding to your device here):

fastboot flash oem SW_binaries_for_Xperia_AOSP_O_MR0_4.4_<release version>_<platform>.img

15. Reboot your device:

fastboot reboot

If the device able to boot, then congratulations!...

Guide reference: https://developer.sony.com/develop/...build-instructions/build-aosp-android-p-9-0-0

ADDITIONAL NOTES:

If you're want Google apps installed, follow these steps below:

1. Download OpenGapps from the website here and put the opengapps zip file into microsd card or USB OTG drive.

2. Download Magisk zip file here and put it into microsd card or USB OTG drive.

3. Download the TWRP image here according to your device and flash the recovery or just boot it directly

fastboot flash recovery TWRP.img

or

fastboot boot TWRP.img

4. Go to Install and select microSD or USB OTG storage and install opengapps package first.

5. Then, install Magisk package for patching in order to enable Google apps functionalities on AOSP ROM.

6. Wipe cache and reboot the device.