【问题标题】:attach binary to android app将二进制文件附加到 Android 应用程序
【发布时间】:2014-09-14 07:51:59
【问题描述】:

我想把一个 arm 预编译的二进制文件附加到我的私人 android 应用程序上。

如果你不介意,我会告诉你我的应用程序。由于我的 gprs 与我的机器人通信,它只需要修改 iptables 规则。它是 android 的遥控器,如果我拒绝所有流量而不是我的机器人的流量,我会得到更好的结果。

所以我用我的 cyanogenmod 的 11 版 iptables 编译了一个 iptables,但支持 -save -restore,因为我想在完成对我的机器人的控制后恢复规则..

问题是我已经在谷歌搜索了很长时间,我刚刚发现 droidwall 似乎只在“res”顶级目录中创建了一个“原始”目录,一旦安装,我可以看到在 adb shell 上,/data/data 路径中的文件夹“app_bin”。 但是当我安装我的应用程序时,创建的那些目录我什至看不到一些奇怪路径中的二进制文件......真的,这是一个非常罕见的情况吗?我在网络上找不到任何文档...

非常感谢,

希望对您有所帮助。

阿贝尔。

编辑(可能的解决方案)

我已经从 android_firewall 项目中下载了代码,它似乎正在从 apk 资源复制到 bin 目录:

./src/com/jtschohl/androidfirewall/Api.java: 最终字符串 app_iptables = dir + "/iptables_armv5"; ./src/com/jtschohl/androidfirewall/Api.java: // 检查 iptables_armv5 ./src/com/jtschohl/androidfirewall/Api.java:文件文件=新文件(ctx.getDir(“bin”,0),“iptables_armv5”); ./src/com/jtschohl/androidfirewall/Api.java: copyRawFile(ctx, R.raw.iptables_armv5, file, "755");

我要试试。持续关注新闻...

【问题讨论】:

    标签: android binary add precompiled


    【解决方案1】:

    是的,这就是解决方案。文件夹名“app_%{TYPE}”是调用getDir(%{TYPE}, MODE_PRIVATE)函数返回的文件夹名;

    所以下面的代码可以实现所需的功能:

       private static void copyRawFile(Context ctx, int resid, File file, String mode) throws IOException, InterruptedException {
               final String abspath = file.getAbsolutePath();
               // Write the iptables binary
               final FileOutputStream out = new FileOutputStream(file);
               final InputStream is = ctx.getResources().openRawResource(resid);
               byte buf[] = new byte[1024];
               int len;
               while ((len = is.read(buf)) > 0) {
                   out.write(buf, 0, len);
               }
               out.close();
               is.close();
               // Change the permissions
               Runtime.getRuntime().exec("chmod " + mode + " " + abspath).waitFor();
           }
    
           private boolean assertBinaries(Context ctx, boolean showErrors) {
               try {
                   File file = new File(ctx.getDir("bin", MODE_PRIVATE), "xtables_multi");
                   if (!file.exists()) {
                       copyRawFile(ctx, R.raw.xtables_multi, file, "755");
                   }
                   file = new File(ctx.getDir("bin", MODE_PRIVATE), "iptables_new.sh");
                   if (!file.exists()) {
                       copyRawFile(ctx, R.raw.iptables_new, file, "755");
                   }
               } catch(Exception e) {
                   if (showErrors) {
                       alert(ctx, R.string.error_assertbinaries + " " + e);
                   }
                   return false;
               }
            return true;
           }
    
           public static void alert(Context ctx, CharSequence msg) {
               if (ctx != null) {
                   Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
               }
           }
    

    希望对你有帮助,

    干杯;亚伯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-11
      • 2020-10-07
      • 2011-10-13
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      相关资源
      最近更新 更多