A place for NopeNopeGuy's guides
Home |
---|
RBE for Android Using Buildbuddy |
Remote Build Execution (RBE) allows you to leverage powerful remote servers to significantly speed up your Android builds. While Google’s RBE service is commonly used, this guide focuses on using BuildBuddy, a flexible alternative. We’ll walk through setting up RBE with LineageOS as an example, but the principles apply to most AOSP-based ROMs.
Requirements:
Install the necessary build tools and an AUR helper (if you don’t have one already):
git clone https://aur.archlinux.org/yay-bin.git
cd yay-bin
makepkg -si
yay -S lineageos-devel
Initialize and sync the LineageOS source code (or your chosen ROM):
repo init -u https://github.com/LineageOS/android.git -b lineage-21.0 --git-lfs --depth=1
repo sync -j$(nproc --all) -c
Tip: See the “Tips and Tricks” section for ways to reduce storage usage. Using --depth=1
in the repo init
significantly reduces the initial download size.
Copy API Key: You’ll see a command similar to this:
build --bes_backend=grpcs://your-instance.buildbuddy.io
build --remote_header=x-buildbuddy-api-key=xxx ...
--remote_header=
. This is your API key. It will look like x-buildbuddy-api-key=xxx
.--bes_backend=
. You only need the part after grpcs://
, e.g., your-instance.buildbuddy.io
.reclient
reclient
package from this link. This is a pre-built version of Google’s reclient
tool.rbe
in the root of your AOSP directory and extract it there. It is important to keep the extracted directory structure.rbe
) or the absolute path.These environment variables configure reclient
to use BuildBuddy. It’s highly recommended to add these to your build/envsetup.sh
file so they are automatically set each time you initialize your build environment.
IMPORTANT SEE NOTES BEFORE YOU DO THIS
# --- Enable RBE and General Settings ---
export USE_RBE=1
export RBE_DIR="path/to/reclient" # Path to the extracted reclient directory (relative or absolute)
export NINJA_REMOTE_NUM_JOBS=72 # Number of parallel remote jobs (adjust based on your RAM, buildbuddy has 80 CPU cores in the free tier)
# --- BuildBuddy Connection Settings ---
export RBE_service="your-instance.buildbuddy.io:443" # BuildBuddy instance address (without grpcs://, add the port 443)
export RBE_remote_headers="x-buildbuddy-api-key=xxx" # Your BuildBuddy API key
export RBE_use_rpc_credentials=false
export RBE_service_no_auth=true
# --- Unified Downloads/Uploads (Recommended) ---
export RBE_use_unified_downloads=true
export RBE_use_unified_uploads=true
# --- Execution Strategies (remote_local_fallback is generally best) ---
export RBE_R8_EXEC_STRATEGY=remote_local_fallback
export RBE_D8_EXEC_STRATEGY=remote_local_fallback
export RBE_JAVAC_EXEC_STRATEGY=remote_local_fallback
export RBE_JAR_EXEC_STRATEGY=remote_local_fallback
export RBE_ZIP_EXEC_STRATEGY=remote_local_fallback
export RBE_TURBINE_EXEC_STRATEGY=remote_local_fallback
export RBE_SIGNAPK_EXEC_STRATEGY=remote_local_fallback
export RBE_CXX_EXEC_STRATEGY=remote_local_fallback # Important see below.
export RBE_CXX_LINKS_EXEC_STRATEGY=remote_local_fallback
export RBE_ABI_LINKER_EXEC_STRATEGY=remote_local_fallback
export RBE_ABI_DUMPER_EXEC_STRATEGY= # Will make build slower, by a lot. Keeping this for documentation
export RBE_CLANG_TIDY_EXEC_STRATEGY=remote_local_fallback
export RBE_METALAVA_EXEC_STRATEGY=remote_local_fallback
export RBE_LINT_EXEC_STRATEGY=remote_local_fallback
# --- Enable RBE for Specific Tools ---
export RBE_R8=1
export RBE_D8=1
export RBE_JAVAC=1
export RBE_JAR=1
export RBE_ZIP=1
export RBE_TURBINE=1
export RBE_SIGNAPK=1
export RBE_CXX_LINKS=1
export RBE_CXX=1
export RBE_ABI_LINKER=1
export RBE_ABI_DUMPER= # Will make build slower, by a lot. Keeping this for documentation
export RBE_CLANG_TIDY=1
export RBE_METALAVA=1
export RBE_LINT=1
# --- Resource Pools ---
export RBE_JAVA_POOL=default
export RBE_METALAVA_POOL=default
export RBE_LINT_POOL=default
USE_RBE=1
: Enables RBE.RBE_DIR
: The path to your extracted reclient
directory.RBE_service
: The BuildBuddy instance address (without grpcs://
, add the port 443).RBE_remote_headers
: Your BuildBuddy API key.*_EXEC_STRATEGY
: Controls how different build steps are handled. remote_local_fallback
means try remotely first, then fall back to local execution if the remote execution fails.RBE_*=1
: Enables RBE for specific build tools.NINJA_REMOTE_NUM_JOBS
: The number of parallel jobs to run remotely. Start with 72 and increase if you have more RAM. 128 should be safe for 16GB RAM systems. You can go higher (e.g., 500) if you have significantly more RAM.RBE_*_POOL
: Specifies the resource pool to use. The default
pool is usually sufficient.Important Notes:
RBE_CXX_LINKS_EXEC_STRATEGY
to local
after your first build is done to reduce build times.Once you’ve set up RBE, you can build your ROM as usual:
source build/envsetup.sh
breakfast your_device # Replace 'your_device' with your device codename
mka bacon -j$(nproc --all)
The mka
command is the recommended way to build. -j$(nproc --all)
uses all available CPU cores for the local build steps. RBE will handle the remote execution according to your configuration.
BTRFS compression can dramatically reduce the storage space needed for AOSP builds.
Create a BTRFS Filesystem:
sudo mkfs.btrfs /dev/your_storage_partition # Replace with your actual partition
Mount with Compression:
sudo mount /dev/your_storage_partition /mnt/aosp -o defaults,noatime,compress-force=zstd,space_cache=v2,commit=120
Replace /mnt/aosp
with your desired mount point.
fstab Entry (Optional): For persistent mounting, add a line to your /etc/fstab
:
/dev/your_storage_partition /mnt/aosp btrfs defaults,noatime,compress-force=zstd,space_cache=v2,commit=120 0 0
Benefits: This can reduce storage usage by around 50% (e.g., a 195GB build might only take up 101GB).
Example Compression Statistics:
Processed 1682984 files, 1779448 regular extents (1841256 refs), 975364 inline.
Type Perc Disk Usage Uncompressed Referenced
TOTAL 54% 101G 186G 195G
none 100% 48G 48G 51G
zstd 37% 52G 137G 143G
Note: You might be able to achieve even greater compression with dwarfs
, but this is more complex to set up.
AOSP builds typically require 32GB+ of RAM. You can use zram
(compressed RAM) to build on systems with less physical RAM (e.g., 8GB).
Load zram Module:
sudo modprobe zram
Configure zram:
sudo swapoff /dev/zram0 # Only if you have existing zram
sudo zramctl /dev/zram0 -s 48G # 48GB for 8GB RAM systems, 32GB for 16GB RAM
sudo mkswap /dev/zram0
sudo swapon /dev/zram0
Important: Using disk swap (a swap file or partition) is not recommended. It will be extremely slow. zram is significantly faster because it uses compressed RAM.
These results demonstrate the potential build time improvements with RBE.
Specs: AMD EPYC 9634 8-Core, 16GB RAM, KVM Server
System 1 Specifications (fastfetch output): AMD EPYC 9634 8-Core CPU, 16GB RAM, KVM Server, Debian GNU/Linux 12 (bookworm).
Build Time (R8, D8, CXX, JAVAC remote build and caching):
Image showing build time of approximately 2 hour 30 minutes with R8, D8, CXX, and JAVAC using remote build and caching.
Build Time (Everything cached except metalava):
System 1 Build Time (everything): 1 hour 27 minutes 43.496 seconds. RBE Stats: down 33.48 GB, up 14.86 GB, 90889 cache hits, 4289 remote executions, 121 local fallbacks.
Specs: Intel Core i5-6500T, 8GB RAM, 48GB ZRAM, BTRFS, CachyOS
System 2 Specifications (fastfetch output): Intel Core i5-6500T CPU, 8GB RAM, 48GB ZRAM, CachyOS x86_64, BTRFS filesystem.
If you encounter any issues, please contact @NopeNopeGuy on Telegram.