0

I want to change the value of variable in xml. The value is based on another file which read by editXml.sh. So I need to run the editXml.sh before app is compiled.

I try to run the script in MainActivity with code as follows:

onCreate() {
......
execScript();
}

execScript(){
    try{
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("sh /.../editXml.sh");
    } catch(Throwable t)
    {
        t.printStackTrace();
    }

}

The editXml.sh is in my local, but the code doesn't work when I run app in Android studio.(Works on local) Should I put my script in the app? And which part of the app? Any suggestion?

3 Answers 3

1

Try this. I've tested this code and it works. Let's you script named script.sh.

  1. Put file script.sh to you project's /res/raw folder.
  2. Use code below.
  3. Build apk. Unpack apk (this is usual zip-archive) and make sure file /res/raw/script.sh exists there.
  4. Install apk on device and start it.

        public static void executeCommandAndGetOutput(String command){
            BufferedReader reader = null;
            String result = "";
            try {
                Process p = Runtime.getRuntime().exec(command);
                reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line = null;
                while ((line = reader.readLine()) != null){
                    result += line + "\n";
                }
                p.waitFor();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(reader != null)
                    try {
                        reader.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
            Log.i("Test", result);
        } 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            String pathToScript = getDir("my_scripts", 0).getAbsolutePath() + File.separator + "script.sh";
    
            // Unpacking script to local filesystem
            InputStream in = getResources().openRawResource(R.raw.script);
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(pathToScript);
                byte[] buff = new byte[1024];
                int read = 0;
                while ((read = in.read(buff)) > 0) {
                out.write(buff, 0, read);
                }
            }
            catch(Exception e){
            }
            finally {
                try {
                    in.close();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            // Make script executable
            executeCommand("chmod 775 " + pathToScript);
            // Execute script
            executeCommand("sh " + pathToScript);
        }
    
    public static String getSystemCommandOutput(String command){
        BufferedReader reader = null;
        String result = "";
    
        try {
            Process p = Runtime.getRuntime().exec(command);
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
            String line = null;
            while ((line = reader.readLine()) != null){
                result += line + "\n";
            }
    
            p.waitFor();
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeQuietly(reader);
        }
    
        return result;    
    } 
    
Sign up to request clarification or add additional context in comments.

19 Comments

hey, I used your code and I can get the unpack path: /data/data/cc.softwarefactory.lokki.android/app_my_scripts/editxml.sh. But why the result is null. It should be the contents of script file, right? The output of console picture is in the next answer.
bty, I need to execute the script to change data value before the code settled finally. But after unpacking from "apk", the script execute and change the value. It means the code is recompiled?
Did you mean you need to change value before build apk? Which value? At /res/values?
I need to read value from /res/values like this: String value = getResources().getString(R.string.LogoutButton) in mainActivity. And the value is changed by script file.
The value is changed by script before reading. So the script need to be executed first.
|
0

You can put script at raw resources and then unpack it to Context.getDir(...) folder and run from there with absolute path.

Also you need to run "chmod 775" (chmod +x) for this file before executing.

Example about copying a file from raw: Copying raw file into SDCard? You can copy to app folder (Context.getDir(...)) instead of sdcard

10 Comments

thanks for your answer. Let's say I run the script through this way: File script = Context.getDir("editXml", 0); Process proc = rt.exec("sh script"); the script can't be recognized in exec(). How to solve this?
Try: exec("sh " + Context.getDir("editXml", 0) + "\script.sh"). If error occured - do chmod (see answer)
getDir() returns FOLDER. You need to put script there and run it with FULL PATH.
Thanks for your patient answers. I add "chmod +x", and it works!
It works when run script from studio terminal, but doesn't work when run through code: Process proc2 = rt.exec("sh " + this.getDir("raw",0) + "/editxml.sh"). I put editxml.sh into raw folder. What's the problem it maybe? It didn't say any error message when debug.
|
0

console output:

     58:07.979    8935-8935/cc.softwarefactory.lokki.android   E/MainActivity﹕ onCreate
     12:58:08.234    8935-8935/cc.softwarefactory.lokki.android E/MainActivity﹕ PATH: /data/data/cc.softwarefactory.lokki.android/app_my_scripts/editxml.sh
     12:58:08.258    8935-8935/cc.softwarefactory.lokki.android E/MainActivity﹕ result:
     12:58:08.287    8935-8935/cc.softwarefactory.lokki.android E/MainActivity﹕ result: not found

I changed

 Log.i("Test", result) to Log.e(TAG,"result: " + result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.