Running self-compiled Android firmware with Cuttlefish

Recently, I upgraded my local Android source code to the latest Android 15, and Android Studio for Platform, which is used to look at Android source code, has also been upgraded to the latest version. Google’s Cuttlefish has recently released version 1.0, and I’ve also tossed around the idea of using Cuttlefish to run I’ve been using Cuttlefish to run my own compiled Android, so here’s how to use it and the problems I’ve encountered.

What’s a Cuttlefish?

Cuttlefish is a virtual Android device from Google that can be configured to run on our local devices or on top of a server, and officially provides support for running on Docker, which can theoretically run on a Debian device locally or on a server, or on the Google Compute Engine! Docker can theoretically run on local or server Debian devices, or on Google Compute Engine.

In terms of officialization, it is an Android emulator that is closer to a real device, except for the Hardware Abstraction Layer (HAL), it is basically the same functional performance as a physical device. Using it for CTS testing, continuous integration testing will have higher fidelity.

Running it in the command line, there is no emulator-like UI, we can see its UI in two ways, one is through ADB connection, and the other is to turn on its webrtc function to view and interact with it in the browser. And his virtual hardware feature allows us to simulate multiple screens, test bluetooth wifi and various other features.

Installing Cuttlefish and compiling Android firmware

First we need to check if our device supports KVM virtualization using the following command:

1 
grep -c -w "vmx\|svm" /proc/cpuinfo 

If you get a non-zero value, it is supported.

After that we need to have an Android firmware that can optionally go to theAndroid Continuous Integration WebsiteDownload their compiled firmware, you can also compile your own firmware. Download the firmware should pay attention to download the device target with cf, and download the target CPU and need to run the host CPU architecture, ARM on the download of ARM, X86 on the download of X86_64, the specific operation can see the official tutorial. I here is their own compilation, using the following code device I want to compile the options:

1 
lunch aosp_cf_x86_64_phone-trunk_staging-eng 

That way with the firmware, it still won’t be able to run. We still need to go and compile Cuttlefish in thehttps://github.com/google/android-cuttlefishAfter downloading the source code, execute the following code in the cuttlefish source directory to compile and Android:

1 
2 
3 
tools/buildutils/build_packages.sh 
sudo dpkg -i ./cuttlefish-base_*_*64.deb || sudo apt-get install -f 
sudo dpkg -i ./cuttlefish-user_*_*64.deb || sudo apt-get install -f 

If you’re lucky with the chemistry, the above will work all at once, but I’m not a lucky guy. So got an error similar to the following:

1 
While resolving toolchains for target //src/tools/ak/generatemanifest:generatemanifest (6312974): invalid registered toolchain '@local_jdk//:bootstrap_runtime_toolchain_definition': no such target '@local_jdk//:bootstrap_runtime_toolchain_definition': target 'bootstrap_runtime_toolchain_definition' not declared in package '' defined by /home/sam/.cache/bazel/_bazel_jcater/ddb4e20e0e2e6bca92f5deeef02ce168/external/local_jdk/BUILD.bazel (Tip: use `query "@local_jdk//:*"` to see all the targets in that package) 

The reason for this error is that the build tool bazel is used when compiling cuttlefish, which relies on the JDK, and I didn’t set theJAVA_HOMEThis environment variable, so just add it to the environment variable. Something like the following:

export JAVA_HOME=/usr/lib/jvm/zulu-17-amd64 

After the setup is complete, check in the Cuttlefish project directory with the following command to see if the JAVA_HOME is set correctly:

1 
bazel info java-home 

But after messing with it, I had another problem when installing the two deb files, telling me that my computer’sgrub-commonThere is an error in the signature, this is because I added the Copper Pea repository before, and when I upgraded grub, I upgraded the grub package of Copper Pea, which is different from the official one of ubuntu, so I uninstalled the Copper Pea repository, and grub-common was reinstalled, and after that, I had no problem. After all this, we run the following commands to set up the environment and reboot the computer.

1 
2 
sudo usermod -aG kvm,cvdnetwork,render $USER 
sudo reboot 

使用Cuttlefish

In our compiled Android system directory, first execute the following code to initialize the environment:

1 
2 
source ./build/envsetup.sh 
lunch aosp_cf_x86_64_phone-trunk_staging-eng 

The following command will then launch Cuttlefish to run Android:

1 
launch_cvd --daemon 

If you are downloading from the official Android, then there will be some differences with me, so check out the official tutorial.

At this time we can use adb to see if the device has been booted, or you can open it in a browser and browse locally for its opening using the following address and port:

https://localhost:8443/ 

Be sure to use https for the address and click the swtich button on the left to see the UI. webrtc is turned on by default, for more usage of its command line you can check the official documentation, which can be viewed using the following command.

1 
launch --help 

And shutting down Cuttlefish is as simple as using the following command:

1 
stop_cvd 

新版Android Studio for Platform使用

Version 2023 of Android Studio for Platform (hereafter referred to as Asfp) opens with a separateOpen Aosp projectThe new version removes this option. I was confused when I first used it and tested Import and Open, but finally realized that the new version of theNewThe option is directly an import of the Aosp project.

The way to use it is shown below.

We can choose the Module we need to import according to the above figure, choose the location where the project file generated by Asfp for us is stored, after which Asfp will execute thelunchoperation and some of the dependency builds it requires. Underneath our selected directory will also generate aasfp-config.jsonfile, it is our project settings, if we have changes in the future (such as want to see the code of different modules), we can also directly modify this file.

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
{ 
  "repoRoot" : "/home/sam/android/android-source", 
  "modulePaths" : [ 
    "frameworks", 
    "packages/inputmethods" 
    ], 
  "lunchTarget" : "aosp_cf_x86_64_phone-trunk_staging-eng", 
  "nativeConfig" : { 
    "excludePaths" : [ ], 
    "excludeGenPaths" : [ ] 
  }, 
  "syncConfig" : { 
    "environmentVars" : { }, 
    "buildFlags" : [ ] 
  }} 

Reference content and information:

  1. Cuttlefish Official Documentation:https://source.android.com/docs/devices/cuttlefish
  2. Cuttlefish Official Repo:https://github.com/google/android-cuttlefish
  3. Bazel User’s Guide:https://bazel.build/docs/user-manual
  4. Android Cuttlefish emulator:https://2net.co.uk/blog/cuttlefish-android12.html

Read and comment on it!