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:
Ref
anddev.org
This was a great help, I appreciate this post!
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..
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.
Thanks,
‘rooted’ gets using shell command ‘mount’ ?
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.
How can I use this to access another applications database?
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?
@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
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,
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 [email protected] contact me if u have any information
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.
[…] Inspirado en el código de ésta web. […]
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.
[…] https://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/ Like this:LikeBe the first to like this post. Comments RSS feed […]
how to use it frnds!!
Hi BTR Naidu,
Did you find out an answer to your question?
Thanks.
Eduardo
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
[…] either Allow or Block it from root access. This approach might not work if the user is not rooted. Here is a way you can test […]
can we acces “/data/data/com.whatsapp/databases/msgstore.db” file
Great Post!
I use it to change the rights to a file and then read/write in normal way:
os.writeBytes(“chmod 777 ” + PROPERTY_FILE + “\n”);
perfect, that’s what i need
Great full code it successfully access the file .thank you
Here is tutorial on creating root app using library:
https://themakeinfo.com/2015/05/building-android-root-app/
I know this is an old post. But I am following the sample in my own app. Can you explain the result code 255? Is that some special error code if the su binary fails? In my simple tests, it seems that the result code I am getting is the result of the command run by su. These are generally 0 for success and non-zero (I am getting 1) for non-success. So I am confused by your treatment of any result != 255 as a success. Thanks for the post and your elaboration.
Thanks, it is a very useful code!!
[…] de ese Proceso tiene privilegios de root: Process p = Runtime.getRuntime().exec("su"); Ver este blog post para el ejemplo […]
[…] (摘自 http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/) […]
[…] either Allow or Block it from root access. This approach might not work if the user is not rooted. Here is a way you can test […]