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. 栈应用之 括号匹配问题(Python 版)

    栈应用之 括号匹配问题(Python 版) 检查括号是否闭合 循序扫描被检查正文(一个字符)里的一个个字符 检查中跳过无关字符(所有非括号字符都与当前处理无关) 遇到开括号将其压入栈 遇到闭括号时弹出 ...

  2. log4j与commons-logging slf4j的关系

    1. slf4j     他只提供一个核心slf4j api(就是slf4j-api.jar包),这个包只有日志的接口并没有实现     所以如果要使用就得再给它提供一个实现了些接口的日志包,     ...

  3. Maven3核心技术(笔记三)

    第一节:Maven仓库概念 Maven 远程仓库配置文件:$M2_HOME/lib/maven-model-builder-3.3.3.jar 文件:org\apache\maven\model\po ...

  4. c语言快速排序算法(转)

    原文链接http://blog.csdn.net/morewindows/article/details/6684558 快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常 ...

  5. Spark(六)Spark之开发调优以及资源调优

    Spark调优主要分为开发调优.资源调优.数据倾斜调优.shuffle调优几个部分.开发调优和资源调优是所有Spark作业都需要注意和遵循的一些基本原则,是高性能Spark作业的基础:数据倾斜调优,主 ...

  6. 【AtCoder】AGC011 C - Squared Graph

    题解 大意是给出一张图,然后建一张新图,新图的点标号是(a,b) 如果a和c有一条边,b和d有一条边,那么(a,b)和(c,d)之间有一条边 我们把这道题当成这道题来做,给出两张图,如果第一张图有边( ...

  7. Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决

    有人会说不建议Wpf中使用Winform控件,有人会说建议使用Winform控件在Wpf下的替代方案,然而在实际工作中由于项目的特殊需求,考虑到时间.成本等因素,往往难免会碰到在WPF中使用Winfr ...

  8. 黑马程序员_java基础笔记(12)...内省(IntroSpector)

    —————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流!—————————— 1,了解JavaBean.2,BeanUtils工具包. 1,了解JavaBean. ...

  9. 004 Numpy

    一:Numpy介绍 1.简介 Numerical Python 就是数值python包,是python进行科学计算的一个基础包,因此要更好的理解与掌握python科学计算包,尤其是pandas,需要先 ...

  10. 异常类Exception(String message, Throwable cause)中的cause理解

    这个在构造函数里面竟然有一个Throwable,感觉有些奇怪. 1. Throwable cause 这里cause要传一个Throwable的子类异常进去么? 是引起这个异常的异常,如果这个值是空值 ...