Building LineageOS 15.1 from source

Here is the step by step instructions on how to build Lineage OS 15.1 (Android 7.1 Nougat) in GNU/Linux.

I'm using Ubuntu 18.04 on my machine to build, I have tried building Arch as well, even though it will work fine, I had to spend some time to figureout all the dependencies.

You can get most of the information of this post from LineageOS Wiki, but some it in there are outdated.

Let's get started

First thing first, make sure you have enough RAM, there should be atleast 8GB. In my laptop, 8GB RAM wasn't enough, so I increased my swap filesystem size from 4GB to 8GB to handle build. You might have to do the same.

All the instructions following, are intended to run on terminal. I recommend that you use bash instead of zsh. Some of the scripts from Android source code, doesn't work in zsh.

Dependencies

Install following distro dependencies.

sudo apt install python git bc bison build-essential ccache curl flex g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev liblz4-tool libncurses5-dev libsdl1.2-dev libssl-dev libwxgtk3.0-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev openjdk-8-jdk unzip brotli

Setup folders

We need 2 folders

mkdir -p ~/bin ~/android/lineage ~/android/system_dump/system

bin folder, we use to keep repo binary, lineage folder for source code and system_dump folder to temporarily store device binaries.

SDK and Platform Tools

If you already have android sdk installed and configured, you can skip this step. If not, get the latest platform tools zip file from Google.

wget https://dl.google.com/android/repository/platform-tools-latest-linux.zip
unzip platform-tools-latest-linux.zip -d ~/android/

Environment variables

You can either run these command every time you try to build, or you can add it to your .bashrc file.

export LC_ALL=C
export PATH="$HOME/platform-tools:$HOME/bin:$PATH"

If you are adding to bashrc file, you need to either reload it using source ~/.bashrc or you can close and re-open the terminal.

Getting binaries

Even though Android is Open Source, there are several device specific proprietary binaries needed for you to build a working version of the source code. To get those binaries, we need to take it from a real device or from an existing build.

Here we are going to get it from Lineage OS 15.1 build of angler. Angler is the code name for Nexus 6P. You use this procedure for any 15.1 device, make sure you replace the term angler with your device code name in all the following instructions.

To get an existing build, go to download.lineageos.org and get the latest 15.1 nightly build. Save it to ~/android/system_dump/ folder.

Lets go to that folder in terminal.

cd ~/android/system_dump/

Extract necessary files from the zip.

unzip lineage-*.zip system.transfer.list system.new.dat.br

In the latest version of Android, system images are compresses using a tool called Brotli. To extract system image, we need to de-compress the file first.

brotli -d system.new.dat.br

You should be able to see system.new.dat file generated.

Now, to get necessary files extracted, we need a separate tool to convert .dat file to .img file, which we can mount in our machine.

sdat2img is the tool that we are going to use. Get the source code first.

git clone https://github.com/xpirt/sdat2img

Run the command to extract image file.

python sdat2img/sdat2img.py system.transfer.list system.new.dat system.img

Now we need to mount the image file.

sudo mount system.img system/

Since the image file is mounted, extract-files command from the lineage source code will be able to extract necessary file from it.
For that, we need to go the folder which contain the script and run it.

cd ~/android/lineage/device/huawei/angler/
./extract-files.sh ~/android/system_dump/

Almost ready!

Following things are options, but useful.

If you don't want to re-build a lot of things, even for a minor change, or you are working on a specific project under Android repo, an you don't want to re-build all the un-used dependencies every time you try to build, you can set CCACHE which caches output and re-use it the next time you build.

export USE_CCACHE=1

If you have a low memory device (I'm talking about 8GB/16GB) device, then it is possible that some processes during the build can cause build ran out of memory, setting following environment variable make sure that it doesn't. 4G in the tail end specifies that 4GB is the limit. You can inrease this based on your machine specifications.

export ANDROID_JACK_VM_ARGS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4G"

All set

Now we are all set. Go to the source root folder

croot

and build

brunch angler

It should take a good 2 digit hours to finish. When it is done, generated files will be can be seen by CD'ing to $OUT directory.

cd $OUT

A file with lineage-15.1-<date>-UNOFFICIAL-angler.zip name will be the flashable zip and recovery.img will be the recovery file.