write after su command ROOT not working on android lollipop

861 Views Asked by At

i have a piece of code that works on Android 4.4.2 and below Android versions, but not in lollipop.

I knew that when lollipop came out, a lot off apps that needed ROOT permission stop working, but i don't know how they solved its problem.

This is the code:

System.out.println("Running tool");
Process p = Runtime.getRuntime().exec("su");
os = p.getOutputStream();
os.write((context.getFilesDir().getAbsolutePath() + "/tool " + context.getFilesDir().getAbsolutePath() + "\n").getBytes());
os.write("exit\n".getBytes());
p.waitFor();

All phones tested are ROOT.

In the line:

os.write((context.getFilesDir().getAbsolutePath() + "/tool " + context.getFilesDir().getAbsolutePath() + "\n").getBytes());

Throws the exception:

02-15 23:02:23.917  10130-10130/com.test I/System.out? Running tool
02-15 23:02:24.680  10130-10130/com.test W/System.err? java.io.IOException: write failed: EPIPE (Broken pipe)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at libcore.io.IoBridge.write(IoBridge.java:502)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at java.io.FileOutputStream.write(FileOutputStream.java:186)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at java.io.OutputStream.write(OutputStream.java:82)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at com.secondary_test.MainActivity.onClick(MainActivity.java:86)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at com.secondary_test.Principal.getPass(Principal.java:25)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at com.test.Main.onCreate(Main.java:36)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at android.app.Activity.performCreate(Activity.java:5933)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at android.app.ActivityThread.access$900(ActivityThread.java:147)
02-15 23:02:24.681  10130-10130/com.test W/System.err? at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
02-15 23:02:24.682  10130-10130/com.test W/System.err? at android.os.Handler.dispatchMessage(Handler.java:102)
02-15 23:02:24.682  10130-10130/com.test W/System.err? at android.os.Looper.loop(Looper.java:135)
02-15 23:02:24.682  10130-10130/com.test W/System.err? at android.app.ActivityThread.main(ActivityThread.java:5256)
02-15 23:02:24.682  10130-10130/com.test W/System.err? at java.lang.reflect.Method.invoke(Native Method)
02-15 23:02:24.682  10130-10130/com.test W/System.err? at java.lang.reflect.Method.invoke(Method.java:372)
02-15 23:02:24.682  10130-10130/com.test W/System.err? at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
02-15 23:02:24.682  10130-10130/com.test W/System.err? at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
02-15 23:02:24.682  10130-10130/com.test W/System.err? Caused by: android.system.ErrnoException: write failed: EPIPE (Broken pipe)
02-15 23:02:24.690  10130-10130/com.test W/System.err? at libcore.io.Posix.writeBytes(Native Method)
02-15 23:02:24.691  10130-10130/com.test W/System.err? at libcore.io.Posix.write(Posix.java:223)
02-15 23:02:24.691  10130-10130/com.test W/System.err? at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
02-15 23:02:24.691  10130-10130/com.test W/System.err? at libcore.io.IoBridge.write(IoBridge.java:497)

Anyone has encountered with this issue or know how to solve it?

Thanks to everyone.

1

There are 1 best solutions below

1
On

First Of All Check Whether Your Phone Is Rooted Or Not..?

add roottools.jar file to your projects directory and use it in your code to successfully run superuser commands

Add the jar file from this link: https://code.google.com/p/roottools

https://github.com/Stericson/RootTools/releases

Importing roottools.jar into Android Studio