> public class ndk extends Activity
> {
> public native String getString();
> static
> {
> System.loadLibrary("native_lib_name");
> }
> }
> JNIEXPORT jstring JNICALL Java_ndkverification_getString(JNIEnv * env,
> jobject obj)
You're function name doesn't match what JNI expects. To find the native
implementation of a "native" Java function, JNI has two options:
Implicit Matches: Done by name; the name of your C function has to match
the *full* method signature, which includes the package and class names
exactly as they are in Java. So your function definition would have to be:
---
JNIEXPORT jstring JNICALL
Java_com_androiddeveloper_DefaultNdkverification_ndk_getString(JNIEnv
*env, jobject self)
---
Explicit Matches: Done by using the RegisterNatives method of JNIEnv.
This is a *much better option* for many reasons, including being more
reliable, more efficient, and eliminating the ugly function names. To
do this, you would do something like this:
---
#define JAVA_CLASS "com/androiddeveloper/DefaultNdkverification/ndk";
static jstring JNICALL JNI_getString(JNIEnv *env, jobject self);
static JNINativeMethod methods[] = {
{ "getString", "()Ljava/lang/String;", (void *)&JNI_getString}
}
jint JNI_OnLoad(JavaDM *vm, void *reserved)
{
JNIEnv *env;
if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_4) != JNI_OK)
return -1;
jclass cls = (*env)->FindClass(env, JAVA_CLASS);
(*env)->RegisterNatives(env, cls, methods, sizeof(methods) /
sizeof(methods[0]));
return JNI_VERSION_1_4;
}
---
The second method looks like its more work, but it is ultimately a
better option. And once you have it written once, you can pretty much
copy/paste the JNI_OnLoad implementation into any JNI library you want,
and just change the methods array and JAVA_CLASS to suit.
--Mike