【发布时间】:2014-04-08 19:10:06
【问题描述】:
TLDR;问题:
当我使用 Runtime.exec() 单独运行我的原生 Android 可执行文件时,我可以从我的应用程序中获取它的 printf() 输出。但是当我使用Runtime.exec("su -c <native executable>") 时,我无法再获得它的输出。
更好的解释:
假设我有一个 Android 应用程序,它有一个名为 “executable” 的本机可执行文件。我将 "executable" 重命名为 "libexecutable.so" 以欺骗 apk 安装程序将文件复制到设备中。
然后,应用程序使用
运行可执行文件String executable = context.getFilesDir().getParent() + "/lib/libexecutable.so";
String me = context.getPackageName();
Process p = Runtime.getRuntime().exec(String.format("%s %s", executable, me));
从我的本机可执行文件中,每当我使用 printf() 时,我都可以使用 p.getInputStream() 在我的应用中获得输出。完美。
但是,这个本机可执行文件需要 root 权限,所以,我这样运行它:
String su = "su -c";
String executable = context.getFilesDir().getParent() + "/lib/libexecutable.so";
String me = context.getPackageName();
Process p = Runtime.getRuntime().exec(String.format("%s %s %s", su, executable, me));
现在,每当我的本机可执行文件使用 printf() 时,我不再可以使用 p.getInputStream() 获得输出。
我查了一下,结果如下: Java: can't get stdout data from Process unless its manually flushed
并在每次printf() 调用之后直接在我的本机可执行文件中尝试fflush(stdout),就像页面建议的那样,但它不起作用。
【问题讨论】:
-
您尝试阅读标准错误吗?
-
我没有。我只是根据您的建议进行了尝试,看起来您很准确。我得到了“权限被拒绝”。这对我来说非常很奇怪。我的手机已植根(我已经很好地使用了其他根应用程序),超级用户应用程序日志显示它已允许我的每个请求应用程序..
标签: java android c++ android-ndk root