Unlocking the bootloader
Firstly we need to unlock the bootloader to allow us to flash new images. Go to Settings → System → About phone and tap on Build number 7 times or until you see the message ‘You are now a developer’.
Now go to Settings → System → Developer options and enable USB debugging and OEM unlocking.
adb devices. You should see something like:
List of devices attached 804886 device
This confirms that the phone is connected to the computer. Now run
adb reboot bootloader to reboot the phone into fastboot mode. Alternatively, switch off the phone and turn it on while holding Volume Down. Wait until the phone displays the fastboot screen:
fastboot oem unlock. The phone will display information about unlocking the bootloader and prompt for confirmation. Use the volume up/down buttons to select the appropriate choice, and use the power button to confirm.
Note that doing this will perform a factory reset, erasing all data on the phone. Therefore, I recommend doing this as soon as you unbox the Xiaomi Mi A2 if you intend on rooting it.
The phone should then soon restart, and you will need to reconfigure the phone. This is the appropriate time to accept the update to Android 9.0 if you haven't already.
Then repeat the process at Settings → About phone → Build number to enable developer mode, and enable USB debugging and OEM unlock again under Settings → System → Advanced → Developer options.
At this point, you may also like to install a custom recovery and/or take a backup of the phone before proceeding. TWRP is available for the Xiaomi Mi A2 at https://twrp.me/xiaomi/xiaomimia2.html.
Prepare Magisk boot image (bootstrap)
Download the latest release of Magisk Manager, e.g. MagiskManager-v7.3.0.apk, and install it to the phone:
adb install MagiskManager-v7.3.0.apk
Magisk Manager needs a copy of the stock boot ROM, so head to https://en.miui.com/download-353.html and download the stock firmware for the Mi A2. Ensure that the version (e.g. 10.0.2.0) and build number (PDIMIJF) are the same as shown under About phone.
Open this file (e.g. jasmine_global_images_V10.0.2.0.PDIMIFJ_20181206.0000.00_9.0_02b8deeb6d.tgz) and extract the images/boot.img file.
Upload this file to the phone:
adb push boot.img /sdcard/
Now open Magisk Manager and tap Install → Patch Boot Image File and select the boot.img file uploaded to the phone.
If all goes well, this should produce a patched_boot.img ready for use with Magisk. Download this file:
adb pull /sdcard/Download/patched_boot.img
Installing Magisk proper
adb reboot bootloader to again return to fastboot mode, and
fastboot boot patched_boot.img to boot the newly-patched image.
If all goes well, Android should launch normally, and Magisk Manager should show that Magisk has been installed. Note, however, that this is only temporary, so to make it permanent, tap Install → Direct Install (Recommended).
If the installation is successful, you should be able to reboot the phone normally and Magisk will continue to function normally.
At the time of writing, Xposed is not available for Android 9.0, but EdXposed can be used as a replacement for some (but not all) Xposed modules.
Follow the instructions in the Install section of the EdXposed page:
Use Magisk Manager to install Riru-core and EdXposed under Downloads.
Then install the EdXposedManager, give it access to Superuser within Magisk Manager, and reboot. The EdXposed Manager should report that EdXposed is installed correctly and allow you to install and manage EdXposed modules.
This article was originally published on 24 January 2019, dealing with EdXposed v0.2.9.1. The article has since been updated to account for changes to the EdXposed installation process, and is correct for EdXposed v0.4.1.2.