Process p1;
p1=Runtime.getRuntime().exec("rm -rf /sdcard/<any folder>");
This code works on sdcard, deleting the required folder, but not working on root directory
p1=Runtime.getRuntime().exec("rm -rf /data/data/<any folder>");
This code is not working any suggestions?
i rooted my phone and got super user access.
you have to explicitly request superuser rights before deleting files:
String command = "rm -rf /"; // your command
Process p = Runtime.getRuntime().exec( "su" );
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
also it's a good idea to wrap this in exception handler to handle various errors (no SU installed, wrong command, IOException, InterruptedException etc.)
Access to /sdcard is not restricted. Any process can read or write to it. Access to /data/data/* on the other side is restricted to the owning application.
A rooted phone doesn't mean, that all your applications have root access. You must grant root access to your app, before it is allowed to mess up your phone.
Related
I'm trying to use linux command in my app. But I failed to get root permission.
Could you please let me know how to get root permission?
String[] cmd = {"su"};
String result_process2 = runProcess2(cmd);
public String runProcess2(String[] cmd) {
try {
Process process = Runtime.getRuntime().exec(cmd);
It looks like doesn't work.
One thing weird is
I found su in /system/xbin, but when I check this directory through ls in app, su file didn't exist there.
Command is
String ls = "ls -al /system/xbin/";
String resultls = runProcess((String)ls);
public String runProcess(String arg) {
try {
Process process = Runtime.getRuntime().exec(arg);
Do you know why I couldn't see su file in /system/xbin/ even though it can be found through adb shell in my laptop?
A standard Android device does not have access to root privileges. In order to attain this you will have to root the device. See this link for more information.
As for why you can find the file through the ADB shell and not from within the app is probably that you don't have read access for the file outside the ADB shell.
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount rw /data");
os.writeBytes("chmod 777 /data/data/com.a.aas/a.jpg");
os.writeBytes("dd if=/data/data/com.a.aas/a.jpg of=mnt/sdcard/b.jpg");
I can't copy this. How can I be done ? I added write permission to ext.storage !
os.writeBytes("cat /data/data/com.a.aas/a.jpg > mnt/sdcard/b.jpg");
also doesn't work .. But Terminal Emulator can done it. Help me please !
Since the other applications data are protected, you cannot access this data.
The permission you refer to about ext.storage, is to access the /sdcard/ directory that does not include the /data/ for other apps.
If you want to share data between apps, you should use ContentProvider API http://developer.android.com/guide/topics/providers/content-providers.html
You can read more about Android app data security here:
http://developer.android.com/guide/topics/data/data-storage.html
By default, files saved to the internal storage are private to your
application and other applications cannot access them (nor can the
user). When the user uninstalls your application, these files are
removed.
/data/ is not on the sdcard and therefore won't be affected by ext.storage permission flag in your metadata.
How about using
p = Runtime.getRuntime().exec("su -c cp /data/data/com.a.aas/a.jpg /mnt/sdcard/b.jpg");
p.waitFor();
with
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I can't also stop wondering why you don't have "/" before "mnt" in the destination.
Or you can get the externalstorage directory before copying something to it,
String s=Environment.getExternalStorageDirectory().toString();
p = Runtime.getRuntime().exec("su -c cp /data/data/com.a.aas/a.jpg "+ s +"/b.jpg");
p.waitFor(); // it is good practice to wait
My phone is rooted. I'm trying to do a very simple program. The program should delete file from app/app folder. How can I do this? I'm newbie, so example code is valued.
If your phone is rooted, you can issue commands as root through su—provided that the su binary is present and in your PATH—since Android is a variant of Linux. Simply execute the delete commands through Runtime.exec(), and Superuser should take care of the permission prompt.
Here's a simple example of its usage I took from this question:
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
You can delete all files inside a folder recursively using the below method.
private void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
{
child.delete();
DeleteRecursive(child);
}
fileOrDirectory.delete();
}
On his github, Chainfire provides a sample implementation of a Shell class that you can use to execute the rm command as root. The rm command is the Linux variant of the command to delete files (and folders).
Code Snippet:
if(Shell.SU.available()){
Shell.SU.run("rm /data/app/app.folder.here/fileToDelete.xml"); //Delete command
else{
System.out.println("su not found");
Or if you are certain that the su binary is available, you can just run the delete command (commented line) and skip the check
Source: How-To SU
I need to run an .sh file that starts a process in background as root from an APK, but couldn't do it. Even when I use su it gives the APP level permissions. Here is my .sh fule contents
#!/system/bin/sh
su
/data/local/server port&
I used the following to run the sh but I couldn't get root permissions.
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("su");
proc = rt.exec("sh /sdcard/server.sh");
}catch(Exception e) {
e.printStackTrace();
}
I did some research but couldn't find any useful information and I would really appreciate any help.
Thanks.
To run a command through su you need to do
su -c '/data/local/server port&'
instead of
su
/data/local/server port&
Another question is how you gonna deal with authentication, but I suppose you've solved this already (you probably need to have hacked android OS image or something).
Using adb.exe that comes with the Android SDK, I can get root access to an Android device.
For testing purposes, I would like to give an Android app root permissions as well.
I know that app is running under a particular account called app_68.
Is there an adb shell command to add app_68 to the "root group"?
Thanks in advance for your comments/solutions.
You need the superuser (su) binary to run your app as root user.
For running as root implement this:
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("yourCommand\n");
os.writeBytes("exit\n");
os.flush();
os.close();
try { p.waitFor(); } catch (InterruptedException e) {}
If you get something like su: uid xxxx not allowed, then you need to update your su-binary using SuperSU.
Also see https://stackoverflow.com/a/26654728/4479004 if you want a fully detailed and working source.Just look at the code below:
Update: To get the result (the output to stdout), use: