Named pipea (fifo) na androidu

Posto mkfifo() poziv nije dostupan iz java-e na androidu (ili barem meni nije poznato kako). Postoje dva rešenja: pozivanje externe mkfifo komande ili pravljenje C biblioteke koja poziva funkciju i linkovati sa javom (JNI). Drugi način je elegantniji i ali komplikovanije rešenje.

jni/Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

TARGET_PLATFORM := android-3
LOCAL_MODULE := mkfifo
LOCAL_SRC_FILES := mkfifo.c
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

jni/mkfifo.c:

#include <string.h>
#include <jni.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>

JNIEXPORT jint JNICALL
Java_info_zsteva_fifo_mkfifo( JNIEnv* env, jobject thiz, jstring path )
{
jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);

int ret = mkfifo(path_utf, 0700);

(*env)->ReleaseStringUTFChars(env, path, path_utf);

return ret;
}

src/info/zsteva/mkfifo.java:

package info.zsteva;

public class fifo {
// JNI
public native static int mkfifo(String path);
static {
System.loadLibrary("mkfifo");
}
}

Smestiti fajlove na adekvatna mesta i u rootu android projekta pokrenuti ndk-build (za kompajliranje je potreban android-ndk-sdk).

This entry was posted in android and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.