Skip to content

Android: Requesting root access in your app

by mat on January 17th, 2010

This snippet shows how root access can be requested inside an application in order to write a file into a place we do not have permission to access usually. Requesting root access will only work if your phone allows it, or it has been ‘rooted’ (hacked to allow superuser permissions).

Process p; 
try { 
   // Preform su to get root privledges
   p = Runtime.getRuntime().exec("su"); 
   
   // Attempt to write a file to a root-only 
   DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
   os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
   
   // Close the terminal
   os.writeBytes("exit\n"); 
   os.flush(); 
   try { 
      p.waitFor(); 
           if (p.exitValue() != 255) { 
        	  // TODO Code to run on success
              toastMessage("root");
           } 
           else { 
        	   // TODO Code to run on unsuccessful
        	   toastMessage("not root");    
           } 
   } catch (InterruptedException e) { 
      // TODO Code to run in interrupted exception
	   toastMessage("not root"); 
   } 
} catch (IOException e) { 
   // TODO Code to run in input/output exception
	toastMessage("not root"); 
}

Where my “toastMessage” is just a function which creates a toast to display on the screen. On phones with superuser permissions installed (root access) this will display a dialog asking the user to allow or deny the application permission to have root access:

android root access dialog

android request root access dialog

Ref
anddev.org

18 Comments
  1. elviento0891 permalink

    This was a great help, I appreciate this post!

  2. hi~!
    ‘root access dialog’ is made by you?
    or, made by android system?
    if app requires root permissions
    then automatically show up ‘root access dialog’?
    IOException raised when write a file into a place we do not have permission(ex : /system/sd/)
    and app finished..
    not show ‘root access dialog’
    i want make app writing a file into /system/
    help me..

  3. The root access dialog is part of the android system, but its is only avaliable if you have “rooted” the phone it is running on otherwise permission is simply denied.

  4. Thanks,
    ‘rooted’ gets using shell command ‘mount’ ?

  5. No, I think your getting confused. Mount has nothing to do with this.

    A routed phone is one that has been hacked to allow root access. Your application will only work on phones that have been rooted by the user. This is obvious for security reasons.

  6. Rob permalink

    How can I use this to access another applications database?

  7. WeldFire permalink

    Hello Mat,
    You say you can use this method to “write a file into a place we do not have permission to access usually” do you have to try and copy files using os.writebytes()? or just a FileOutputStream?

  8. @WeldFire Sorry I missed your comment, I’ve been quite busy lately. I don’t know an incredible amount about this as I never really ended up using it, but I imagine there is a way to use FileOutputStream, maybe even if you just need to turn it into a byte array (which you could then use with os.writebytes())

    Deleted your comment on other post as I’ve answered here

  9. Rocker permalink

    Thank you Mat. I tried to modify the hosts file but not success.

    String cmd = String.format(“echo %s >> /etc/hosts\n”, entry);
    os.writeBytes(cmd);

    Do you have any idea?
    Thanks,

  10. juwon permalink

    I have an huawei ascending 2 and it says I don’t have root access what app can I install dat. allows this. permission my email is juwon377@Gmail.com contact me if u have any information

  11. Jonathan permalink

    Is there also a way to make the app detect when superuser permissions have been denied? Currently this code doesn’t seem to test that possibility.

  12. Nice info but how to make your jni (C Function) run as root?

    Have something like this in my code.

    Process p;
    try {
    // Preform su to get root privledges
    p = Runtime.getRuntime().exec(“su”);

    // Attempt to write a file to a root-only
    DataOutputStream os = new DataOutputStream( p.getOutputStream() );
    os.writeBytes( “echo \”Do I have root?\” > /data/temporary.txt\n” );

    // Close the terminal
    os.writeBytes(“exit\n”);
    os.flush();
    try {
    p.waitFor();
    if (p.exitValue() != 255) {
    System.loadLibrary(“hello-jni”);
    // Code to run on success
    tv.setText( stringFromJNI() ); // This function returns with permission denied error.
    }
    else {
    // Code to run on unsuccessful
    tv.setText(“not root”);
    }
    } catch (InterruptedException e) {
    // Code to run in interrupted exception
    tv.setText(“not root”);
    }
    } catch (IOException e) {
    // Code to run in input/output exception
    tv.setText(“not root”);
    } // end SU block

    I have the file created in /data folder but I guess that echo runs in a child shell whereas my jni lib gets loaded an android app env. How to make sure that the jni code runs in super user mode as well?

    Thanks in advance.

  13. Nitish permalink

    how to use it frnds!!

  14. Eduardo permalink

    Hi BTR Naidu,

    Did you find out an answer to your question?

    Thanks.

    Eduardo

  15. Krishnamurthy permalink

    I am trying to run ubuntu-linux in andriiod but at command propmpt when i give “su” command the error message “permission denied” please help

Trackbacks & Pingbacks

  1. Diario de un androide » Como hacer que una aplicación solicite permisos de superusuario
  2. Android: howto request root access? « 神啊,我是个罪人
  3. get root permissions through Android SDK | Coding and Programing

Leave a Reply

Note: I am currently writing my thesis so probably wont have time to reply to your comment
Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS