Mount R/W system in android application to edit Read Only files

隐身守侯 提交于 2019-12-01 11:16:39

I use:

    os.writeBytes("mount -o remount rw /system/\n"); 
    //instead of a comma, I have a space. 
    //When I tried it with a comma, mine didn't work either.

And that allows me to successfully mount.

If you exit right after that, of course it will not work. You have to stay within the same process and use the linux commands to edit the file.

I have no idea how to edit the files, but I suggest googling how to do things in linux terminal, and then putting the proper code in os.writeBytes("CODE_HERE");

Though, as far as the mounting process is concerned, I don't know if that command will work universally. It may just fortunately work on my device.

EDIT:
I now use RootTools: http://code.google.com/p/roottools/downloads/list
And here is the Wiki page:
http://code.google.com/p/roottools/w/list

But I now am using:

RootTools.remount(file, mountType);
//For example:
RootTools.remount("/system/", "rw");

I believe that is universal

Edit: All version of codes below DOES NOT mount system as RW. *Read comments below to see why. Solution of this is not a simple command.

Edit1: I went on Super User apk, Settings tab, and "tapped" at the last item, to update the su binary. With that update, everything below isnt working.

Edit2: started a whole conversation with my self here. Fix for the current latest binary is at the bottom of the post

==================================================================================

Found out how to do it! Second day of efforts, and finally found it!!!!! Tried several things, and answer was to a simple change mode,

what i have done:

First Version Code:(doesnt work)

String[] mountRW = { "su", "-c",
"chmod  777 /system/etc/build.prop"};

String[] mountRO = {"su", "-c",
"chmod  755 /system/etc/build.prop"};

//TODO REMOVE testing purposes          
File file2 = new File("/system/build.prop");

//Make file Read-Write
process = Runtime.getRuntime().exec(mountRW);
process.waitFor();

//TODO REMOVE testing purposes
Log.d("MOUNT RW?", "RW WRITABLE? "+ file2.canWrite());

///////////////////////
// process the file
//////////////////////

// After editing finish,
//make Read Only file again
process = Runtime.getRuntime().exec(mountRO);
process.waitFor();

//TODO REMOVE
Log.d("MOUNT RO?", "RO WRITABLE? "+ file2.canWrite());

I didnt paste some try catch cases. Also i got another problem.. And i solved it in Version 2. THe little problem was, that, i was asking for a specific for a su command, and the user, had to accept SU cmd for RO, SU cmd for RW.. and another time for other stuff in my program. In 2nd version i m using the generic su command, so user has to accept SU privileges only ONE time, and i m using output stream.

Code Version 2(Recomended) (doesnt work):

String mountRW = "chmod  777 /system/build.prop";
String mountRO = "chmod  755 /system/build.prop";

//TODO REMOVE
File file2 = new File("/system/build.prop");

//Make file Read-Write
process = Runtime.getRuntime().exec("su"); //Generic SU Command
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(mountRW + " \n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();


//TODO REMOVE
Log.d("MOUNT RW?", " RW WRITABLE? "+ file2.canWrite());

////////////////////////////
/// mod the file
///////////////////////////

// After editing finish, make Read Only file again
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(mountRO + " \n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();

//TODO REMOVE
Log.d("MOUNT RO?", "RO WRITABLE? "+ file2.canWrite());
  • Both codes require Root on your device.
  • Both versions doesnt include catch cases. (Eclpise will found them for you)
  • Check out your logcat(adb logcat), to see that indeed it works!
  • With latest su binary, this code changes slightly. The change mode command requires 4 digits. 0777 for rw permissions, and 0755 for ro permissions!
  • This code by its own, it does nothing to your device.

Only it mounts built.prop RW, and then mounts it back to RO. Although if you change it, you may brick your device! Take care!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!