Hosting an executable within Android application
1) No, there should be no constrains, besides those that access system files and thus require root. The best place would be straight to /data/data/[your_package_name] to avoid polluting elsewhere.
2) A very thorough discussion about compiling against native libraries can be found here: http://www.aton.com/android-native-libraries-for-java-applications/ . Another option is a cross-compiler for arm (here is the one used to compile the kernel, it's free: http://www.codesourcery.com/sgpp/lite/arm ). If you plan to maintain a service that executes your cammand, be warned that services can be stopped and restarted by android at any moment.
3) Now, if you don't have the source code, I hope that your file is at least compiled as an arm executable. If not, I don't see how you could even run it.
You will execute the file by running the following commands in your java class:
String myExec = "/data/data/APPNAME/FILENAME";
Process process = Runtime.getRuntime().exec(myExec);
DataOutputStream os = new DataOutputStream(process.getOutputStream());
DataInputStream osRes = new DataInputStream(process.getInputStream());
I know nothing about your executable, so you may or may not need to actually get the inputStream and outputStream.
I am assuming that running adb to push the binary file is out of the question, so
I was looking for a neat way to package it. I found a great post about including an executable in your app. Check it out here:
http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App
The important part is this one (emphasis mine):
From Android Java app, using
assets
folder
- Include the binary in the assets folder.
- Use
getAssets().open(FILENAME)
to get anInputStream
.- Write it to
/data/data/APPNAME
(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/APPNAME/FILENAME
using the code above.- Run your executable using the code above.
The post uses the assets
folder, insted of the raw
folder that android suggests for static files:
Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
To access the data folder, you can follow the instructions here:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Also, there's the File#setExecutable(boolean);
method that should works instead of the shell command.
So, putting everything together, I would try:
InputStream ins = context.getResources().openRawResource (R.raw.FILENAME)
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(buffer);
fos.close();
File file = context.getFileStreamPath (FILENAME);
file.setExecutable(true);
Of course, all this should be done only once after installation. You can have a quick check inside onCreate()
or whatever that checks for the presence of the file and executes all this commands if the file is not there.
Let me know if it works. Good luck!
Android, Java, running an Executable resource
Yes, you can do this. You will need to make sure that the native executable is for the appropriate target architecture (some type of ARM processor, usually).
But you shouldn't extract it to /data/local/tmp
. You will need to extract it to /data/data/application.package.name/lib
, which is a directory to which your app has read/write permissions.
There are more details in this question and its accepted answer. It looks as though you can have the executable extracted automatically for you if you name it as if it were a library file rather than a standalone executable.
Related Topics
Can Not Find the Tag Library Descriptor for "Http://Java.Sun.Com/Jsp/Jstl/Core"
Java: Why Do You Need to Specify an 'F' in a Float Literal
Difference Between Java.Util.Random and Java.Security.Securerandom
Javafx - Border Radius <-> Background Color
How to Fix Google Cloud Messaging Registration Error: Service_Not_Available
Elegant Way to Read File into Byte[] Array in Java
How to Automatically Generate Getters and Setters in Eclipse
Android Change Material Elevation Shadow Color
Clearing and Setting the Default Home Application
Java Output Formatting for Strings
Scanner Doesn't See After Space
"Java Dateformat Is Not Threadsafe" What Does This Leads To
How to Use Jsp/Jstl to Generate Dynamic CSS/JavaScript Files
Registering a Headset Button Click with Broadcastreceiver in Android
Android Archive Library (Aar) VS Standard Jar
Android Heterogeneous Gridview Like Pinterest
How to Get Elements of Fragments Created by Viewpager in Mainactivity