http://marakana.com/forums/android/examples/49.html
I was working on created java native interface for one of my Android app. Its old during Android 2.1 days. Today after couple of years, I tried to create interface again, in my Ubuntu host system, and got some issues. I though better to keep one hello world post for JNI.
Create a project with name of your choice and copy this minimal code in class, lets say in callnative.java
package com.example.ih;
public class callnative {
static {
System.loadLibrary("my_lib"); //to load library
}
public native int add(int x, int y); //calling native function
public native String hello();
}
Go to /workspace/ih/bin path and give below command,
$javah -jni -classpath /home/abhishekkd/workspace/ih/bin/classes com.example.ih.callnative
$cat com_example_ih_callnative.h
You will see a heard file generated something like,
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ih_callnative */
#ifndef _Included_com_example_ih_callnative
#define _Included_com_example_ih_callnative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_ih_callnative
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_ih_callnative_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_example_ih_callnative
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_ih_callnative_hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
I was working on created java native interface for one of my Android app. Its old during Android 2.1 days. Today after couple of years, I tried to create interface again, in my Ubuntu host system, and got some issues. I though better to keep one hello world post for JNI.
Create a project with name of your choice and copy this minimal code in class, lets say in callnative.java
package com.example.ih;
public class callnative {
static {
System.loadLibrary("my_lib"); //to load library
}
public native int add(int x, int y); //calling native function
public native String hello();
}
Go to /workspace/ih/bin path and give below command,
$javah -jni -classpath /home/abhishekkd/workspace/ih/bin/classes com.example.ih.callnative
$cat com_example_ih_callnative.h
You will see a heard file generated something like,
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ih_callnative */
#ifndef _Included_com_example_ih_callnative
#define _Included_com_example_ih_callnative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_ih_callnative
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_ih_callnative_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_example_ih_callnative
* Method: hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_ih_callnative_hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Now, implement declared functions,
$gedit hello_lib.c
#include "com_example_ih_callnative.h"
JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
(JNIEnv * env, jobject obj) {
return (*env)->NewStringUTF(env, "Hello World!");
}
JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
(JNIEnv * env, jobject obj, jint value1, jint value2) {
return (value1 + value2);
}
No comments:
Post a Comment