Building AOSP 16 on Google Cloud Platform
Guide to downloading and building AOSP 16 on Google Cloud. Covers repo sync and scaling up the VM for faster compilation.
This post continues from the previous guide where we set up an Ubuntu 22.04 VM on Google Cloud Platform with the necessary tools and Nested Virtualization enabled. Now, we will proceed to download the Android 16 source code and build the Cuttlefish virtual device target.
Outline
Prerequisites: Start the VM
If you shut down your VM after the previous setup, you’ll need to restart it and connect via SSH.
# Check the status of your instances
gcloud compute instances list
# NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
# aosp1 asia-south1-a n2-standard-2 10.160.0.2 34.180.7.120 TERMINATED
# If the status is TERMINATED, start the instance
gcloud compute instances start aosp1 --zone asia-south1-a
# Verify it is RUNNING and get the external IP
➜ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
aosp1 asia-south1-a n2-standard-2 10.160.0.2 34.180.7.120 RUNNING
Once you have the external IP, connect using the SSH key we configured:
ssh -i aosp1-gcp <external-ip>
Step 1: Configure Git Identity
Before interacting with the Android repositories, we need to configure our git user identity. This
is required by the repo tool.
conrad@aosp1:~$ git config --global user.name "xxxxxx xxxxx"
conrad@aosp1:~$ git config --global user.email "xxxxxx@gmail.com"
conrad@aosp1:~$ git config --global user.email
xxxxxx@gmail.com
conrad@aosp1:~$ git config --global user.name
xxxxxx xxxxx
Step 2: Initialize the AOSP Repository
We will create a directory for the source code and initialize the repository using the repo tool.
We are following the Opersys Embedded Android Exercises, which targets android-16.0.0_r2.
conrad@aosp1:~$ mkdir aosp-16
conrad@aosp1:~$ cd aosp-16/
conrad@aosp1:~/aosp-16$ repo init -u https://android.googlesource.com/platform/manifest \
-b android-16.0.0_r2
Downloading Repo source from https://gerrit.googlesource.com/git-repo
repo: Updating release signing keys to keyset ver 2.3
Your identity is: xxxxxx xxxxx <xxxxxx@gmail.com>
If you want to change this, please re-run 'repo init' with --config-name
Testing colorized output (for 'repo diff', 'repo status'):
black red green yellow blue magenta cyan white
bold dim ul reverse
Enable color display in this user account (y/N)? y
repo has been initialized in /home/conrad/aosp-16
conrad@aosp1:~/aosp-16$
Step 3: Sync the Source Code
Now we download the source code. This process can take an hour or more depending on your connection speed, as it fetches approximately 250GB of data.
repo sync -c -j$(nproc)
-c: Fetch only the current branch to save space.-j$(nproc): Use all available CPU cores for parallel fetching.
Step 4: Scale Up the VM for Building
The n2-standard-2 instance is great for downloading code but too weak to build AOSP in a
reasonable time. Building on it could take 10+ hours or fail due to memory exhaustion. We will
resize the instance to a more powerful machine type just for the build phase.
Recommended: n2-highmem-16 (16 vCPUs, 128GB RAM) or n2-highmem-32 (32 vCPUs, 256GB RAM).
-
Stop the VM:
➜ gcloud compute instances stop aosp1 --zone asia-south1-a Stopping instance(s) aosp1...done. Updated [https://compute.googleapis.com/compute/v1/projects/aosp-build-480610/zones/asia-south1-a/instances/aosp1]. -
Resize the VM:
➜ gcloud compute instances set-machine-type aosp1 --machine-type=n2-highmem-16 \ --zone asia-south1-a Updated [https://www.googleapis.com/compute/v1/projects/aosp-build-480610/zones/asia-south1-a/instances/aosp1]. -
Start the VM:
➜ gcloud compute instances start aosp1 --zone asia-south1-a Starting instance(s) aosp1...done. Updated [https://compute.googleapis.com/compute/v1/projects/aosp-build-480610/zones/asia-south1-a/instances/aosp1]. Instance internal IP is 10.160.0.2 Instance external IP is 34.100.182.186 -
Verify the new configuration:
➜ gcloud compute instances list NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS aosp1 asia-south1-a n2-highmem-16 10.160.0.2 34.100.182.186 RUNNING
Once the VM is running, SSH back into it and verify the resources:
➜ ssh -i aosp1-gcp 34.100.182.186
...
conrad@aosp1:~/aosp-16$ nproc
16
conrad@aosp1:~/aosp-16$ free -h
total used free shared buff/cache available
Mem: 125Gi 664Mi 122Gi 0.0Ki 2.3Gi 123Gi
Swap: 0B 0B 0B
Step 5: Start a Persistent Session (tmux)
Since the build takes a long time (1.5 - 2 hours), it is critical to use a persistent session tool
like tmux. This ensures that if your SSH connection drops, the build continues running.
conrad@aosp1:~/aosp-16$ tmux new -s aosp_build
- To detach from the session (and keep it running in the background), press
Ctrl+bthend. - To reattach later, run:
tmux attach -t aosp_build.
Step 6: Configure the Build Environment
Set up the environment variables and select the Cuttlefish target for x86_64. We use the target specified in the exercises:
conrad@aosp1:~/aosp-16$ source build/envsetup.sh
conrad@aosp1:~/aosp-16$ lunch aosp_cf_x86_64_only_phone-aosp_current-eng
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=16
TARGET_PRODUCT=aosp_cf_x86_64_only_phone
TARGET_BUILD_VARIANT=eng
TARGET_ARCH=x86_64
TARGET_ARCH_VARIANT=silvermont
HOST_OS=linux
HOST_OS_EXTRA=Linux-6.8.0-1044-gcp-x86_64-Ubuntu-22.04.5-LTS
HOST_CROSS_OS=windows
BUILD_ID=BP2A.250605.031.A3
OUT_DIR=out
SOONG_ONLY=false
============================================
Step 7: Build AOSP
Start the build process using the m command.
conrad@aosp1:~/aosp-16$ m
...
[100% 162285/162285] //frameworks/base/api:system-api-stubs-docs-non-updatable check current API [common]
#### build completed successfully (04:23:36 (hh:mm:ss)) ####
This step took approximately 4 hours and 23 minutes on an n2-highmem-16 machine.
Note: Using a machine with more cores (e.g., n2-highmem-32 or 64) would significantly reduce
this time.
Step 8: Stop the VM to Save Costs
The n2-highmem-16 machine we used for the build is expensive to keep running idle. Once the build
is successfully completed, you should stop the instance immediately to avoid incurring unnecessary
costs.
➜ gcloud compute instances stop aosp1 --zone asia-south1-a
When you are ready to run Cuttlefish (in the next guide), you can restart it (and optionally resize it back to a cheaper machine type if you don’t need 16 cores for just running the device).
Next Steps
In the next post, we will cover:
- Configuring the GCP Firewall for WebRTC streaming.
- Installing Cuttlefish host tools.
- Launching the virtual device and accessing it via a web browser.