How to root android emulator (tested on Android 7.1.1/ Nougat)
Requirements:
Instructions
- Install the SuperSu.apk
Install the SuperSu app firstly, just do drag and drop (if running latest emulator version or sideload through adb i.e adb -e install supersu.apk
)
After installing it, when you run it shows a screen as shown below indicating “There is no SU binary installed..”. This error just confirms the device is not yet rooted.
- Make emulator’ system partition writable
As it suggests, we need to give the emulator permission to write system files.
Type the following code to accomplish this: emulator -avd {emulator_name} -writable-system
If you have more than one AVD, you can get a list of avds by using the command: emulator -list-avds
Note: Navigate to the emulator folder where Android SDK is installed and open command prompt there by pressing shift and right clicking.
- Pushing su binary in system directory
- Extract the Recovery flashable.zip (containing the su binaries of different architectures)
Important! Only use the su binary that matches your avd architecture e.g x86, arm etc.., and note the path where you extracted these binaries.
- Make sure you are running adb as root and also you need to remount.
Just enter these codes
adb root
adb remount
Now its time to push the su binary:
This is the code I successfully used: adb -e push C:\Users\User1\Desktop\rootemu\x86\su.pie /system/bin/su
(nevermind about my specific location of su binary, any location is okay as long there is no white space)
note: To figure out bin
or xbin
do in console before: > adb shell
, > ls /system/xbin/su
If this fails try this pushing to this directory instead /system/xbin/su
. Also for emulators running android 5.1 and below use the su
and not su.pie
- Change permissions of the su binary
- Next let’s do a bit of modification of the permissions of su binary. We have to do this in emulator device through adb:
adb -e shell
su root
cd /system/bin
chmod 06755 su
Important!! Take note of su binary path (mine is /system/bin)
- Setting the
install
directive on su binary and set a daemon
Type the codes:
su --install
and for setting up daemon:
su --daemon&
Important!! Take note of spacing
- Setting SELinux to Permissive(i.e turning off SE Linux)
- Finally turn off selinux through this code:
setenforce 0
Open SuperSU app and it may ask to update binaries, you can use Normal method.
Note: If you're experiencing bootloops, rather don't update the binaries, just use it as it is.
That’s pretty much it!!
Open any application requiring SU permissions just to double check and indeed SuperSU ask if you wish to grant it su permissions.
To have the root persist update su binary (using Normal method), then copy system.img from temp directory (Users\AppData\Local\Temp\Android Emulator
the file is usually randomly named e.g 1359g.tmp
with a large size) and replace default system.img
.
Update:
I have noted is is easier to obtain a temporary system image in Linux, than Windows. You can try using snapshot image.
##Update 4 August 2018
With the emergence of emulator 27.3.x
it now makes preserving root much easier through snapshot feature (if copying the system.img
method isn't working):
Ideally it is more like hibernarig the virtual device with config intact, hence everything is preserved.
##Update 10 December 2023 (unofficial workaround)
With the implementation of Windows Subsystem For Android (WSA) alongside the patch from MustardChef;
It is possible to run Windows Subsystem For Android on Windows 10 and
Windows 11 PC using prebuilt binaries with Google Play Store
(MindTheGapps) and/or Magisk or KernelSU (root solutions) built in.
The installation steps are pretty much straight-forward as you need to download the prebuilt version(s) available here
Snapshots
You can now save multiple AVD snapshots for a given device
configuration and choose which of the saved snapshots to load when you
start the emulator. Starting a virtual device by loading a snapshot is
much like waking a physical from a sleep state, as opposed to booting
it from a powered-off state.
This implies the only requirement to start the emulator is adding the -writable-system
parameter to the normal emulator -avd [avdname]
command to start the emulator. (Running the emulator just with emulator -avd [avdname]
doesn't launch the rooted version/copy or may lead to some error)
Tested on API level 22
Also for bootloop issues see the other post: Android Emulator: How to avoid boot loop after rooting? and updates thereof.
Remarks
Most content in reference was for older android versions and hence the reason for different commands and paths which I modified.
Acknowledgements;