[英]How do I fix java.io.IOException: write failed: EPIPE (Broken pipe) when I am rooted?
I am running my code on a rooted Nexus, Android 6.0.1, with my apk as a system app (ie android:sharedUserId="android.uid.system"
in system/priv-app
. Since it is a system app, and I am rooted, I do not understand why I cannot run my su
commands. Let me iterate, I can run su
and rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml
through an adb shell
successfully.我在根 Nexus Android 6.0.1 上运行我的代码,我的 apk 作为系统应用程序(即
android:sharedUserId="android.uid.system"
in system/priv-app
。因为它是一个系统应用程序,并且I am rooted, I do not understand why I cannot run my su
commands. Let me iterate, I can run su
and rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml
through an adb shell
成功地。
This is what I am trying to run:这就是我想要运行的:
public void reset_GAID_3() throws Exception {
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
outputStream.close();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}
}
There error is occuring at: outputStream.writeBytes("rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml\n");
出现错误:
outputStream.writeBytes("rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml\n");
Here is the stacktrace:这是堆栈跟踪:
Caused by: android.system.ErrnoException: write failed: EPIPE (Broken pipe)
at libcore.io.Posix.writeBytes(Native Method)
at libcore.io.Posix.write(Posix.java:271)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
at libcore.io.IoBridge.write(IoBridge.java:493)
at java.io.FileOutputStream.write(FileOutputStream.java:186)
at java.io.OutputStream.write(OutputStream.java:82)
at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)
at com.example.adtry3.MainActivity.reset_GAID_3(MainActivity.java:363)
at com.example.adtry3.MainActivity.reset_GAID_button(MainActivity.java:336)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:5204)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View$PerformClick.run(View.java:21156)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
My understanding is that I have an error because I am not executing the rm
in a superuser instance, but I thought that I am.我的理解是我有一个错误,因为我没有在超级用户实例中执行
rm
,但我认为我是。 I attempted to change it to a single command:我试图将其更改为单个命令:
su -c 'rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml'
This did not work.这没有用。 Any ideas where I am going wrong?
有什么想法我哪里出错了吗? Thanks.
谢谢。
Not quite the best answer in the world, but I fixed it by removing the su
instance and running it through a ProcessBuilder
instead.不是世界上最好的答案,但我通过删除
su
实例并通过ProcessBuilder
运行它来修复它。
public void reset_GAID_3() throws Exception {
ProcessBuilder su = new ProcessBuilder("rm", "-f", "/data/data/com.google.android.gms/shared_prefs/adid_settings.xml");
try{
Process pc = su.start();
System.out.println("Ran SU cmd");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed SU cmd");
}
}
I am sure there was a problem with how the code was being executed after the su
command, but this is a way around it.我确信在
su
命令之后执行代码的方式存在问题,但这是一种解决方法。 Since the su
is what seemed to be causing the problem, I simply removed it.由于
su
似乎是导致问题的原因,我只是将其删除。 Removing it means I could use a simple ProcessBuilder
to get what I needed done.删除它意味着我可以使用一个简单的
ProcessBuilder
来完成我需要的工作。
Why did this still work?为什么这仍然有效? Since this apk is located in
system/priv-app
, and it has android:sharedUserId="android.uid.system"
in the Manifest, this means it's already operating with root permissions, so it would be redundant to use su
.由于此 apk 位于
system/priv-app
中,并且在 Manifest 中有android:sharedUserId="android.uid.system"
,这意味着它已经以 root 权限运行,因此使用su
将是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.