Android Emulator Development, Anbox, & Waydroid

Minimal Anbox AOSP Build: Stripping Android for a Leaner, Faster Container Experience

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction: The Quest for a Lean Android Container

Anbox and Waydroid have revolutionized how we run Android applications on Linux, providing seamless integration and near-native performance. However, the default Android Open Source Project (AOSP) images, even for emulator targets, often come laden with components unnecessary for a containerized environment. This bloat can lead to increased resource consumption, slower boot times, and a larger disk footprint. This article details the process of building a highly stripped-down AOSP image specifically tailored for Anbox or Waydroid, enabling a leaner, faster, and more efficient Android container experience.

By removing non-essential applications, services, and frameworks from the AOSP build, we can significantly reduce the image size and improve overall performance. This guide is for developers, system administrators, and enthusiasts looking to optimize their Android-on-Linux setup.

Setting Up Your AOSP Build Environment

Prerequisites

Building AOSP is a resource-intensive task. Ensure your system meets the following minimum requirements:

  • Operating System: Ubuntu 18.04+ (LTS recommended) or Debian.
  • Disk Space: At least 250 GB of free space (SSD highly recommended for speed).
  • RAM: 16 GB minimum, 32 GB or more recommended.
  • CPU: Modern multi-core processor (8+ threads recommended).

Install the necessary build tools and dependencies:

sudo apt update
sudo apt install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 libncurses5 lib32ncurses5-dev x11proto-core-dev libx11-dev libgl1-mesa-dev libxml2-utils xsltproc bc python3 python-is-python3 libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev libffi-dev libgdbm-dev

Initializing the AOSP Source Tree

First, download the `repo` tool and make it executable:

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

Next, create a working directory for your AOSP source and initialize the repository. For this tutorial, we’ll target a recent stable branch, e.g., Android 13 (Tiramisu).

mkdir aosp-minimal && cd aosp-minimal
repo init -u https://android.googlesource.com/platform/manifest -b android-13.0.0_r23 --depth=1 # Choose a recent stable branch
repo sync -j$(nproc)

The `repo sync` command will download the entire AOSP source code, which can take several hours depending on your internet connection and system speed.

Deconstructing the AOSP Build System for Minimalism

Understanding Build Targets and Product Definitions

After syncing, you’ll set up the build environment using the `envsetup.sh` script and choose a build target with the `lunch` command. For Anbox/Waydroid, we typically use an `x86_64` emulator target, as these are designed for virtualized environments.

source build/envsetup.sh
lunch

When prompted, select a target like `aosp_x86_64-userdebug`. The `userdebug` variant provides root access and debugging capabilities, which is useful for development.

AOSP build targets are defined by a combination of device, product, and board configurations. These definitions specify which packages, modules, and configurations are included in the final image. Our goal is to modify these definitions to exclude unnecessary components.

Identifying Bloat: What Can Go?

A standard AOSP build includes a vast array of applications and services that are often redundant in a containerized environment. These include:

  • Default User Applications: Browser, Calculator, Calendar, Camera, Contacts, DeskClock, Email, Gallery, Music, Messaging, Phone, QuickSearchBox, SoundRecorder.
  • Non-Essential System Apps: Print Spooler, Wallpaper Picker, Live Wallpaper Picker, Easter Eggs.
  • Development/Debugging Tools: Terminal, CtsShim (though some debug tools might be useful during initial setup).
  • Hardware-Specific Services: NFC, Bluetooth (if not intending to pass through).
  • Google Apps (GApps): While not part of AOSP by default, if you were integrating GApps, these would be a primary target for removal in a minimal build.

We need to be cautious not to remove critical system components that the Android framework relies on, such as core input methods, essential providers (like `MediaProvider` or `DownloadProvider` if needed), or core networking components.

Strategic Package Exclusion: Crafting Your Minimal Image

Locating Product Definitions

The core of stripping down AOSP involves modifying the product definition files. For `aosp_x86_64`, relevant files are typically found under `device/generic/goldfish/x86_64/` and `device/generic/goldfish/common/`. Specifically, look for files ending in `.mk`, such as `aosp_x86_64.mk`, `device.mk`, or `product.mk`.

Instead of directly modifying existing files (which can make `repo sync` problematic), it’s often better to create an overlay or a new product definition that inherits from the base and then explicitly removes packages.

The `PRODUCT_PACKAGES_EXCLUDE` Directive

The `PRODUCT_PACKAGES_EXCLUDE` variable is your primary tool for removing packages. You can add this variable to a custom `.mk` file or directly to the target product’s `.mk` file. For instance, you could create `device/generic/goldfish/x86_64/minimal_anbox.mk` and configure your `lunch` target to use it, or simply append to `aosp_x86_64.mk` (with caution).

Here’s an example of how you might populate `PRODUCT_PACKAGES_EXCLUDE`:

# device/generic/goldfish/x86_64/aosp_x86_64.mk (or a new custom .mk file)

# Inherit from the base aosp_x86_64 configuration
$(call inherit-product, device/generic/goldfish/x86_64/aosp_x86_64.mk)

PRODUCT_PACKAGES_EXCLUDE += 
    Calendar 
    Contacts 
    Dialer 
    DeskClock 
    Email 
    Gallery2 
    Music 
    Browser 
    Camera2 
    Launcher3 
    Messaging 
    Phone 
    PrintSpooler 
    WallpaperPicker 
    LiveWallpaperPicker 
    InputDevices 
    Nfc 
    PackageInstaller 
    DocumentsUI 
    WallpaperCropper 
    PhotoTable 
    EasterEgg 
    QuickSearchBox 
    SoundRecorder 
    CaptivePortalLogin 
    Terminal 
    LegacyCamera 
    DreamService 
    CtsShimPriv 
    CtsShim 
    ConnectivityTest 
    MtpApp 
    UserDictionaryProvider 
    Vpn 
    Wi-FiDisplay 
    WAPPushManager 
    BluetoothExt 
    # Add any other specific apps or services you've identified as unnecessary

# Optional: If you need a very minimal launcher, you might keep
# Launcher3 or replace it with something extremely basic. 
# For many containerized uses, a full launcher might not be needed.

# Note: Be extremely careful when removing core system services or providers.
# Removal of items like MediaProvider, DownloadProvider, or core InputMethods
# can render the system unstable or unusable for many apps.

After making these modifications, you might need to run `make clean` or `make clobber` before rebuilding to ensure all changes are picked up correctly.

Building Your Stripped-Down AOSP Image

Setting Up the Build Environment

Navigate back to the root of your AOSP source directory (`aosp-minimal/`) and set up the build environment again. If you created a custom product, you might need to define it or select the modified `aosp_x86_64` target.

source build/envsetup.sh
lunch aosp_x86_64-userdebug # Or your custom target

Initiating the Build Process

Now, start the build. This process will compile all the included components and create the Android images.

make -j$(nproc)

The build time will vary significantly based on your system’s specifications and the extent of the code changes, but a minimal build should be faster than a full AOSP build. Upon successful completion, your images will be located in `out/target/product/generic_x86_64/`. The key files you’ll need are `system.img`, `vendor.img`, and potentially `product.img` and `userdata.img`.

Deploying Your Minimal Image to Anbox or Waydroid

Anbox Deployment

To use your new minimal image with Anbox, you typically replace the existing images. Anbox stores its images in `/var/lib/anbox/android-images/`.

sudo cp out/target/product/generic_x86_64/system.img /var/lib/anbox/android-images/android-system.img
sudo cp out/target/product/generic_x86_64/vendor.img /var/lib/anbox/android-images/android-vendor.img
# If Anbox uses product.img and userdata.img, copy them as well
sudo systemctl restart anbox-container-manager.service

After restarting the Anbox container manager, your Anbox instance should boot with the new, minimal image.

Waydroid Deployment

Waydroid offers a simpler way to initialize with custom images. Ensure you have Waydroid installed, then use the `init` command:

sudo waydroid init -f -s out/target/product/generic_x86_64/system.img -v out/target/product/generic_x86_64/vendor.img

The `-f` flag forces re-initialization if Waydroid was already set up. After initialization, start Waydroid normally (`sudo waydroid show-full-ui`).

The Benefits of a Lean Android Container

Running a minimal AOSP image in Anbox or Waydroid offers several compelling advantages:

  • Reduced Disk Footprint: Significantly smaller image files save valuable storage space.
  • Faster Boot Times: Fewer components to load and initialize translate directly to quicker startup.
  • Lower Resource Consumption: Less RAM and CPU are consumed by background services and unused applications, freeing up resources for your host system or the apps you actually want to run.
  • Improved Security: A smaller attack surface due to fewer installed applications and services reduces potential vulnerabilities.
  • Tailored Experience: You have complete control over what runs in your Android container, ensuring it’s optimized precisely for your use case.

Conclusion: A Faster, More Efficient Android Experience

Building a minimal Anbox AOSP image is a powerful way to reclaim resources and enhance the performance of your Android containers. While the initial setup and build process require patience and attention to detail, the resulting lean and efficient Android environment is well worth the effort. By understanding the AOSP build system and strategically removing unnecessary components, you can craft an Android experience that is perfectly tuned for your specific needs, delivering a faster, more responsive, and more secure virtualized Android environment on your Linux machine.

Android Mobile Specs & Compare Directory

Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!

Compare Devices Specs →
Google AdSense Inline Placement - Content Footer banner