7

Hosting an emulated android device on GCP

Deploying the Cuttlefish AOSP Android emulator on the cloud offers a scalable and cost-effective solution for self-hosted CI/CD. It eliminates licensing fees, combines build and test workflows on a single machine, and provides a responsive environment for automated Android app development and testing.

Deploying the Cuttlefish AOSP Android Emulator on the Cloud for Self-Hosted CI/CD

When it comes to developing and testing Android applications, a robust Continuous Integration and Continuous Deployment (CI/CD) environment can greatly enhance productivity. Traditional Android emulators often come with challenges like poor responsiveness, significant licensing fees, and complicated multi-machine configurations. The Cuttlefish Android Emulator, part of the Android Open Source Project (AOSP), provides a compelling alternative for developers looking to set up an effective CI/CD pipeline on the cloud. In this post, we'll explore how to deploy Cuttlefish on the cloud and use it as a self-hosted CI/CD solution for Android app development and testing.

What is Cuttlefish?

Cuttlefish is an open-source Android emulator developed by Google as part of AOSP, designed primarily for automated testing in CI/CD workflows. Unlike other Android emulators, Cuttlefish is cloud-optimized, enabling developers to run Android virtual devices in the cloud with support for advanced features like WebRTC for remote access, responsiveness improvements, and easy setup for scaling tests.

The key benefits of Cuttlefish include:

  • Open-Source Availability: No licensing fees, unlike other options like Genymotion.
  • WebRTC Integration: Enables users to remotely access the emulator through a web browser, offering features like keyboard and microphone integration.
  • Single-Machine Setup: Unlike some other solutions, Cuttlefish can be run on a single machine, reducing the need for a complex, multi-node setup.

Why Cuttlefish for Self-Hosted CI/CD?

The Cuttlefish emulator makes it possible to create a cost-effective and easily scalable CI/CD pipeline for Android app development. Traditional setups often involve multiple machines—one for compiling and one for running an emulator. The Cuttlefish emulator can run both functions on the same cloud machine, eliminating the need for licensing fees associated with proprietary emulators, improving responsiveness, and streamlining the deployment pipeline.

For teams developing Android apps, Cuttlefish can serve as a self-hosted CI/CD environment by automating builds, tests, and deployments without relying on third-party emulator licenses. The flexibility and cloud integration make it ideal for testing across multiple devices at scale.

How to Deploy Cuttlefish on the Cloud

Deploying Cuttlefish on the cloud involves several key steps, which we'll break down to make the process as straightforward as possible. You can use platforms such as Google Cloud, AWS, or Azure to set up your infrastructure, but this guide assumes you are familiar with setting up virtual machines and cloud services.

Step 1: Set Up a Cloud Virtual Machine

  • Select a Machine Type: An n2-standard-2 instance on Google Cloud is often sufficient for typical testing tasks, but a more performant machine can be used for larger apps or more concurrent users.
  • Enable Nested Virtualization: Make sure nested virtualization is enabled by running grep -c -w "vmx\|svm" /proc/cpuinfo. The output should be greater than zero.
  • Install Required Packages: SSH into the instance and install dependencies using the following commands:
    sudo apt update
    sudo apt install -y git devscripts config-package-dev debhelper-compat golang

Step 2: Clone and Set Up Cuttlefish

  • Clone the Cuttlefish Repository:
    git clone https://github.com/google/android-cuttlefish
    cd android-cuttlefish
    debuild -i -us -uc -b
    sudo dpkg -i ../cuttlefish-common_*_*64.deb || sudo apt-get install -f
  • Add User for Emulator: Create a new user (android-user) since running Cuttlefish as root can lead to issues:
    sudo adduser android-user
    sudo usermod -aG sudo android-user
    sudo usermod -aG kvm,cvdnetwork android-user
  • Download Required Files: Download necessary emulator images:
    mkdir cf
    cd cf
    touch download.sh
    chmod 777 download.sh
    ./download.sh x86_64

Step 3: Configure Networking and WebRTC

Cuttlefish uses WebRTC to enable browser access to the emulator, which allows developers to interact with the emulator as if it were on their local machine.

  • Open Required Ports: To use WebRTC, create firewall rules to allow traffic on the following ports:
    • tcp:6520, tcp:8443, tcp:15550-15558
    • udp:3478, udp:15550-15558
  • TURN Server Setup: To ensure reliable WebRTC connections, set up a TURN server. Cuttlefish uses TURN servers to help traverse NAT and firewalls, improving connection reliability.
    sudo apt-get install coturn
    sudo nano /etc/default/coturn  # Uncomment the TURNSERVER_ENABLED=1 line
    sudo service coturn restart

Step 4: Launch the Emulator

  • Start Cuttlefish: After configuration, launch the emulator with:
    su android-user
    cd /home/android-user/cf
    HOME=$PWD ./bin/launch_cvd
  • Connect to the Emulator: Once started, connect through a browser using the external IP and port 8443. You may need to bypass the browser security warning (e.g., type "thisisunsafe" in Chrome).

Step 5: Integrate with CI/CD Pipeline

  • Automate Build and Test Jobs: You can integrate Cuttlefish into a CI/CD pipeline tool like Jenkins, GitLab CI, or GitHub Actions. This integration will allow you to automate builds, run instrumented tests, and validate APKs on the cloud emulator after every commit.
  • Run Emulator as a Daemon: To enable automated runs, launch the Cuttlefish emulator as a daemon:
    HOME=$PWD ./bin/launch_cvd -daemon

Benefits of Cuttlefish as a CI/CD Solution

  1. Cost Efficiency: No licensing fees, and you can use existing cloud infrastructure.
  2. Scalability: Easily create multiple emulator instances for testing on different Android versions or device configurations.
  3. Simplified Workflow: Use a single virtual machine for both building and testing, reducing overhead and complexity.
  4. High Responsiveness: By running on a cloud VM with WebRTC integration, developers experience minimal lag, enhancing testing quality.

Conclusion

Deploying the Cuttlefish AOSP Android emulator on the cloud is a powerful way to build a self-hosted CI/CD environment for Android development. The use of a single machine for both compiling and emulation, combined with open-source advantages and WebRTC for easy access, makes Cuttlefish a great choice for developers looking to enhance their testing workflows. This solution provides a more responsive, cost-effective, and scalable way to automate Android application development and testing, taking your CI/CD pipeline to the next level.