I'm building a application which makes use of a Native service binary file.
Now my requirements are:
Place the native service binary file in the assets folder of application.
- When the application runs, copy this native file in the device (path like
getExternalFilesDir(null)
) - Execute this binary file within the device.
- Check if the file has been succesfully executed in logs.
Now to copy the file, I've used:
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for (String filename : files) {
if (filename.equals("native-file")) { // native-file is the file name present is assets folder
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
break;
}
}
}
And after copying to execute the file I'm using:
File sdCard = getExternalFilesDir(null); // directory where native file is placed
Process proc = Runtime.getRuntime().exec("sh -c shell " + sdCard.getAbsolutePath() + "/native-file "+ sdCard.getAbsolutePath() +"/native-file.log\" "); // command 1
proc = Runtime.getRuntime().exec("sh -c less " + sdCard.getAbsolutePath() +"/native-file.log\""); // command 2
Issues:
- How can I make sure that the file is placed.
- How to make sure the that it is executed. Because I'm not able to find and log files (native-file.log) when I run the application.
PS - I can not provide my JNI file due to proprietry issues. So i guess we can use an opensource asl library.(there is a native file by name asl-native)