Run native executable in Android appdemo†
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 the Your PC if you have the Android SDK.
↑how can I run UNIX commands in Android App? †
You can use Runtime.exec () on 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 =NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); intRead; Char[] buffer =New Char[4096]; StringBuffer Output=NewStringBuffer (); while(read = reader.read (buffer)) > 0) {output.append (buffer,0, read); } reader.close (); //Waits for the command to finish.process.waitfor (); returnoutput.tostring ();} Catch(IOException e) {Throw NewRuntimeException (e);} Catch(interruptedexception e) {Throw NewRuntimeException (e);}
This code was based on the this article. Thanks Yussi to let me know this by comment.
↑ok, but what can I put my own native executable in Android App? †
First, you need-cross-compile your native executable for ARM.
Here's a-to (dynamic link version). Or You can use Scratchbox (Japanese).
If you get a file with a format like this, it's probably OK:
%1for2.6. statically linked, not stripped
You are three ways to put the binary to the phone:
- from the Android Java app, using Assets folder (by Fnerg in comment below)
- include the binary in the Assets folder.
- use Getassets (). Open ("Yourbinaryhere") to get a inputstream.
- Write It to/data/data/ app-package-name (e.g./data/data/net.gimite.nativeexe), where your Application have 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 the 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.nativeex e), where your application have access to the write files and make it executable. You can use the 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 the Android SDK and we have root, you can put T He executable by:
%/data/777 /data/tmp# exit$ exit% adb push yourapp/data/tmp% 744 /data/tmp//data/tmp/yourapp
Note that you cannot make files executable in/sdcard.
Run native executable in Android App