Using jni on Android: UNsatisfiedLinkError

I am new to jni and I have been going through a tutorial to implement a simple native method, but I get unsatisfied linuncerror. As far as I know, I definitely followed the steps in the textbook. Please help me.

Here is the java shell code:

package com.cookbook.jni;

public class SquaredWrapper {

    // Declare native method (and make it public to expose it directly)
    public static native int squared(int base);

   // Provide additional functionality, that "extends" the native method
   public static int to4(int base)
   {
      int sq = squared(base);
      return squared(sq);
   }

   // Load library
   static {
      System.loadLibrary("squared");
   }
}

Here is what my Android.mk file looks like:

LOCAL_PATH: = $ (call my-dir)

includes $ (CLEAR_VARS)

LOCAL_MODULE: = square LOCAL_SRC_FILES: = squared.c

includes $ (BUILD_SHARED_LIBRARY)

This is what my .c file looks like:

#include "squared.h"
#include <jni.h>

JNIEXPORT jint JNICALL Java_org_edwards_1research_demo_jni_SquaredWrapper_squared
  (JNIEnv * je, jclass jc, jint base)
{
     return (base*base);
}

And here is what my .h file looks like:

enter code here/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_cookbook_jni_SquaredWrapper */

#ifndef _Included_com_cookbook_jni_SquaredWrapper
#define _Included_com_cookbook_jni_SquaredWrapper
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_cookbook_jni_SquaredWrapper
* Method:    squared
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_cookbook_jni_SquaredWrapper_squared
  (JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif
+5
source share
1 answer

Your JNI signature does not match. In your .c file, change:

JNIEXPORT jint JNICALL Java_org_edwards_1research_demo_jni_SquaredWrapper_squared

to

JNIEXPORT jint JNICALL Java_com_cookbook_jni_SquaredWrapper_squared

, "" C JNI Java. , , , JNI Java. - , Java JNI .

, Java- ( .c ):

#include "squared.h"
#include <jni.h>

static const char* SquaredWrapper = "com/cookbook/jni/SquaredWrapper";

jint squared(JNIEnv * env, jobject this, jint base) {
     return (base*base);
}

// Methods to register for SquaredWrapper
static JNINativeMethod SquareWrapperMethods[] = {
        {"squared", "(I)I", squared}
};

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env;
    if ( (*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK)
        return JNI_ERR;

    jclass class = (*env)->FindClass(env, SquaredWrapper);
    (*env)->RegisterNatives(env, class, SquaredWrapperMethods, sizeof(SquaredWrapperMethods)/sizeof(SquaredWrapperMethods[0]));

    return JNI_VERSION_1_6;
}

void JNI_OnUnload(JavaVM* vm, void* reserved) {
    JNIEnv* env;
    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
        return;

    jclass class = (*env)->FindClass(env, SquaredWrapper);
    (*env)->UnregisterNatives(env, class);

    return;

}

, . , . 4- const char * SquaredWrapper - , . JNI_OnLoad JNI_OnUnLoad, . - JNINativeMethod. 3, Java- const char *, JNI Java C . JNI Java. "(Arg1Arg2Arg3...) Ret", , int double float, "(ID) F", , void, "() V". , :

http://dev.kanngard.net/Permalinks/ID_20050509144235.html

:)

: , BTW, , JNI_OnLoad JNI_UnOnLoad , .c .

+8

All Articles