是否可以在非 root 的 android 手机上运行本机 arm 二进制文件? [英] Is it possible to run a native arm binary on a non-rooted android phone?

查看:43
本文介绍了是否可以在非 root 的 android 手机上运行本机 arm 二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在低级 Android 编程(使用 CodeSourcery 工具链的本机 C/C++)的浑水摸鱼.我在模拟器上试用了这个可执行文件,并且成功了.我想在真机上试一试.所以我插入了我的 nexus 并将文件推送到文件系统.然后我尝试执行二进制文件,但出现权限错误.我如何安装它或将它发送到哪里真的无关紧要,我不是 root 并且它不允许我执行它.有没有办法在非root手机上运行这样的程序?

Well, I've been diving in the murky waters of low-level Android programming (native C/C++ using the CodeSourcery toolchain). I tried out the executable on an emulator and it worked. I'd like to try it out on a real device. So I plugged in my nexus and pushed the files on to the filesystem. Then I tried to execute the binary, and I got a permission error. It really doesn't matter how I mount it, or where I send it, I'm not root and it's not letting me execute it. Is there any way to run a program like this on a non-rooted phone?

推荐答案

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

After using the toolchain included in the Android NDK to compile your binaries, it is possible to package them with a typical Android app and have them spawn as subprocesses.

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

You'll have to include all the necessary files within the assets folder of your application. In order to run them, you have to have the program copy them from the assets folder to a runnable location like: /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);
    }
}

请记住,assetPath 不是绝对的,而是与 assets/相关的.

Keep in mind that the assetPath is not absolute but in respect to assets/.

IE:assets/nativeFolder"就是nativeFolder"

IE: "assets/nativeFolder" is just "nativeFolder"

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

To then run your application and read its output you could do something like this:

  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();

这篇关于是否可以在非 root 的 android 手机上运行本机 arm 二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆