I ve got a problem, executing su with parameters (containing blank space?!). My Command.java looks like this:
public class Command
{
Process process;
public String executeCommand(String command)
{
StringBuffer output = new StringBuffer();
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
}
In my MainActivity it is working for single commands.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView content = (TextView) findViewById(R.id.txt_content);
Command cmd = new Command();
String outp = cmd.executeCommand("su -c 'id'");
Log.d("OOOOOOUT", outp);
content.setText(outp);
}
The result shows, that it is working:
D/OOOOOOUT: uid=0(root) gid=0(root) context=u:r:init:s0
This is also working for the ls command. But when I try to parse an argument with paramters, it will not get executed.
E.g.
String outp = cmd.executeCommand("su -c 'ls /data/data/'");
Log.d("OOOOOOUT", outp);
content.setText(outp);
I also tryed the following:
String outp = cmd.executeCommand("su -c \'ls /data/data/\'");
String outp = cmd.executeCommand("su -c 'ls -l'");
And even more. When I execute this command within the shell directly I get the following output:
shell#hammerhead:/ $ su -c 'ls -l'
drwxr-xr-x root root 1970-06-20 18:01 acct
drwxrwx--- system cache 2016-06-21 22:06 cache
-rwxr-x--- root root 272364 1970-01-01 01:00 charger
dr-x------ root root 1970-06-20 18:01 config
I also tryed using the complete path:
shell#hammerhead:/ $ /system/xbin/su -c 'ls -l'
drwxr-xr-x root root 1970-06-20 18:01 acct
drwxrwx--- system cache 2016-06-21 22:06 cache
Also within the application. I guess its a parsing error. Sometimes I see people adding the "\n" at the end of a command? No clue why. I really appreciate any help within this topic. Thanks!
I think the way to pass arguments to the su command would be to use a string array:
Runtime.getRuntime().exec(new String[]{"su","-c", "ls /data/data/"});
Related
I have a rooted Android 7 phone and I would like to dump unix input event files. Using adb I could do it using the following command:
adb shell getevent -t /dev/input/event7 > recorded_touch_events.txt
This will dump the event7 file into recorded_touch_events.txt. But this only works when the phone is connected by usb cable with the PC. Using Android I can dump files with the following code:
th = new Thread(new Runnable(){
private Process exec;
#Override
public void run() {
try {
exec = Runtime.getRuntime().exec(new String[]{"su","-c","getevent -t /dev/input/event7"});
InputStreamReader is = new InputStreamReader(
exec.getInputStream());
String s;
BufferedReader br = new BufferedReader(is);
while(((s = br.readLine()) != null) && run){
// write line to text file
}
is.close();
exec.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
In this way, I could store every read line in a text file.
Are there other approaches (probably faster ones) for directly dumping the event file?
getevent is used to print input events out in human readable form. For example during interactive debug session. You do not need to use getevent for just dumping or any other computer processing task. Just open and read the input file. The event record format is very simple.
Certainly No, you are doing it right.
I am copying a kernel executable through my app in my package folder in /data/data and trying to run it in android app using Instrumentation
I have tried ProcessBuilder and Runtime.getRuntime().exec as well both are showing Exit Code 1 and command does not run.
My command:
String command = "/data/data/<packageName>/e4crypt add_key -S 0x11112222333344445555666677778888 <<< "+key;
Process proc = Runtime.getRuntime().exec(command);
int i = proc.exitValue();
BufferedReader stdInputs = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
StringBuilder output = new StringBuilder();
while ((line = stdInputs.readLine()) != null) {
output.append(line);
}
How to format the long string? I have tried ProcessBuilder and Runtime.getRuntime().exec and UiDevice.getInstance(getInstrumentation()).executeShellCommand()
The first parameter is "add_key" Even when i run the executable with one parameter it shows Exitcode=1 and it does not run. Same command works fine from command window.
I suspect the command formatting is incorrect.
I am developing a root app for modifying build.prop programatically.
I copied build.prop from /system/build.prop to /sdcard/
but after modifying it I was unable to copy back to root partition
here is my code for copying build.prop from /system/build.prop to /sdcard/
protected void SUCommand()
{
String sSUCommand = "cp /system/build.prop /sdcard/";
final String[] sCommand = {"su","-c",sSUCommand};
Thread thSUProcess = new Thread()
{
public void run()
{
try
{
Process p = Runtime.getRuntime().exec(sCommand);
}
catch(Exception e){}
}
};
thSUProcess.start();
}
I changed String sSUCommand = "cp /system/build.prop /sdcard/"; to String sSUCommand = "cp /sdcard/build.prop /system/"; to copy it to system partition
but didn't worked
I already rooted my phone and running lot of root apps successfully
please tell me the right way to do it
You might want to mount system partition as rw instead of rd so as to do it
https://android.stackexchange.com/questions/25250/how-to-mount-system-in-rw-mode-if-no-custom-recovery
I need to capture all the logs written through my application. I know from Jetllybean OS we can read need only our application log. But when I tried by using command "logcat -d" using exec method by application and I did not get any data.
Please help me on this.
Thanks,
Saravanakumar
This is the example that I was playing around with before that will generate a log text file in local storage:
private static String generateLogcatLogCommond = "logcat -d > /sdcard/IssueReport/log.txt";
public static String generateLogcatLog() throws InterruptedException {
try {
File issueReport = new File(Environment.getExternalStorageDirectory(), "IssueReport");
if (!issueReport.exists())
issueReport.mkdir();
File logFile = new File(issueReport,"log.txt");
logFile.createNewFile();
Process process = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(generateLogcatLogCommond);
logLocation = "/sdcard/IssueReport/log.txt";
Log.d("Client", logLocation);
} catch (IOException e) {
e.printStackTrace();
}
return logLocation;
}
What the above code is doing is using 'sh' to run 'logcat -d' command and save it as a file locally. This will get ALL the logcat log. For you, you can change that to 'logcat -s ""' and it will save all logcat log of your application to a file.
I'm writing an android app, and i need to read 7 SYSTEM files at start up. What would be the most efficient way to do this?
This is my code right now and it's pretty slow
read(file1);
read(file2);
...
read(file7);
...
public static String read(String file) {
String fileContents = "";
try {
String[] args = {"/system/bin/cat", file };
ProcessBuilder cmd = new ProcessBuilder(args);
Process p = cmd.start();
InputStream stream = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
fileContents = reader.readLine();
reader.close();
stream.close();
}
catch (IOException e) {
e.printStackTrace();
}
return fileContents;
}
There has to be some way to cat each file without closing the stream which should significantly speed things up. I tried making a shell script with the 7 cat cmds and reading the output but everything is all mashed together and i can't split the results.
I tried splitting the cat cmds with echo cmds to form a deliminator in the output:
cat file1
echo !
cat file2
echo !
But the deliminators "!" don't show up in the output and i can't figure out why.
Any suggestions?
I guess you could try multiple threads. Probably 7 would do just fine, but you have to check if 7 is not too much for the slowest device you are targeting. You need to leave some CPU for UI thread.