Home > Net >  android coding java excute su command android not work
android coding java excute su command android not work

Time:11-06

i have a device rooted by magisk. i want to make an app to excute "su ls data/data". But when i run app it only returned two packages eg: com.google.android.gms, com.nghiatd.nghiaclicker( this is package name of my app). But when i excute that command on terminal emulator app it returned full package name of apps in data/data folder. i have added read,write,ACCESS_SUPERUSER permission in manifest. magisk granted permission.i have tested on android 10. that funtion returned full package name of all apps. anyone know why android 12 does not return full ? This is funtion:

`


    enter code here

public static String executeCommandSU( String command)
    {
            try {
                Process process = new ProcessBuilder().command("su").start();
                process.getOutputStream().write((command   "\n").getBytes("ASCII"));
                //process.getOutputStream().write(("ls data"   "\n").getBytes("ASCII"));
                process.getOutputStream().flush();
                SystemClock.sleep(3000);
                StringBuffer sb = new StringBuffer();
                BufferedReader bre = new BufferedReader(
                        new InputStreamReader(process.getErrorStream()));
                while (bre.ready()) {
                    sb.append(bre.readLine());
                }
                String s = sb.toString();
                if (!s.replaceAll(" ", "").equalsIgnoreCase("")) {
                    Log.e("SystemHelper", "Error with command: "   s);
                    return s;
                }
                sb = new StringBuffer();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));
                while (br.ready()) {
                    sb.append(br.readLine() "\n");
                }
                s = sb.toString();
                if (!s.replaceAll(" ", "").equalsIgnoreCase("")) {
                    Log.e("`SystemHelper`", "Output from command: "   s);
                    return s;
                }
                return s;
            } catch (IOException e) {
                 Log.e("SystemHelper", "Error executing: "   command, e);
            }
        return "";
    }`

`

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestPermission();
    //String s=executeCommand(new String[]{"su","-c","ls data/data"});
    String s=executeCommandSU("ls data/data");
    Log.i("vvvvv",s);
    Toast.makeText(getApplicationContext(),"aaaaaaa: ",Toast.LENGTH_LONG).show();
}

CodePudding user response:

You are using ready() incorrectly. Look up the docs to see why. Change:

 while (br.ready()) {
     sb.append(br.readLine() "\n");
 }

to

 while (true) {
     String l = br.readLine();
     if (l == null) // EOF
         break;
     sb.append(l "\n");
 }

Do the same for the error stream.

Also to make su terminate change:

process.getOutputStream().write((command   "\n").getBytes("ASCII"));

to:

process.getOutputStream().write((command   "\nexit\n").getBytes("ASCII"));

Haven't tested it. Hope it works.

CodePudding user response:

edit targetSdk 29 in build.gradle file will solve problem

  • Related