java.lang.UnsatisfiedLinkError:未找到本机方法:jni()中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13248288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:01:36  来源:igfitidea点击:

java.lang.UnsatisfiedLinkError: Native method not found: Error in jni()

javaandroidjava-native-interface

提问by kapil Gupta

I m trying to do addition of two 2D Array with the use of jni with an android activity. in the android main activity class i make an object of class sample.java. And pass the field of sample class instance to the native code for addition. the sample.java as follows

我正在尝试使用 jni 和一个 android 活动来添加两个 2D 数组。在 android 主活动类中,我创建了一个类 sample.java 的对象。并将示例类实例的字段传递给原生代码进行加法。sample.java 如下

    package com.cdacb.mars.ntvoperation;

    public class Sample {

        public int[][] array1;
        public int[][] array2;
        public int[][] array3;
    }

the definition for native code is :

本机代码的定义是:

JNIEXPORT void JNICALL
Java_com_cdacb_mars_ntvoperation_Operation_int2dAddition
(JNIEnv *env,jobject this,jobject object ){

            jclass class = (*env)->GetObjectClass(env,object);
            if(class==Null){
                printf("Error in finding class");
                return 0;
            }

            jfieldID fid1= (*env)->GetFieldID(env , class ,"array1","[[I");
            if(fid1==Null){
                    printf("Error in finding mat1 field ");
                    return 0;
                }

            jfieldID fid2= (*env)->GetFieldID(env , class ,"array2","[[I");
                if(fid2==Null){
                        printf("Error in finding mat2 field ");
                        return 0;
                    }

            jfieldID fid3= (*env)->GetFieldID(env , class ,"array3","[[I");
                    if(fid3==Null){
                            printf("Error in finding mat3 field ");
                            return 0;
                        }


            jobjectArray arr1= (jobjectArray)(*env)->GetObjectField(env,this,fid1);
            jobjectArray arr2= (jobjectArray)(*env)->GetObjectField(env,this,fid2);
            jobjectArray class= (*env)->NewObjectArray(env,length,Class,NULL);

            for(jint i=0; i<16; i++){
                class[i]=arr1[i]+arr2[i];
            }

            (*env)->SetObjectField(env, this, fid3, class);
  }

the android.mk file is

android.mk 文件是

    LOCAL_PATH := $(call my_dir)

    include $(CLEAR_VARS)

    LOCAL_MODULE    := Ntvoperation
    LOCAL_SRC_FILE  := operation.c

    include $(BUILD_SHARED_LIBRARY)

But when i run thie=s module it generate error in logcat as:

但是当我运行 thie=s 模块时,它在 logcat 中生成错误为:

11-06 14:46:35.875: D/dalvikvm(936): Trying to load lib /data/data/com.cdacb.mars.ntvoperation/lib/libNtvoperation.so 0x411e7090
11-06 14:46:35.905: W/dalvikvm(936): No implementation found for native Lcom/cdacb/mars/ntvoperation/Operation;.int2dAddition:(Lcom/cdacb/mars/ntvoperation/Sample;)V
11-06 14:46:35.924: E/AndroidRuntime(936): java.lang.UnsatisfiedLinkError: Native method not found: com.cdacb.mars.ntvoperation.Operation.int2dAddition:(Lcom/cdacb/mars/ntvoperation/Sample;)V
11-06 14:46:35.924: E/AndroidRuntime(936):  at com.cdacb.mars.ntvoperation.Operation.int2dAddition(Native Method)
11-06 14:46:35.924: E/AndroidRuntime(936):  at com.cdacb.mars.ntvoperation.Operation.onCreate(Operation.java:36)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.app.Activity.performCreate(Activity.java:5008)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.app.ActivityThread.access0(ActivityThread.java:130)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.os.Looper.loop(Looper.java:137)
11-06 14:46:35.924: E/AndroidRuntime(936):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-06 14:46:35.924: E/AndroidRuntime(936):  at java.lang.reflect.Method.invokeNative(Native Method)
11-06 14:46:35.924: E/AndroidRuntime(936):  at java.lang.reflect.Method.invoke(Method.java:511)
11-06 14:46:35.924: E/AndroidRuntime(936):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-06 14:46:35.924: E/AndroidRuntime(936):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-06 14:46:35.924: E/AndroidRuntime(936):  at dalvik.system.NativeStart.main(Native Method)

please help me for solve this problem.

请帮我解决这个问题。

my main java class is

我的主要java类是

        package com.cdacb.mars.ntvoperation;

        import android.R.string;
        import android.os.Bundle;
        import android.app.Activity;
        import android.widget.TextView;



        public class Operation extends Activity {

            private  native void int2dAddition(Sample object);


             String  display=" ";

            @Override
            public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
           //     setContentView(R.layout.main);
                TextView screen = new TextView(this);
                Sample tc=new Sample();
                tc.array1=new int[4][4];
                tc.array2=new int[4][4];
                tc.array3=new int[4][4];
               for(int col=0;col<4;col++){
                   for(int row=0;row<4;row++){
                      tc.array1[col][row]=2;
                      tc.array2[col][row]=4;
                      tc.array3[col][row]=0;

                   }
               } 

               int2dAddition(tc);

               for(int col=0;col<4;col++){
                   for(int row=0;row<4;row++){

                      display= display + tc.array3[col][row] +",";

                   }

               }      

               screen.setText(display);
               setContentView(screen);
            }


            static{
                   System.loadLibrary("Ntvoperation");
                   }

        }

回答by CCJ

This issue may be related to C++ function name decoration; if you surround your C++ functions with extern "C" {...} then you should be able to avoid that hassle and JNI should find your native methods. I was having the same issue until I read thisthread describing a similar problem but where it was verified that ANSI C native functions worked but C++ threw the native method not found error. Hope this helps, and props to @yellowstonely who provided this suggestion on the linked thread

这个问题可能与C++函数名修饰有关;如果你用 extern "C" {...} 包围你的 C++ 函数,那么你应该能够避免这种麻烦,JNI 应该找到你的本地方法。我遇到了同样的问题,直到我读到这个描述类似问题的线程,但在那里验证了 ANSI C 本机函数可以工作,但 C++ 抛出了本机方法未找到错误。希望这会有所帮助,并向在链接线程上提供此建议的 @yellowstonely 表示支持