简体   繁体   English

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

[英]Is it possible to run a native arm binary on a non-rooted android phone?

Well, I've been diving in the murky waters of low-level Android programming (native C/C++ using the CodeSourcery toolchain).好吧,我一直在低级 Android 编程(使用 CodeSourcery 工具链的本机 C/C++)的混沌水域潜水。 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.我如何安装它,或者我将它发送到哪里并不重要,我不是 root 并且它不允许我执行它。 Is there any way to run a program like this on a non-rooted phone?有没有办法在非root手机上运行这样的程序?

Update : notice that apps targeting API level 29 (Android 10) and above will not be able to use the trick below, as the OS will restrict the execute permission.更新:请注意,针对 API 级别 29(Android 10)及更高版本的应用程序将无法使用以下技巧,因为操作系统将限制执行权限。 See Behavior changes: apps targeting API 29+ .请参阅行为更改:针对 API 29+ 的应用

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.使用 Android NDK 中包含的工具链编译二进制文件后,可以将它们与典型的 Android 应用程序打包,并将它们作为子进程生成。

You'll have to include all the necessary files within the assets folder of your application.您必须在应用程序的 assets 文件夹中包含所有必要的文件。 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为了运行它们,您必须让程序将它们从资产文件夹复制到可运行的位置,例如:/data/data/com.yourdomain.yourapp/nativeFolder

You can do this like so:你可以这样做:

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

Keep in mind that the assetPath is not absolute but in respect to assets/.请记住,assetPath 不是绝对的,而是相对于 assets/.

IE: "assets/nativeFolder" is just "nativeFolder" IE:“assets/nativeFolder”只是“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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在非 root android 手机上添加属性? - Is it possible to add a property on a non-rooted android phone? Android:在非根目录设备上运行脚本 - Android: run a script on non-rooted device 在非root用户的Android上模拟主机文件 - Emulate hosts file on non-rooted Android 非root用户的Android手机用户可以查看/编辑存储在Realm DB中的数据吗? - Can a non-rooted phone android user see/edit the data stored in Realm DB? 安装错误:INSTALL_FAILED_UID_CHANGED非root用户Android手机 - Installation error: INSTALL_FAILED_UID_CHANGED Non-rooted Android Phone 从非root手机的内部存储中删除文件 - Deleting files from internal storage of a non-rooted phone Android : 使用非 root 的 android 设备捕获 HTTP 请求 - Android : Capturing HTTP Requests with non-rooted android device 非root用户设备上的Android用户空间文件系统驱动程序? - Android userspace filesystem driver on non-rooted device? 以编程方式在Android设备上创建Google帐户(非root用户设备) - Creating a google account on android programatically (non-rooted device) 运行exec()时出错。 命令:工作目录:null环境:null - 如何在非root设备上正确执行Android中的二进制文件? - Error running exec(). Command: Working Directory: null Environment: null - how to execute a binary in Android correctly on a non-rooted device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM