如何将可执行文件打包至APK并运行(转)

原文链接:http://www.lupaworld.com/home.php?mod=space&uid=345712&do=blog&id=248921

好久没有写bolg了,前些天遇到一个很纠结的问题,就是如何将一个可执行文件打包到APK中并且运行该文件,开始想用NDK的方式将其以动态库的方式打包至APK中,可是由于条件限制没有源码只有一个可执行文件。无奈之下只好采取曲线救国的道路。好了闲话少说我们进入主题吧

首先我们要知道APK的打包原理有哪些文件时可以打包到APK中去的先将APK文件用winRAR工具打开可以看到起文件目录结果如下

|-assest
|-lib
|-META-INF
|-res
|-AndroidManifest.xml
|-classes.dex
|-resources.arsc

        从上述可以看出 Android的apk文件中除了一些标准的资源文件,我们还可以在/assets和/res/raw中置入一些非标准的文件,但是却只能通过输入流访问,因为这些文件在安装时并没有被复制到data目录下。这个限制在Java层可能并无大碍,但是如果想通过本地C/C++代码访问的话,似乎就只能指定路径了。
       想要将一个可执行文件打包入APK我们可以将可执行文件当做资源文件打包至APK,接下来就是读取/assets和/res/raw下的文件,然后通过FileOutputStream openFileOutput (String name, int mode)将其写入到对应数据目录下的files目录,之后文件路径可以通过getFilesDir()+"filename"取得,编码上稍微有点繁琐。核心代码如下

//先检查files目录下是否存在该文件不能存在拷贝该文件到files目录下
if (!fileIsExists(executablePath))
{

          String executableFilePath = getFilesDir().getParent()+ "/files/" + commandtext.getText().toString();

 try

{

  InputStream input = getResources().getAssets()  .open( commandtext.getText().toString());读取Assets目录下的可执行文件到输入流

   int filesize = input.available(); //获取文件小
   writeFileData(executableFilePath, input, filesize);//将文件写到executableFilePath下
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void writeFileData(String fileName, InputStream message, int filesize)
 {

try
  {
  FileOutputStream fout = new FileOutputStream(fileName);
  byte[] bytes = new byte[filesize];
  message.read(bytes);
  fout.write(bytes);
  fout.close();
}

         catch (Exception e)

{

e.printStackTrace();
}

}

public boolean fileIsExists(String fileName)
{

File f = new File(fileName);
if (!f.exists())
{
       return false;
}
return true;

}

执行可执行文件核心代码如下

File elfFile = new File(executableFilePath);
boolean b = elfFile.setExecutable(true, true); //设置可执行权限
Runtime runtime = Runtime.getRuntime();
Process proc = null;
try
{
       proc = runtime.exec(executableFilePath);
} catch (IOException e1)
{
      // TODO Auto-generated catch block
      e1.printStackTrace();
}

        在API Level 9及以上版本中,可通过File类的boolean setExecutable(boolean executable, boolean ownerOnly)方法将文件重新加上可执行权限。API Level 9以下版本则需通过shell命令,并且需要root权限,具体做法如下:
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());

os.writeBytes("chmod 555 " + executablePath + " ");
os.flush();

os.writeBytes(executablePath + " &" + " ");
os.flush();

os.writeBytes("exit ");
os.flush();

原文地址:https://www.cnblogs.com/newgreen/p/3471265.html