在 Android 中调用二进制可执行程序(native executable)

前几天有需要在java代码中调用二进制程序,就在网上找了些资料,写点东西记录下。

Android 也是基于linux 的系统,当然也可以运行二进制的可执行文件。只不过Android 限制了直接的方式只能安装运行apk文件。虽然有NDK可以用动态链接库的方式来用C的二进制代码,但毕竟不方便。至少我们可以调用linux的一些基本 命令,如ls,rm等。

第一种方法:Runtime.exec(String[] args)


这种方法是java语言本身来提供的,在Android里面也可以使用。args是要执行的参数数组。大概用法如下:

S tring[] args = new String[2];

args[0] = "ls";

args[1] = "-l";

try { Process process = Runtime.getRuntime().exec(arg);


//get the err line

InputStream stderr = process.getErrorStream(); InputStreamReader isr err = new InputStreamReader(stderr); BufferedReader br err = new BufferedReader(isr err);


//get the output line InputStream outs = process.getInputStream(); InputStreamReader isrout = new InputStreamReader(outs); BufferedReader brout = new BufferedReader(isrout);

String errline = null;

String result = "";

// get the whole error message string while ( (line = br err.readLine()) != null) { result += line; result += "/n";

}

i f( result != "" )

{

// put the result string on the screen

}

// get the whole standard output string

while ( (line = brout.readLine()) != null) { result += line; result += "/n"; } if( result != "" ) {

// put the result string on the screen

}

}catch(Throwable t) { t.printStackTrace(); }

以上 代码执行了linux的标准命令 ls -l。执行此命令后的标准输出是在brout中。如果出错,像参数错误,命令错误等信息就会放在brerr中。

有需要的话从里面读出来便可。

第二种方法:Class.forName("android.os.Exec")

这个方法是一个老外找到的,原贴在这里:http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App

代码大概是这样:

try {

    	
// android.os.Exec is not included in android.jar so we need to use reflection.
    	
Class<?> execClass = Class.forName("android.os.Exec");
    	
Method createSubprocess = execClass.getMethod("createSubprocess",
            String.class, String.class, String.class, int[].class);
    	
Method waitFor = execClass.getMethod("waitFor", int.class);
    
    	
// Executes the command.
    	
// NOTE: createSubprocess() is asynchronous.
    	
int[] pid = new int[1];
    	
FileDescriptor fd = (FileDescriptor)createSubprocess.invoke( null, "/system/bin/ls", "/sdcard", null, pid);
    
   	
 // Reads stdout.
    	
// NOTE: You can write to stdin of the command using new FileOutputStream(fd).
    	
FileInputStream in = new FileInputStream(fd);
    	
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    	
String output = "";
    	
try {
        	
String line;
        	
while ((line = reader.readLine()) != null) {
            	
output += line + "/n";
        }
    	
} catch (IOException e) {
        	
// It seems IOException is thrown when it reaches EOF.
    	
}
    
    	
// Waits for the command to finish.
    	
waitFor.invoke(null, pid[0]);
    
    	
return output;
} catch( ... )
...

这种方法是用了 Android 提供的android.os.Exec类。在 Android 的源代码中已经提供了类似 terminal 的ap,在

/development/apps/Term/src/com/android/term 中,这个ap就是用android.os.Exec来调用linux的基本命令的。不过这个类只有在Android的内置ap中才可以使用。单独写一个非 内置ap的话是无法直接调用android.os.Exec的。间接的方法就是类似上面,用 Class.forName("android.os.Exec")来得到这个 类,execClass.getMethod("createSubprocess", ... )来得到类里面的方法,这样就可以使用本来不能用的类了。这就是反射?...

这个方法看起来要麻烦一些,而且比较歪,我觉得还是用java本身提供的东西比较好,毕竟简单,移植性也好点。

至于自己写的二进制执行程序如何放到apk里面如何调用,原帖也说的很清楚,不过要提醒一下,assets文件夹对单个文件大小有限制为1MB. 所以如果有资源文件大于1MB我看只好自己先切割一下了,运行的时候再自己拼起来...

    http://blog.csdn.net/liujian885/archive/2009/12/01/4912218.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android.mk文件是Android编译系统使用的构建脚本之一,它描述了如何构建一个Android应用程序或者一个Android库。Android.mk文件使用makefile语法,可以通过定义一系列的规则来指定编译过程需要执行的命令。 要预编译一个可执行程序,可以在Android.mk文件添加以下规则: ```makefile # 编译可执行程序 include $(CLEAR_VARS) LOCAL_MODULE := myexecutable LOCAL_SRC_FILES := myexecutable.c include $(BUILD_EXECUTABLE) ``` 这个规则定义了一个名为myexecutable可执行程序,其源代码位于myexecutable.c文件。通过调用BUILD_EXECUTABLE规则,构建系统将自动编译和链接该程序,并将其放置在输出目录。 要在Android应用程序使用该可执行程序,可以在应用程序的Android.mk文件添加以下规则: ```makefile # 添加可执行程序依赖 include $(CLEAR_VARS) LOCAL_MODULE := myapp LOCAL_SRC_FILES := myapp.c LOCAL_SHARED_LIBRARIES := libmyexecutable include $(BUILD_SHARED_LIBRARY) # 引用可执行程序 include $(CLEAR_VARS) LOCAL_MODULE := libmyexecutable LOCAL_SRC_FILES := myexecutable.c include $(BUILD_EXECUTABLE) ``` 这个规则定义了一个名为myapp的Android应用程序,其源代码位于myapp.c文件。该应用程序依赖于名为libmyexecutable的共享库,而该共享库实际上是我们之前定义的myexecutable可执行程序。因为我们将myexecutable编译为可执行程序,而不是共享库或静态库,所以我们需要将其转化为共享库,以便在Android应用程序使用。最后,我们需要在Android.mk文件引用myexecutable可执行程序,以便将其编译为共享库。 在Android应用程序使用预编译的可执行程序可以提高应用程序的性能和可维护性,因为我们可以将一些计算密集型的操作委托给预编译的可执行程序来处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值