Howto: Flash an Android factory image to a Nexus device
Every time Google releases a new version of Android, sooner or later factory images for Nexus devices will pop up on their developers website. Sometimes it’s desirable to install these factory images instead of waiting for an over-the-air update (OTA). If you follow Google’s instructions on how to flash these images to your device, you will lose all personal data. Here’s how you flash one of these images to your device without losing any of your data.
Prerequisites
- You need a running ADB installation. The easiest way is to install the Android SDK. Also, you have to enable USB debugging on your device.
- You need an unlocked bootloader. Unlocking your device’s bootloader for the first time will remove all your data. There are plenty of tutorials on how to unlock the bootloader of a Nexus device.
Step 1: Download factory image archive
Go to the Factory Images for Nexus Devices page and download the correct image for your device. I will use the Nexus 5 (“hammerhead”) image for Android 5.1.1 (LMY48I) in all further examples.
Step 2: Extract archives
Extract the archive you just downloaded:
$ tar xfv hammerhead-lmy48i-factory-a38c3441.tgz
# hammerhead-lmy48i/
# hammerhead-lmy48i/flash-all.bat
# hammerhead-lmy48i/flash-all.sh
# hammerhead-lmy48i/flash-base.sh
# hammerhead-lmy48i/bootloader-hammerhead-hhz12h.img
# hammerhead-lmy48i/radio-hammerhead-m8974a-2.0.50.2.26.img
# hammerhead-lmy48i/image-hammerhead-lmy48i.zip
As you see, this extracts flash scripts, two image files for bootloader and radio, as well as another ZIP file containing further images. Go ahead and extract that ZIP file:
$ cd hammerhead-lmy48i
$ unzip image-hammerhead-lmy48i.zip
# Archive: image-hammerhead-lmy48i.zip
# inflating: android-info.txt
# inflating: userdata.img
# inflating: cache.img
# inflating: system.img
# inflating: boot.img
# inflating: recovery.img
Step 3: Reboot your device to bootloader
Enable USB debugging on your device, connect a USB cable and reboot to bootloader:
$ adb reboot bootloader
Step 4: Flash images
Flash the bootloader
, radio
, cache
, boot
, system
and optionally the recovery
image. Don’t flash the userdata
image, this will wipe all user data on your device! Don’t use the flash scripts provided in the first archive, as they will wipe all your data!
$ fastboot flash bootloader bootloader-hammerhead-hhz12h.img
$ fastboot reboot-bootloader
$ fastboot flash radio radio-hammerhead-m8974a-2.0.50.2.26.img
$ fastboot reboot-bootloader
$ fastboot flash cache cache.img
$ fastboot flash boot boot.img
$ fastboot flash system system.img
$ fastboot flash recovery recovery.img
$ fastboot reboot
Optionally: Re-root
If your device was rooted before you flashed the factory image, you have to re-root it afterwards. Let your device boot normally once, then go ahead and root it with your rooting method of choice.
That’s it, enjoy your new Android version!