Ita
Ita

Reputation: 1292

How to define a return type for a JNI method with a custom Object class?

I have a method in JNI (C++) and I want to be able to return a custom Object type (not a primitive or a String etc...) I've written something down but I keep getting java.lang.UnsatisfiedLinkError error.

Here are the details:

The method:

static jobject Java_android_sdk_Core_ProcessFrame(JNIEnv *env, jobject myobj, jbyteArray frameData)
    {

     jclass clazz;
     jmethodID cid;
     jobject jCoreOut;
     static const char* const strClassName = "android/sdk/Core/CoreOutput";
     clazz = env->FindClass(strClassName);
     if (clazz == NULL) {
      LOGE("Can't find class CoreOutput");

     }
     cid = env->GetMethodID(clazz,"<init>", "()V");
     jCoreOut = env->NewObject(clazz, cid);


     // Free local references 
        env->DeleteLocalRef(clazz);

     return jCoreOut;

    }

I have the methods descriptors defined in the following way:

    static const JNINativeMethod gMethods[] = {
        { "CreateCore", "(III)V", (void*) Java_android_sdk_Core_CreateCore },
  { "ProcessFrame", "([B)Landroid/sdk/Core/CoreOutput;", (void*) Java_android_sdk_Core_ProcessFrame }
 };

I'm performing the method registration by calling on:

     if (env->RegisterNatives(clazz, gMethods,
    sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
 {
  LOGE("Failed registering JNI methods");
  return result;
 }

And the registration for my other native methods is successful. (I'm able to use them...)

When I try to invoke the ProcessFrame method I get the following output from Logcat:

11-23 16:10:10.139: ERROR/AndroidRuntime(4923): java.lang.UnsatisfiedLinkError: ProcessFrame

I'm sure it has something to do with me not defining the method correctly. Can anyone shed some light on this?

Can anyone point me to a good tutorial which covers more than handling of primitive types in JNI?

Thank you,

Itamar

Upvotes: 3

Views: 2936

Answers (1)

user489041
user489041

Reputation: 28294

UnsatisfiedLinkError is thrown when the JVM cant find the method. So it has to do with your method declaration. Look into javah for creating the function header for you. Look into this: javah

Upvotes: 1

Related Questions