How to get root access on Android emulator?

2019-01-02 17:01发布

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.

10条回答
梦醉为红颜
2楼-- · 2019-01-02 17:39

These answers are all unnecessarily complicated :)

$ > adb shell
generic_x86:/ $
generic_x86:/ $ exit
$ > adb root
restarting adbd as root
$ > adb shell
generic_x86:/ #
查看更多
步步皆殇っ
3楼-- · 2019-01-02 17:39

Here my pack with all you need. Or you can use this script:

echo on
set device=emulator-5554
set avd_name=
set adb=d:\Poprygun\DevTools\Android\Android-sdk\platform-tools\adb -s %device%
set emulator=d:\Poprygun\DevTools\Android\Android-sdk\emulator\emulator
set arch=x86
set pie=

echo Close all ANDROID emulators and press any key
pause
start %emulator% -avd Nexus_One_API_25 -verbose -writable-system
echo Wait until ANDROID emulator loading and press any key
pause

%adb% start-server
%adb% root
%adb% remount
%adb% shell setenforce 0
%adb% install D:\SuperSU\SuperSU.apk
%adb% push D:\SuperSU\su\%arch%\su.pie /system/bin/su
%adb% shell chmod 0755 /system/bin/su
%adb% push D:\SuperSU\su\%arch%\su.pie /system/xbin/su
%adb% shell chmod 0755 /system/xbin/su
%adb% shell su --install
%adb% shell "su --daemon&"
pause
exit /b
查看更多
宁负流年不负卿
4楼-- · 2019-01-02 17:40

How to root android emulator (tested on Android 7.1.1/ Nougat)

Requirements:

Instructions

  1. 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.

enter image description here


  1. 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.


  1. 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


  1. 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)


  1. 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


  1. Setting SELinux to Permissive(i.e turning off SE Linux)

    • Finally turn off selinux through this code:

setenforce 0


  1. 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.

enter image description here

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.

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;

查看更多
宁负流年不负卿
5楼-- · 2019-01-02 17:44

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:

mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
cd /system/bin
cat sh > su && chmod 4775 su

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!

查看更多
登录 后发表回答