java调用android打包_Android-----打包可执行文件并在apk调用

本文介绍如何将交叉编译的可执行文件集成到Android应用中,通过在assets目录放置文件,然后在Java代码中读取并设置执行权限,最终实现从 APK 调用执行文件。主要步骤包括:1) 交叉编译生成hello可执行文件;2) 将文件放入assets目录;3) 在Activity中复制文件到data目录并执行。
摘要由CSDN通过智能技术生成

1.首先交叉编译可执行文件hello

2.将hello放到assets下

0818b9ca8b590ca3270a3433284dd417.png

3.举例:

package com.test.android.exe;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

public class MainActivity extends Activity {

private String exe_path = "data/data/com.test.android.exe/hello";

private File exe_file;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

try {

copyBigDataToSD(exe_path);

exe_file = new File(exe_path);

exe_file.setExecutable(true, true);

execCmd(exe_path);

} catch (IOException e1) {

e1.printStackTrace();

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

private void execCmd(String cmd) throws IOException {

Runtime runtime = Runtime.getRuntime();

Process process = runtime.exec(cmd);

InputStream is = process.getInputStream();

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line = null;

while (null != (line = br.readLine())) {

Log.e("########", line);

}

try {

process.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

private void copyBigDataToSD(String strOutFileName) throws IOException

{

InputStream myInput;

OutputStream myOutput = new FileOutputStream(strOutFileName);

myInput = this.getAssets().open("hello");

byte[] buffer = new byte[1024];

int length = myInput.read(buffer);

while(length > 0)

{

myOutput.write(buffer, 0, length);

length = myInput.read(buffer);

}

myOutput.flush();

myInput.close();

myOutput.close();

}

}

4.效果:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值