This guide will asume you follow the official Sony build guide, but add some extra tips.
Initialize your build directory
cd into it and run the provided
repo init command (assuming you installed
repo init -u https://android.googlesource.com/platform/manifest \ -b android-9.0.0_r21 -g default,-x86,-mips,-darwin,-notdefault
(use the appropriate branch tag instead of
If you’re on another system like Fedora or Arch Linux, you can set up a Ubuntu chroot build environment instead.
A build chroot takes about 1-1.5GB of space and will save you from headaches, e.g. when Android modules aren’t compiled against your host glibc.
debootstrap, then run this (
bionic is the ubuntu version codename for
debootstrap --variant=buildd --arch=amd64 bionic ~/android/ubuntu-android
chroot into the newly created builder system, either via
chroot or just using
systemd-nspawn (Assuming you created your android build environment via
sudo systemd-nspawn \ -D /home/<your-username>/android/ubuntu-android useradd -m -s /bin/bash builder # -> No need to add a password, nspawn will log you in automatically
For Android Q, the way
.img files are mounted during build has changed, e.g.
resize2fs fails with a standard nspawn container.
ln -s /proc/self/mounts /etc/mtab
And add this to the options of systemd-nspawn:
--bind=/dev/loop0:/dev/loop0 \ --bind=/dev/loop1:/dev/loop1 \ --bind=/dev/loop2:/dev/loop2 \ --bind=/dev/loop3:/dev/loop3 \
(So that enough loop devices are available)
Install required packages
apt install software-properties-common python add-apt-repository universe apt update apt install bison g++-multilib git gperf libxml2-utils make \ zlib1g-dev zip liblz4-tool libncurses5 apt install openjdk-8-jdk # Optional: apt install ccache apt install rsync libssl-dev aapt # Kernel recompilation: apt install bc autoconf automake autopoint autotools-dev bsdmainutils gawk \ groff-base libarchive-zip-perl libpipeline1 libtimedate-perl libtool kmod # For selinux: apt install setools python-networkx policycoreutils # For JACK (it needs curl and lsof) apt install curl lsof
Cross-reference the needed packages with the official Sony build guide should this document be out of date.
If you’re building in a chroot:
Exit out of the build system by typing
exit (and pressing
Ctrl-] three times
nspawn), then re-launch your build system and log in as your builder user.
If you want to share your own ccache etc. with your build environment,
the appropriate directories.
sudo systemd-nspawn \ --bind=/home/<your-username>/android/build-env/:/home/builder/build-env/ \ --bind=/home/<your-username>/.ccache/:/home/builder/.ccache/ \ --bind=/home/<your-username>/.local/bin/repo:/usr/local/bin/repo \ --bind=/home/<your-username>/.repoconfig/:/home/builder/.repoconfig/ \ -D /home/<your-username>/android/ubuntu-android \ --user=builder
(Assuming you installed
Applying needed patches
Clone the Sony
local_manifests git repo as instructed and run
repo_update.sh. Your build directory will now be ready for a first build.
Prepare build environment
source build/envsetup.sh inside your build environment and then
Select your device from the list. It will look like
f83xx is the model number of your device. You can choose between
userdebug builds, see Choose a target.
For your own usage,
userdebug is most likely what you want.
user target doesn’t include root. You’ll need to look into
vendorsetup.sh for your device if you want to use it.
Optimize the build
A full build will take about two to three hours on a beefed-out recent
system (Core i7 8th gen 4 cores, 16GB RAM, good SSD). That time can however be
cut down for successive builds to around an hour by utilizing
parallelizing the build.
It is recommendable to use a ccache size of around 50-100GB, depending on whether you plan to build different ROMS or for multiple devices.
prebuilds/misc/linux-x86/ccache/ccache -M 50G
Then set the environment variable with
export USE_CCACHE=1. This variable
needs to be set anew every time you open a new terminal, so it is
advisable to put
export USE_CCACHE=1 at the end of your
.bashrc in your home
/home/builder/.bashrc if you’re in a chroot).
ccache compression might also save a lot of disk space for you. Set
export CCACHE_COMPRESS=1 in your
To use parallel compilation, you need to find out how many threads your CPU has
nproc. Then use that number for
make -j<nr-of-threads>. For most
computers it will be the number of CPU cores times 2, e.g. “8” for a 4-core CPU.
make -j <nr-of-threads>. If everything went smoothly, you’ll have a folder
system.img. You can then flash these files onto your device with
If you want to share the fruits of your labor, you can create compressed
flashable .zip files. Use
make otapackage instead of
make. This will create
a flashable zip file in
out/target/product/<device-codename> named something
aosp_f8331-ota-eng.hostname-2018-10-19. You can flash this file via
sideload or use a custom recovery like TWRP.
After you have tested your build on your own device, you can share the file freely, given that it contains free/libre software under the GPL and other free licenses. The license also means you have to divulge your build sources. So put your patches into a git repo and share them with the world.
m module.name -j $(nproc)to only rebuild a single module. You can then
adb pushor even
adb syncthe changes directly to your device
make bootimageto re-generate kernel and ramdisk(