简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM