是否可以在非root用户手机上运行本机arm二进制文件?

Ani*_*RNG 16 android arm root codesourcery android-ndk

好吧,我一直潜水在低级Android编程(使用CodeSourcery工具链的本机C/C++)的浑水中.我在模拟器上试用了可执行文件,但它确实有效.我想在真实的设备上试一试.所以我插入了我的nexus并将文件推送到文件系统.然后我尝试执行二进制文件,我得到了一个权限错误.无论我如何安装它,或者我发送它的地方都没关系,我不是root,它不会让我执行它.有没有办法在非root手机上运行这样的程序?

sar*_*war 37

在使用Android NDK中包含的工具链来编译二进制文件之后,可以使用典型的Android应用程序将它们打包并将它们作为子进程生成.

您必须在应用程序的assets文件夹中包含所有必需的文件.为了运行它们,您必须让程序将它们从assets文件夹复制到可运行的位置,例如:/data/data/com.yourdomain.yourapp/nativeFolder

你可以这样做:

private static void copyFile(String assetPath, String localPath, Context context) {
    try {
        InputStream in = context.getAssets().open(assetPath);
        FileOutputStream out = new FileOutputStream(localPath);
        int read;
        byte[] buffer = new byte[4096];
        while ((read = in.read(buffer)) > 0) {
            out.write(buffer, 0, read);
        }
        out.close();
        in.close();

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

请记住,assetPath不是绝对的,而是资产/.

IE:"assets/nativeFolder"只是"nativeFolder"

然后运行您的应用程序并阅读其输出,您可以执行以下操作:

  Process nativeApp = Runtime.getRuntime().exec("/data/data/com.yourdomain.yourapp/nativeFolder/application");


            BufferedReader reader = new BufferedReader(new InputStreamReader(nativeApp.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();

            // Waits for the command to finish.
            nativeApp.waitFor();

            String nativeOutput =  output.toString();
Run Code Online (Sandbox Code Playgroud)

  • 确保将新的(复制的)二进制文件标记为可执行文件:`mBin.setExecutable(true);`如果您使用的是Java的`File`对象. (4认同)
  • 是的,Android有一个chmod,但它至少曾经是一个只接受八进制模式的简单模式,而不是大多数桌面unix上可用的文本标志.并且Android的shell错误消息是众所周知的非特定 - 权限被拒绝实际上可能是任何类型的问题. (2认同)