开发者

Redirect output from JNI library

开发者 https://www.devze.com 2023-04-11 11:02 出处:网络
I\'m looking for a way to redirect/capture the stdout output of a native library in a simple Android app.I\'ve seen a couple posts on this but I\'m not finding the answers to my questions.

I'm looking for a way to redirect/capture the stdout output of a native library in a simple Android app. I've seen a couple posts on this but I'm not finding the answers to my questions.

As much as possible I'd like to avoid tinkering with the existing native library. My first impulse is to use a fifo file and redirect the native library's output. I can't seem to get past the call to Runtime.开发者_运维知识库getRuntime().exec("mkfifo(,0666)"). I'm trying to create the file in /data/data/ but the file doesn't seem to exist after the program runs. I also can't seem to find 'mkfifo' in the Android filesystem (via adb shell).

Couple of dumb questions: - Where is mkfifo? - Should I be able to see the fifo file after the program runs or is there a more reliable way to test the fifo's existence from Java after I run the above command? - Is there another/better approach to take for this?

Thanks.


Do you want to redirect it specifically to a file? Then what you probably wanna do is:

int fd = open(filename, "w");
dup2(fd, 1);
close(fd);

I have no idea how will this affect the Java subsystem.

Also note that the /data/data might not be writable. The application sandbox is under /data/data/com.mypackage/, and Android expects you to create subdirectories for your data there via Context.getDir(). /sdcard is always writable, but you have to check availability first - the card is by definition removable storage.

If redirecting to a file is not your ultimate desire, then pipe() might be a better choice. dup2() the write end to 1, and read from the read end, preferably in a different thread. Then it's up to you what to do to the data read.


It's very easy in your case:

Process process = Runtime.getRuntime().exec(cmd);
InputStream processOut = process.getInputStream();
InputStream processError = process.getErrorStream();

But I can't find the solution for myself. I have java library that puts its content into a file (identified by String fileName or FileDescriptor fd). Also there is a native library. I need to redirect output from the first one to input of the second library. I thought that fifo is a solution, but android file system is fat32 and can't have FIFO files.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号