【问题标题】:Run binary from with root Android Application从根 Android 应用程序运行二进制文件
【发布时间】:2011-09-20 18:39:10
【问题描述】:

我想在/dev/local 运行一个名为 native 的二进制文件(我通过 adb 推送它),并具有 root 权限。 为了实现这一点,我编写了以下代码:

    try {
        root=Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(root.getOutputStream());
        DataInputStream osRes = new DataInputStream(root.getInputStream());
        os.writeBytes("/data/local/native\n");
        os.flush();
        TextView output=(TextView)findViewById(R.id.textview);
        output.append(osRes.readLine());
        root.waitFor();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, e.toString() , Toast.LENGTH_SHORT).show();
    }

这给了我一个NullPointerException。所以,我尝试将其更改为:

    try{
        root=Runtime.getRuntime().exec("su");
        root.waitFor();
        DataOutputStream os = new DataOutputStream(root.getOutputStream());
        DataInputStream osRes = new DataInputStream(root.getInputStream());
        os.writeBytes("/data/local/native\n");
        os.flush();
        TextView output=(TextView)findViewById(R.id.textview);
        output.append(osRes.readLine());
        root.waitFor();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, e.toString() , Toast.LENGTH_SHORT).show();
    }

这样我得到了 BrokenPipe 错误。

请帮助我,我想以 root 权限运行二进制文件,有没有其他方法或者我做错了什么?

【问题讨论】:

    标签: java android unix android-ndk native


    【解决方案1】:

    尝试运行su -c /path/to/executable

    【讨论】:

      【解决方案2】:

      我已经回答过很多次,也帮助过其他人。请试试这个:

      Usage: Run(new String[]{"/system/bin/sh", "-c", "su"});
      
      public String Run(String[] cmd) {
          StringBuffer theRun = null;
          try {
              Process process = Runtime.getRuntime().exec(cmd);
      
              BufferedReader reader = new BufferedReader(
                      new InputStreamReader(process.getInputStream()));
              int read;
              char[] buffer = new char[4096];
              StringBuffer output = new StringBuffer();
              while ((read = reader.read(buffer)) > 0) {
                  theRun = output.append(buffer, 0, read);//output
              }
              reader.close();
              process.waitFor();
      
          } catch (IOException e) {
              throw new RuntimeException(e);
          } catch (InterruptedException e) {
              throw new RuntimeException(e);
          }
              return theRun.toString();//return output
      }
      

      此外,此链接也很有帮助:http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/

      【讨论】:

        【解决方案3】:

        我知道我的回答迟了,但如果你仍然面临这个问题,这就是我所做的。我还必须运行具有 root 权限的二进制文件。我尝试了几件事,例如将二进制文件放在 /system/app 文件夹中,系统应用程序从该文件夹运行并因此具有 root 权限。但它没有用。因此,我不得不修改 su.c 文件并重新编译内核,如THIS 站点中所述。如果重新编译内核不是一个选项,您可以尝试在 roottool(如busybox)中进行相同的更改并重新编译。您现在可以使用“busybox su -c”而不仅仅是“su -c”。

        【讨论】:

        • 除了间接地提出一个事实,即库存的 Android 'su' 不能用于预期的目的,这是一堆应该删除的错误猜测。重新编译内核根本没有任何作用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-17
        相关资源
        最近更新 更多