簡體   English   中英

是否可以在非 root 的 android 手機上運行本機 arm 二進制文件?

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

好吧,我一直在低級 Android 編程(使用 CodeSourcery 工具鏈的本機 C/C++)的混沌水域潛水。 我在模擬器上試用了可執行文件,它工作正常。 我想在真實設備上試用一下。 所以我插入了我的關系並將文件推送到文件系統。 然后我嘗試執行二進制文件,但出現權限錯誤。 我如何安裝它,或者我將它發送到哪里並不重要,我不是 root 並且它不允許我執行它。 有沒有辦法在非root手機上運行這樣的程序?

更新:請注意,針對 API 級別 29(Android 10)及更高版本的應用程序將無法使用以下技巧,因為操作系統將限制執行權限。 請參閱行為更改:針對 API 29+ 的應用

使用 Android NDK 中包含的工具鏈編譯二進制文件后,可以將它們與典型的 Android 應用程序打包,並將它們作為子進程生成。

您必須在應用程序的 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);
    }
}

請記住,assetPath 不是絕對的,而是相對於 assets/.

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM