Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write files to /system

Tags:

android

io

root

I know this has been asked a lot but never answered. I definetly need to write files to root there is no other solution. I currently use this code but it doesn't show anything new in /system/. I want to copy file from my assets to the /system folder (with it's subdir's)

public void installFiles(View v) {
            try {
        Runtime.getRuntime().exec("su");
    } catch (IOException e) {
        mDebugView.append(e.toString());
    }
    copyPath("system/bin", "/system/bin/");
    copyPath("system/lib", "/system/lib/");
    copyPath("system/etc", "/system/etc/");
    copyPath("system/etc/audio", "/system/etc/audio/");
    copyPath("system/etc/soundimage", "/system/etc/soundimage/");
    copyPath("system/lib/soundfx", "/system/bin/soundfx/");
}

public void copyPath(String from, String to) {
    mDebugView.append("Copying path assets/" + from + " to " + to + "\n");
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list(from);
        for (String filename : files) {
            mDebugView.append(filename + "... \n");
            if (new File(filename).isFile()) {
                mDebugView.append("Copying " + filename + "\n");
                InputStream in = null;
                OutputStream out = null;
                in = assetManager.open(filename);
                out = new FileOutputStream(to);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            }
        }
    } catch (IOException e) {
        Log.e(this.getClass().toString(), e.toString());
        mDebugView.append(e.toString() + "\n");
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    mDebugView.append("..");
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}
like image 206
paulgavrikov Avatar asked Nov 22 '25 13:11

paulgavrikov


2 Answers

Your

Runtime.getRuntime().exec("su");

does nothing. As the process is created and then released. To move files you will need to use the cat binary with su. IE

Runtime.getRuntime().exec("su cat filepath1 > filepath2");

for as many commands as you want to do it would be better to get the process instance of su and then execute all of your move commands at once.

Also note that you may have to mount the system partition as rw as it is probably not r/w by default.

like image 121
Jug6ernaut Avatar answered Nov 25 '25 06:11

Jug6ernaut


You don't see any changes on /system because it's mounted as read-only by default. Please ensure that you remounted it before writing files. Although as others mentioned su should be used with a command.

like image 44
Andrey Ermakov Avatar answered Nov 25 '25 06:11

Andrey Ermakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!