How to Get Root Access on Android Emulator

Obtain root access via su on the Android emulator

Ok, I solve the problem by myself :/

It works with sdk revision 10 and in an avd 2.2. The problem with the tip I have followed previously is the remount step. Here is the list of commands working for me (extract from http://forum.xda-developers.com/showthread.php?t=821742) :

adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system
adb push su /system/xbin/su
adb shell chmod 06755 /system
adb shell chmod 06755 /system/xbin/su
adb install superuser.apk

You can get the su binary and the superuser app here : http://forum.xda-developers.com/showthread.php?t=682828

You have to do that each time you start the emulator.

ADB root is not working on emulator (cannot run as root in production builds)

To enable root access: Pick an emulator system image that is NOT labelled "Google Play". (The label text and other UI details vary by Android Studio version.)

Exception: As of 2020-10-08, the Release R "Android TV" system image will not run as root. Workaround: Use the Release Q (API level 29) Android TV system image instead.

Test it: Launch the emulator, then run adb root. It should say

  • restarting adbd as root

or

  • adbd is already running as root

not

  • adbd cannot run as root in production builds

Alternate test: Run adb shell, and if the prompt ends with $, run su. It should show a # prompt.

Steps: To install and use an emulator image that can run as root:

  1. In Android Studio, use the menu command Tools > AVD Manager.
  2. Click the + Create Virtual Device... button.
  3. Select the virtual Hardware, and click Next.
  4. Select a System Image.
    • Pick any image that does NOT say "(Google Play)" in the Target column.
    • If you depend on Google APIs (Google Sign In, Google Fit, etc.), pick an image marked with "(Google APIs)".
    • You might have to switch from the "Recommended" group to the "x86 Images" or "Other Images" group to find one.
  5. Click the Download button if needed.
  6. Finish creating your new AVD.
    • Tip: Start the AVD Name with the API level number so the list of Virtual Devices will sort by API level.
  7. Launch your new AVD. (You can click the green "play" triangle in the AVD window.

[Credit belongs to @Merk but this should be posted as an answer rather than a comment.]

How can I get root access on an Android 7.0 emulator?

I'm writing the json files to /data/data//files using context.getFilesDir().getPath() + "/" + fileName

That is internal storage, not external storage.

Use adb shell run-as ... ??? to run commands as your app's user, which will allow those commands to access internal storage. Here, ... is the application ID of your app, and ??? is a command.

So, for example, to list the contents of a typical location for getFilesDir() for an app whose user ID is com.foo.bar, use:

adb shell run-as com.foo.bar ls /data/data/com.foo.bar/files

This only works for debuggable apps.

how to get root access on android 2.3 emulator

On the emulator provided with the SDK r10, you can get a root shell executing "adb shell" from your host computer. Once you have such root shell, you cat follow this steps to get a command that can log you as root from the terminal emulator:

# Remount /data to allow executables and setuids on it
mount -o remount,rw /dev/block/mtdblock1 /data

# There's no "cp" command on Android
cat /system/bin/sh > /data/su

# Give setuid permissions to the shell
chmod 7755 /data/su

Now, from the emulator, just run "/data/su" and that's it, you're root.

The normal "/system/xbin/su" command included in the SDK performs internal user id checks, so these commands...

mount -o remount,rw /dev/block/mtdblock0 /system
chmod 7755 /system/xbin/su

...just won't work. There's no way to trick /system/xbin/su to allow the normal user (UID 10018 in my case) to become root.

Please note that dealing with setuid programs can be a security risk (not higher than having a universal "su" command, though). Use this solution at your own risk.



Related Topics



Leave a reply



Submit