I'm new to Android. I'm trying to run a shell command to rename a file in the system. I've got root access to it.
The shell command:
$ su
# mount -o remount,rw /system
# mv system/file.old system/file.new
I have tried this but doesn't work:
public void but1(View view) throws IOException{
Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("mount -o remount,rw /system");
process = Runtime.getRuntime().exec("mv /system/file.old system/file.new");
}
You can run more then one command using the same process, by writing the commands in the process's OuputStream
. This way the commands will run in the same context that the su
command is running. Something like:
Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("mount -o remount,rw /system\n");
out.writeBytes("mv /system/file.old system/file.new\n");
out.writeBytes("exit\n");
out.flush();
process.waitFor();
You need each command to be in the same process as su
, since the switch to root doesn't apply to your application, it applies to su
, which completes before you get to mount
.
Instead, try two exec's:
...exec("su -c mount -o remount,rw /system");
...exec("su -c mv /system/file.old system/file.new");
Also, be aware that I've seen some systems where the mount -o remount,rw /system
would fail however mount -o remount,rw /dev/<proper path here> /system
would succeed. The "proper path here" is different from one manufacturer to the next, but it can be programatically gathered.