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. JAVA复习笔记分布式篇:zookeeper

        前言:终于到分布式篇,前面把JAVA的一些核心知识复习了一遍,也是一个JAVA程序员最基本要掌握的知识点,接下来分布式的知识点算是互联网行业的JAVA程序员必备的技能:     概念:ZooK ...

  2. 可怕的线程上下文类装载器(TCCL)

    在明天的 OSGi 2012 社区活动上,我将以"如何使你的类库在不依赖 OSGi 的情况下进行友好地 OSGi"为主题进行演讲.在演讲中我将会提及 Java 的线程上下文类加载器 ...

  3. day5模块学习--yaml文件处理

    yaml文件处理(http://pyyaml.org/wiki/PyYAMLDocumentation)     摘要: 本文讲的是yaml在python上的使用教程详解, YAML是一种容易人类阅读 ...

  4. 【LOJ】#2024. 「JLOI / SHOI2016」侦查守卫

    题解 童年的回忆! 想当初,这是我考的第一次省选,我当时初二,我什么都不会,然后看着这个东西,是不是能用我一个月前才会的求lca,光这个lca我就调了一个多小时= =,然后整场五个小时,我觉得其他题不 ...

  5. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest B - Bring Your Own Bombs 离散化+扫描线+计算期望

    扫描线一边扫一边算期望,细节比较多. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  6. HTML Input 表单校验之datatype

    凡要验证格式的元素均需绑定datatype属性,datatype可选值内置有10类,用来指定不同的验证格式. 如果还不能满足您的验证需求,可以传入自定义datatype,自定义datatype是一个非 ...

  7. ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out.

    ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out. 通过pip安装 num ...

  8. python import 与 from ... import ...

    import test test = 'test.py all code' from test import m1 m1 ='code'

  9. 减少TIME_WAIT连接状态

    减少TIME_WAIT连接状态.网络上已经有不少相关的介绍,大多是建议: shell> sysctl net.ipv4.tcp_tw_reuse=1 shell> sysctl net.i ...

  10. 什么是DHTML?

    DHTML是近年来网络发展进程中最振奋人心也最具实用性的创新之一.它是一种通过各种技术的综合发展而得以实现的概念(当然,不同的浏览器,实现的程度也不同),这些技术包括Javascript, VBScr ...