Run native executable in Android App

Demo 

Here's demo application called "Run Native Exe" to:

  • run local UNIX commands
  • run native executable downloaded from the Web

Package: NativeExe-0.2.apk
Source code: on Github (ADT project)

To install the package,

  • Go to Settings→Application and check "Unknown sources"
  • Open the package link above using Android Browser

or type "adb install NativeExe-*.apk" in your PC if you have Android SDK.

How can I run UNIX commands in Android App? 

You can use Runtime.exec() in standard Java. Here's sample code to run /system/bin/ls /sdcard in Android App:

try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard"); // Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.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.
process.waitFor(); return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

This code is based on this article. Thanks yussi to let me know this by comment.

OK, but how can I put my own native executable in Android App? 

First, you need to cross-compile your native executable for ARM.

Here's a way (dynamic link version). Or you can use Scratchbox (Japanese).

If you get a file with a format like this, it's probably OK:

% file yourapp
yourapp: ELF -bit LSB executable, ARM, version (SYSV), for GNU/Linux 2.6., statically linked, not stripped

You have three ways to put the binary to the phone:

  • From Android Java app, using assets folder (by fnerg in comment below)

    • Include the binary in the assets folder.
    • Use getAssets().open("YourBinaryHere") to get an InputStream.
    • Write it to /data/data/app-package-name (e.g. /data/data/net.gimite.nativeexe), where your application has access to write files and make it executable.
    • Run "/system/bin/chmod 744 /data/data/app-package-name/yourapp" using the code above.
    • Run your executable using the code above.
  • From Android Java app, downloading via HTTP (which I use in my demo application above)

    • Dowload the executable using HTTP and put it to /data/data/app-package-name (e.g. /data/data/net.gimite.nativeexe), where your application has access to write files and make it executable. You can use standard Java FileOutputStream to write files there.
    • Run "/system/bin/chmod 744 /data/data/app-package-name/yourapp" using the code above.
    • Run your executable using the code above.
  • By adb (needs SDK and root)

    • If you want to put the executable to YOUR phone connected with adb command in Android SDK and you have root, you can put the executable by:
% adb shell
$ su
# mkdir /data/tmp
# chmod /data/tmp
# exit
$ exit
% adb push yourapp /data/tmp
% adb shell
$ chmod /data/tmp/yourapp
$ /data/tmp/yourapp

Note that you cannot make files executable in /sdcard.

Run native executable in Android App的更多相关文章

  1. 原生Android App项目调用Untiy导出的Android项目

    背景:采用Google VR SDK for Unity 开发3D场景功能,然后导出Android项目,合并到一个Android App里面,供其它Activity调用. 用Google VR for ...

  2. 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native

    [源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...

  3. Android app : use html or native?

    Android app可分为两种:网络(html)应用程序和原生(native)应用程序 首先,我们先来讨论下如何判断一个app是html实现还是native实现. 设置-->>开发者选项 ...

  4. Android App 安全的HTTPS 通信

    漏洞描述 对于数字证书相关概念.Android 里 https 通信代码就不再复述了,直接讲问题.缺少相应的安全校验很容易导致中间人攻击,而漏洞的形式主要有以下3种: 自定义X509TrustMana ...

  5. Android cannot be cast to android.app.Fragment

    10-21 17:33:45.171: E/AndroidRuntime(7644): java.lang.RuntimeException: Unable to start activity Com ...

  6. React Native工程修改Android包名

    默认初始化的React Native工程,生成Android工程的时候,包名默认是React Native工程的名字,跟一般Android工程com.company.xxx不一样. 这时候就需要手动修 ...

  7. Android App优化之ANR详解

    引言 背景:Android App优化, 要怎么做? Android App优化之性能分析工具 Android App优化之提升你的App启动速度之理论基础 Android App优化之提升你的App ...

  8. Android app 全局异常统一处理

    异常处理需求 Android app 出现 crash 时,会出现 "程序异常退出" 的提示并关闭,体验不好,另外主要是无法知道哪里出现的崩溃,需要知道哪里造成的异常,就需要一个全 ...

  9. Android.app.SuperNotCalledException错误

    - ::): FATAL EXCEPTION: main - ::): android.app.SuperNotCalledException: Activity {com.solar/com.sol ...

随机推荐

  1. MYSQL-重做系统恢复MYSQL过程

    记笔记是好习惯,记笔记是好习惯,记笔记是好习惯! 重要的事情说三遍. 说多了都是泪.第一次装MYSQL时候就遇到了很多问题,当时解决了忘记记录了.家里硬盘满了,于是买了个4T的硬盘重装系统.重装系统后 ...

  2. AdvStringGrid 删除数据

    unit Unit6; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  3. 用户说体验 | 关于阿里百川HotFix你需要了解的一些细节

    最近很火的热修复技术,无意中了解到阿里百川也在做,而且Android.iOS两端都支持,所以决定试一试.试用一段时间后,感觉还不错,主要是他们有一个团队在不断维护更新这个产品,可以看到他们的版本更新记 ...

  4. grail开发环境的搭建

    本文参考:Grails入门指南(第二版) 1. 下载jdk和Grail http://www.oracle.com/technetwork/java/javase/downloads/ http:// ...

  5. poj 2187 N个点中输出2点的最大距离的平方

    旋转卡壳 Sample Input 40 00 11 11 0Sample Output 2 # include <iostream> # include <cstdio> # ...

  6. JavaScript工程师都应懂的33个概念

    最近福利发的有点多啊,各种硬干货,小伙伴们是不是觉得很爽啊.Github真的蕴含着各种各样的宝藏,难怪各个大厂也都纷纷贡献自己的代码到Github上. 所以各种干货还是会源源不断的po给大家,觉得有帮 ...

  7. java8 - 4

    import java.io.PrintStream; import java.util.Comparator; import java.util.function.BiFunction; impor ...

  8. Caffe训练AlexNet网络模型——问题二

    训练时,出现Check failed:error == cudaSuccess (2 vs. 0) out of memory,并且accruary = 0,如下图所示: 解决方法:将train_va ...

  9. shuffle过程分析

    shuffle的过程分析 shuffle阶段其实就是之前<MapReduce的原理及执行过程>中的步骤2.1.多个map任务的输出,按照不同的分区,通过网络copy到不同的reduce节点 ...

  10. vwware虚拟机无法连接外网

    1.问题:动态IP时连接外网没有问题,但是将IP改为静态IP时发现没有办法连接外网 查看文件/etc/resolv.conf,里面的内容全部都被注释 [root@jenkins network-scr ...