Write c through the java jni (java native interface).
Below is a file write example using native c.
#include
#include
#include
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
FILE* file = fopen(“textexample”,”w+”);
if (file != NULL)
{
fputs(“text example!\n”, file);
fflush(file);
fclose(file);
}
return (*env)->NewStringUTF(env, “JNI (with file io)!”);
}
The java activity to drive this c interface is:
package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
static {
System.loadLibrary(“hello-jni”);
}
}
The android.mk file is:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)