Replacing Android with LineageOS

Fresh Breath of Life

Author(s):

Breathe new life into your old smartphone: The TWRP recovery image and the free Android offshoot LineageOS let you install the latest security updates every week.

Every mobile device needs its own Android build because of numerous drivers that are not available in the source code. The need to maintain every version of Android for every mobile device means that many manufacturers eventually stop supporting updates. Often, smartphones or tablets that still work perfectly can no longer be used without worry because the manufacturer has simply ceased to offer bug fixes and security updates.

The fact that many drivers for smartphone hardware are not available as source code makes it difficult to provide Android upgrades. The LineageOS project [1], the successor to the CyanogenMod project, which was discontinued in 2016, proves that it is not impossible to keep these devices up-to-date. Unpaid volunteers at LineageOS do the work that many manufacturers do not want to do: They combine current Android releases with the required device-specific drivers.

Dewy

The LineageOS project (Figure 1) provides Android systems with a fresh patch status every month for around 300 devices. The builds are released weekly, unless there is a problem during the build. The Devices page on the LineageOS Wiki [2] provides the details of whether a LineageOS build is available for your smartphone or tablet.

Figure 1: The LineageOS team continues work of the CyanogenMod project, which was discontinued in 2016. LineageOS provides over 300, often older, devices with fresh Android updates every month.

Practically all Android devices support a recovery mode that starts a small, specialized Android system to restore the main system in case of damage. Out of the box, recovery mode only installs the state intended by the manufacturer. Therefore, the first step towards installing an alternative OS such as LineageOS is to flash a manufacturer-independent recovery system. I prefer to use TWRP [3] as a recovery tool because, unlike the recovery system supplied by the LineageOS project, it creates backups of the existing system (Figure 2). TWRP (pronounced "twerp"), is a custom recovery tool for Android phones. TWRP replaces the recovery system provided by the hardare manufacturer, which then allows you to install an independent system such as LineageOS.

Figure 2: The TWRP recovery system offers a graphical interface that is friendly enough for non-professionals.

Unless you unlock the bootloader, you can't install another recovery system or a full system on the Android smartphone later (Figure 3). As of Android 6, you can enable the OEM unlock or Bootloader unlock item in the Android settings below Developer options | OEM unlock to unlock the bootloader.

Figure 3: The Android Fastboot bootloader reports "Device is UNLOCKED."

If you do not see the Developer options menu item under System | Advanced, tap the Build number displayed at the end of the heading seven times in the About the phone settings category. You also need to enable the USB debugging option for flashing via USB.

However, this procedure does not remove locks set up separately by the manufacturer to prevent the installation of other software and rooting of the smartphone. Some manufacturers require you to register the device to unlock it, and then – after telling you that the warranty is now void – they hand over a code. Others refuse to unlock the device altogether. However, it is probably not possible, in the EU at least, to make the hardware warranty dependent on you not installing your own software. The LineageOS Wiki page for your device [2] also describes how to unlock the bootloader.

Once you have freed the bootloader, it is time to download the necessary software: First, you need the TWRP recovery image [4] for your device. On your Linux computer, install the Android tools (the Ubuntu packages are android-tools-adb and android-tools-fastboot, otherwise it's just android-tools) containing the Fastboot program as a counterpart to the bootloader on the Android device. Working from the PC, the tools load the recovery image into the partition assigned by the manufacturer's recovery routine. Finally, you need an image of the actual LineageOS system, which the recovery routine finally installs on the system partition after a backup.

Right Choice

Android builds – the final LineageOS as well as TWRP – only work on the device they were built for using the Android open source project and numerous proprietary drivers. To make sure you're installing the right image, you need to know the manufacturer's code name for the device. This will help you identify the right images on the LineageOS and TWRP download pages. The app Device ID [5] gives you the name on a running system (Figure 4).

Figure 4: The Android Device ID app shows the code name of the device; you can then use this code name to find the right image to flash.

Variants of a basic device type can exist in some cases; they are stated in the Device name line in the About phone tab in Android settings. Normally a build should support all variants, but it doesn't hurt to check the LineageOS wiki page for the device matching the codename to see if your model shows up under Supported models.

Once all the software components are in place, boot the Android device to the unlocked bootloader. Most Android devices (with the exception of Samsung, see the box "Installing TWRP on Samsung Devices") rely on Android's own Fastboot bootloader. The LineageOS wiki explains how to access the bootloader for each device. Usually, you can reach the bootloader by turning off the device and then turning it back on while holding down the Volume Down key.

Installing TWRP on Samsung Devices

Smartphones and tablets by Samsung do not use the Fastboot bootloader, which is otherwise common for Android. Samsung has developed its own Windows tool named Odin for flashing firmware. Fortunately, there is a free Linux port named Heimdall [6]; the popular Linux distributions have Heimdall packages.

Volume Up+Home+Power boots a powered-down device to Download mode, which expects data for flashing via USB. If heimdall print-pit on the Linux console prints a series of messages and the device finally boots the normal system, then the communication via USB cable between PC and smartphone is working in Download mode.

After turning the device off again, you can switch back into Download mode using the key combination shown above. The command from Listing 1 installs the TWRP recovery image [4] for the model. Then immediately turn off the device by either removing the battery or pressing Volume Down+Power for eight to 10 seconds. Once the screen goes blank, release the keys immediately. On the powered-off device, press Home+Volume Up+Power to start recovery mode with TWRP. You will now be able to flash Lineage via the recovery option just as you would with other Android devices.

Listing 1

Flashing Samsung Devices

$ heimdall flash --RECOVERY twrp-version_number-Devicecode.img --no-reboot

Fastboot fields the data from the PC fastboot command-line program and writes it to partitions on the mobile device. The prerequisite is a USB connection between the PC and smartphone. After plugging in the USB cable, first run adb devices to test whether this connection with the bootloader launched as described and the console program works (Figure 5).

Figure 5: If the list returned by adb devices contains an entry, the USB connection between the mobile device bootloader and PC is working.

The command in Listing 2 installs the TWRP recovery system on the recovery partition. The percentage values displayed at the command line during the transfer often turn out to be not very meaningful. The main thing is that the operation completes without an error message.

Listing 2

Flashing the Image

$ fastboot flash recovery image_file

The factory-installed recovery system is now overwritten. The option to use it to return to the factory state of the smartphone is no longer available. Instead, you can now back up and restore the actual state of the system with TWRP.

Use the volume keys to select which system the currently active bootloader will start. The options START (normal system) and RECOVERY MODE are important. You can start the recovery that you need next right away or make sure the normal system is working to reassure yourself. In this case, turn off the device again and go back to the bootloader.

Data Manager

TWRP recovery comes with a clear graphical interface (Figure 2). First you need the Backup function to save the previous system, which has not yet been replaced by LineageOS. Later on, when you install the weekly LineageOS updates, it always makes sense to back up a version that you know will work up front. The backup only takes a few minutes on a newer device with a sufficiently fast SD card.

The backup screen opens, showing you the partitions System, Data (excl. storage), and Boot preselected for the backup. These settings are correct for a later restore of an entire system. In Select Storage, you now need to select the desired storage medium. The storage medium is usually not Internal Storage but an SD card.

Many users move the large backups to the PC immediately after they have been created. You will find the data on the storage medium below TWRP/BACKUPS/<DEVICE ID>/ in a directory with a name that starts with the date and time and contains a system name such as lineage. To restore a backup, restore this folder to the same location. The backups will then appear as a list in the TWRP Restore function. After selecting a backup, all you do to restore it is drag a slider to the right. If a file selector appears instead of the list of existing backups, use Select Storage to switch between Internal Storage and SD Card.

A file manager is available on the TWRP home screen after you select Advanced (Figure 6). You can access the home screen at any time via the button at the bottom of your screen. Using the file manager is not immediately intuitive: For example, it is important to know that the contents of the SD card can be found in /data/media/0/, including the aforementioned TWRP/ folder. To delete a backup subfolder, open it and tap Select Current Folder bottom right. The file manager then displays the Delete option (line top right in Figure 6).

Figure 6: TWRP integrates a file manager that you can use to delete older backups or other data as needed to free up space.

Tabula Rasa

Anyone installing a system of a different type, be it a change from the default Android to Lineage or a newer LineageOS version, now needs to trigger a factory reset (Wipe in TWRP, the Factory Reset, is preselected (Figure 7)). All the installed apps are lost but not the data, such as music files or photos. It goes without saying that a backup of this data is recommended, in addition to the system backup with TWRP.

Figure 7: The TWRP Wipe menu item performs a factory reset after dragging the slider – almost always the only reset operation you need, as the program itself correctly notes.

The Titanium Backup [7] smartphone program gives you an option for backing up the installed apps, including settings before wiping, but it requires the device to be rooted up front (Figure 8). In principle, you can use this option to transfer apps purchased in the Play Store to a smartphone without using Google's services because Titanium saves the associated APK installation packages, which can then simply be copied. In practice, the apps usually check whether they are running on a device registered to the buyer and refuse to work or cannot be installed at all. The MicroG framework [8], an open source replica of Google's services, promises a remedy.

Figure 8: The Titanium Backup app backs up all installed apps, including their settings, on rooted devices and quickly restores them after a factory reset.

The MicroG services also need to contact the Google server for license management but will conceal your personal data, according to their developers. Their location services work offline, for example. Most apps start without problems in the environment, but some do not [9].

Although MicroG continuously transmits the device IP to Google, many users might be more trusting of an open source app – for example, when it says that it does not read and transmit the GPS coordinates if the location services are disabled. If you insist on a completely Google-free smartphone, you have no choice but to restrict yourself to the alternative F-Droid app store (Figure 9). There are far fewer apps available there but still around 4,500.

Figure 9: Many authors of free Android software exclusively publish their apps in the alternative F-Droid store, or they use F-Droid in addition to the Google Play Store.

MicroG cannot be post-installed on LineageOS because it has to spoof signatures to Google – LineageOS does not allow that at the system level. However, there is the LineageOS fork LineageOS for MicroG [10], which integrates MicroG into an otherwise unmodified LineageOS system. /e/OS [11] goes a bit further, with its own app store and many free apps from the Play Store.

Install

After running the backup function, the next step is flashing. Click the Install button in the main menu to get the flashing started. This step installs the ZIP images [2] of the LineageOS distribution. You just need to tap on an archive file in the file manager on the Install screen.

In the next screen, the software offers you the option of flashing additional ZIP archives onto the new system (Add more Zips), for example, a root extension for LineageOS or the Google Play Store (see box entitled "LineageOS Add-Ons"). You can launch the actual flash process by dragging the Swipe to confirm Flash slider to the right.

LineageOS Add-Ons

Many power users root their system, as rooting is the only way to adblock, control cells with a tool such as Tasker [12], or tune the kernel settings for better performance and improved power consumption. However, current LineageOS releases no longer offer the option to root via a checkbox in the developer options. Instead, the project recommends installing Magisk [13]. Rename the downloaded Magisk-VERSION.apk file to Magisk-VERSION.zip, and TWRP's Install function will recognize it as an installation image that you select with Add more Zips before flashing the main system. On a rooted system, install the original Magisk-v2x.x.apk package, which refreshes the app without launching the rescue system.

LineageOS does not ship Google Play Store or other company services and apps by default. If you want to use the Play Store or paid apps, you need to install OpenGApps [14] before starting the new system for the first time. Apps are also available as ZIP files, which you can add to the flash process in TWRP by selecting Add more Zips. On the OpenGApps page, select the correct variant based on three properties: the processor platform (see the LineageOS wiki page for your device: Architecture), the Android version (also in the LineageOS wiki), and the Variant, that is, the content scope of the OpenGApps package. The stock option sets up the full scope installed on Nexus phones. The nano option includes all items not available through the Play Store and is recommended for users who want to build their own systems.

Quickly Served Up

Sideloading lets you you transfer files directly from your computer without requiring you to manually copy the files on the SD card. (TWRP still needs enough storage space for the image on the SD card.) Sideloading also lets you install several ZIPs one after another.

For a sideload, with the smartphone connected to the PC and the TWRP system running, first call adb devices to check if the mobile device can be accessed. The adb tool is in the android-tools-adb (Ubuntu) or android-tools package. The adb call should return a line with a device identifier, followed by Recovery. Select Advanced | ADB Sideload on the TWRP homescreen and drag the appropriate slider to the right.

On the PC, run adb sideload IMAGE. Again, the percentage value displayed on the PC console is not very meaningful; it often remains at 0% for a long time. In our lab, the transfer even supposedly aborted at 49 percent, but TWRP reported a complete installation and the new system booted flawlessly.

Now you have a clean Android system without bloatware, either with or without Google App Store, and with the resource-saving Trebuchet homescreen app (Figure 10), which is perfect for many users. You can select the home app in the Android settings below Apps and Notifications | Default Apps | Start App.

Figure 10: Trebuchet, the default homescreen app on LineageOS, combines good configurability with acceptable resource consumption.

The Android web browser, a calendar, a picture viewer and camera app, a telephony app, email, a text message app, a simple audio player, a voice recorder, a calculator, and a file manager are pre-installed.

Conclusions

I recommend the LineageOS project as the first port of call for anyone who wants to protect an older smartphone or tablet that is no longer maintained and doesn't receive Google security patches. The LineageOS derivatives LineageOS for MicroG and /e/OS make it even easier to enjoy a Google-free smartphone without too many restrictions.