Run native executable in Android App
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的更多相关文章
- 原生Android App项目调用Untiy导出的Android项目
背景:采用Google VR SDK for Unity 开发3D场景功能,然后导出Android项目,合并到一个Android App里面,供其它Activity调用. 用Google VR for ...
- 不可或缺 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 ...
- Android app : use html or native?
Android app可分为两种:网络(html)应用程序和原生(native)应用程序 首先,我们先来讨论下如何判断一个app是html实现还是native实现. 设置-->>开发者选项 ...
- Android App 安全的HTTPS 通信
漏洞描述 对于数字证书相关概念.Android 里 https 通信代码就不再复述了,直接讲问题.缺少相应的安全校验很容易导致中间人攻击,而漏洞的形式主要有以下3种: 自定义X509TrustMana ...
- 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 ...
- React Native工程修改Android包名
默认初始化的React Native工程,生成Android工程的时候,包名默认是React Native工程的名字,跟一般Android工程com.company.xxx不一样. 这时候就需要手动修 ...
- Android App优化之ANR详解
引言 背景:Android App优化, 要怎么做? Android App优化之性能分析工具 Android App优化之提升你的App启动速度之理论基础 Android App优化之提升你的App ...
- Android app 全局异常统一处理
异常处理需求 Android app 出现 crash 时,会出现 "程序异常退出" 的提示并关闭,体验不好,另外主要是无法知道哪里出现的崩溃,需要知道哪里造成的异常,就需要一个全 ...
- Android.app.SuperNotCalledException错误
- ::): FATAL EXCEPTION: main - ::): android.app.SuperNotCalledException: Activity {com.solar/com.sol ...
随机推荐
- 栈应用之 括号匹配问题(Python 版)
栈应用之 括号匹配问题(Python 版) 检查括号是否闭合 循序扫描被检查正文(一个字符)里的一个个字符 检查中跳过无关字符(所有非括号字符都与当前处理无关) 遇到开括号将其压入栈 遇到闭括号时弹出 ...
- github后端开发面试题大集合(二)
作者:小海胆链接:https://www.nowcoder.com/discuss/3615?type=0&order=0&pos=8&page=0来源:牛客网 7.非关系型数 ...
- UVA10212 【The Last Non-zero Digit.】
暴力可做!!!(十秒还不打暴力!!!)暴力算阶乘边算边取余上代码 #include<iostream> #define int long long //开long long using n ...
- VFS,super_block,inode,dentry—结构体图解
总结: VFS只存在于内存中,它在系统启动时被创建,系统关闭时注销. VFS的作用就是屏蔽各类文件系统的差异,给用户.应用程序.甚至Linux其他管理模块提供统一的接口集合. 管理VFS数据结构的组成 ...
- JS中给函数参数添加默认值
最近在Codewars上面看到一道很好的题目,要求用JS写一个函数defaultArguments,用来给指定的函数的某些参数添加默认值.举例来说就是: // foo函数有一个参数,名为x var f ...
- 基于docker 搭建Prometheus+Grafana
一.介绍Prometheus Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采 ...
- 20155309南皓芯2016-2017 2《Java程序设计》第一周学习总结
关于java学习笔记的思考问题 第一章:JDK与JRE,JVM之间有没有必然的联系 第二章:可执行文件夹找到相关链接库 第三章:for与while循环的用法与比较,break与continue跳出的注 ...
- SQL 2008 还原SQL 2005备份文件不成功的解决方
在SQL Server 2008 r2上还原一个SQL Server 2005时,还原不成功,提示如下信息: 按照如下情况则还原成功: -- 查看备份文件的类型 RESTORE FILELISTONL ...
- spring boot配置文件中 spring.mvc.static-path-pattern 配置项
spring boot项目中的静态资源文件存放在static文件下面,当通过浏览器访问这些静态文件时,发现必须要添加static作为前缀才能访问,折腾了一番后发现,这个前缀跟 spring.mvc.s ...
- 2017-2018-1 20179202《Linux内核原理与分析》第十一周作业
Metasploit实现木马生成.捆绑.免杀 1.预备知识 (1)Metasploit Metasploit是一款开源的安全漏洞检测工具,全称叫做The Metasploit Framework,简称 ...