HippoMan
HippoMan

Reputation: 2328

Android: Getting "su" to work in a rooted AVD in the same way it works on a real rooted device?

I'm writing an app for rooted devices that needs to get into superuser mode. I know how to do this in a way which works on actual rooted devices, but when I test it in the AVD, which also is rooted, the "su" command does not behave in the same way that it does on a real device. It will not allow a non-root user to "su" to root ... the AVD only allows root to su to a non-root user.

Does anyone know how to get the following code fragment to work within an Android emulator?

        // ... etc. ...

        Process process = null;

        try {
            // This works on a rooted device and fails within
            // the rooted AVD ...
            process = new ProcessBuilder("/system/xbin/su").start();
        }
        catch (Throwable t) {
            t.printStackTrace();
            return;
        }

        DataOutputStream  writer = new DataOutputStream(process.getOutputStream());
        InputStreamReader stream = new InputStreamReader(process.getInputStream());
        BufferedReader    reader = new BufferedReader(stream);

        try {
            writer.writeBytes("exec /system/bin/mount -o remount,rw /system\n");
            // Just in case the "exec" fails, above ...
            writer.writeBytes("exit 1\n");
            writer.close();
        }
        catch (Throwable t) {
            t.printStackTrace();
            return;
        }
        finally {
            try {
                reader.close();
            }
            catch (Throwable t) {
                // do nothing
            }
            try {
                stream.close();
            }
            catch (Throwable t) {
                // do nothing
            }
        }

        // ... etc. ...

This fails because the app is not running as root, but in the AVD, /system/xbin/su command only works if it is being invoked by the root user. The following adb shell dialog illustrates that point (I'm connecting to the running AVD) ...

% adb shell
root@generic:/ # ### we are now running as root
root@generic:/ # /system/xbin/su u0_a70
root@generic:/ $ ### this worked because "su" is invoked as root
root@generic:/ $ ### we are now running as user u0_a70
root@generic:/ $ /system/xbin/su root
su: uid 10070 not allowed to su
1|root@generic:/ $ ### this didn't work as a non-root user

Does anyone know how to get the "su" command to work in the rooted AVD in the same way that "su" works in a rooted device?

Thanks in advance.

PS: This is for Android 4.4.x

Upvotes: 0

Views: 1431

Answers (1)

dario.budimir
dario.budimir

Reputation: 161

Don't use stock Android emulator, use GenyMotion and follow this tutorial XDA link

hope that this will help you :)

Upvotes: 1

Related Questions