Building LineageOS in WSL2
Tags: wsl, android, lineageos
Published: 2021-11-03 20:49:58

A few days ago, I needed an Android phone for some experiments, so I fished out an old Nexus 5 "Hammerhead" from a drawer where I keep the assorted lengths of wires. 

However, it had a pretty outdated Android 6 OS, and some apps refused to install. So I decided to get it a slightly newer LineageOS 14.1 (based on Android 7). Feeling adventurous, I decided to build one myself instead of downloading a ready build (from god-knows-where, by the way).

I used [this guide](https://wiki.lineageos.org/devices/hammerhead/install). Overall, it was surprisingly comprehensive. Still, I encountered a few obstacles, writing them down here for reference.

### Notable hiccups ###

**Preparing tools**  
- libwxgtk3.0-dev was renamed to libwxgtk3.0-gtk3-dev in Ubuntu 20.04
- to access USB devices in WSL, I had to run `adb` in tcp bridge mode (see [https://github.com/microsoft/WSL/issues/4619#issuecomment-596990676](https://github.com/microsoft/WSL/issues/4619#issuecomment-596990676))

**Getting the source files in place**  
- I'm using zsh, and it doesn't normally execute .profile, so my `$HOME/bin` wasn't included in the path -- `repo` didn't run. Being lazy, I just dropped it in the sources folder, and was punished on one of the next steps: failure to execute `repo` was breaking the `breakfast` pipeline. Luckily, the error was logged so I figured it out and fixed properly.
- Proprietary blobs for the device: this seemed like a Catch-22 situation; then I realised that I could get those from TheMuppets ([https://github.com/TheMuppets/proprietary_vendor_lge/tree/cm-14.1](https://github.com/TheMuppets/proprietary_vendor_lge/tree/cm-14.1)); note the branch in the path.
- TheMuppets repo must be included in the .repo/local_manifests/roomservice.xml like this:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <project name="LineageOS/android_device_lge_hammerhead" path="device/lge/hammerhead" remote="github" />
  <project name="LineageOS/android_kernel_lge_hammerhead" path="kernel/lge/hammerhead" remote="github" />
  <project name="TheMuppets/proprietary_vendor_lge" path="vendor/lge" remote="github" revision="cm-14.1" />
</manifest>
```
- After rerunning `repo sync` the blobs were added to the source files.

**Building**  
- The build process was inexplicably failing somewhere around 2%. I ran `jack-diagnose` tool from `prebuilts/sdk/tools`, but it wasn't detecting any issues. Neither the jack-server logs had anything of note in them.
- I suspected that jack-server was quietly crashing due to insufficient memory, and did several things at once: 
  - increased WSL memory limit from 6Gb to 8Gb (my laptop has 16Gb total), 
  - in `~/.jack-server/config.properties`, set `jack.server.max-service` to 1 instead of 4, 
  - set `-Xmx5G` instead of `-Xmx4G` in `ANDROID_JACK_VM_ARGS`,
  - and upgraded jack-server package as suggested [here](https://forum.xda-developers.com/t/discussion-how-to-fix-jack-server-failing-to-build-with-error-try-jack-diagnose.3575179/#post-71485731).

Not sure which exact part was crucial, but after doing this the build went on smoothly.

**Loading the firmware**  
- The TWRP recovery wouldn't start from the bootloader, so I had to use this command: `fastboot boot <recovery.img>` instead.
- I had to install the "pico" (smallest) Google Apps package along the OS ROM, otherwise it wouldn't fit in without repartitioning.

**Finally**  
The first boot of LineageOS took a while, and I had to reboot it once when it got stuck at "looking for updates" step. 
After the initial setup, it has been working pretty well! On with the experiments!