I have All Android SDK versions(from 1.5 to 2.3.3), and I tried many methods for getting root in Android emulator. I don't use any Android device and test everything on emulator(AVD).
I need to get root access in any one of the Android emulator to use the 'iptables' and 'busybox' functionality. And to use iptables I must have root access. Atleast 'su' command should execute in the terminal emulator.
I also installed z4root
application,
But it takes very long time and doesn't finish rooting, and gets stuck. some say that if we downgrade the system to below RC30, this way we can get root access. if this is true, then how to do this? I use both Linux and Windows OS.
Please someone tell me any method to root my emulator.
These answers are all unnecessarily complicated :)
Here my pack with all you need. Or you can use this script:
How to root android emulator (tested on Android 7.1.1/ Nougat)
Requirements:
SuperSU app (chainfire) latest version 2.82
Recovery flashable.zip (contains su binary) (Here is alternative backup link provided XDA user Ibuprophen for flashable zips if the main link is not working: Flashable zip releases)
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.exe -avd {emulator_name} -writable-system
Note: Navigate to the tools folder where Android SDK is installed and open command prompt there by pressing shift and right clicking.
Pushing su binary in system directory
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.
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
orxbin
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 thesu
and notsu.pie
Change permissions of the su binary
adb -e shell su root cd /system/bin chmod 06755 su
Important!! Take note of su binary path (mine is /system/bin)
install
directive on su binary and set adaemon
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)
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.g1359g.tmp
with a large size) and replace defaultsystem.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 thesystem.img
method isn't working):Ideally it is more like hibernarig the virtual device with config intact, hence everything is preserved.
Snapshots
This implies the only requirement to start the emulator is adding the
-writable-system
parameter to the normalemulator -avd [avdname]
command to start the emulator. (Running the emulator just withemulator -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;
Irvin H: Rooting the android emulator -on Android Studio 2.3((Android 4.4)
Android AVD root access fail.
I used part of the method from the solutions above; however, they did not work completely. On the latest version of Andy, this worked for me:
On Andy (Root Shell) [To get, right click the HandyAndy icon and select Term Shell]
Inside the shell, run these commands:
Then, install SuperSU and install SU binary. This will replace the SU binary we just created. (Optional) Remove SuperSU and install Superuser by CWM. Install the su binary again. Now, root works!