AndroidRuntime: java.lang.UnsatisfiedLinkError: Native metho

AndroidRuntime: java.lang.UnsatisfiedLinkError: Native method not found: com.example.compute.UseNDK.AddNumbers:(II)I

本文关键字:Native metho UnsatisfiedLinkError lang java AndroidRuntime      更新时间:2023-10-16

我有一个带有本机函数的小 JNI 文件,该文件添加两个变量并打印结果。但是当我运行这个程序时,主要活动是运行并从用户那里获取输入,但是当我按下添加按钮时,它会给出此错误 AndroidRuntime: java.lang.UnsatisfiedLinkError: 找不到本机方法: com.example.compute.UseNDK.AddNumbers:(II)我请帮助我解决这个领域的新问题。

主要活动.java

package com.example.compute;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.util.Log; /*import log class*/

public class MainActivity extends Activity implements OnClickListener
{
//   static 
//    {
//      System.loadLibrary("add");
//    }
//   
//   private native int AddNumbers(int value1,int value2);
//@Override

    private static final String TAG = MainActivity.class.getName();
    private Button btnCalculate;
    private EditText editResult;
    private EditText editV1;
    private EditText editV2;
    private TextView result;    
    UseNDK ntv = new UseNDK();
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnCalculate = (Button)findViewById(R.id.btnAdd);
            // editResult = (EditText)findViewById(R.id.textResult);
            editV1 = (EditText)findViewById(R.id.editV1);
            editV2 = (EditText)findViewById(R.id.editV2);
            result = (TextView)findViewById(R.id.textResult);
            btnCalculate.setOnClickListener(this);
        }
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                if(v.getId() == R.id.btnAdd)
                {
                    int v1 , v2 , res = 0;
                    v1 = Integer.parseInt(editV1.getText().toString());
                    v2 = Integer.parseInt(editV2.getText().toString());
                    res = ntv.AddNumbers(v1, v2);
                    result.setText(new Integer(res).toString());
                }
            }
    @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
}

使用NDK.java

package com.example.compute;
import android.util.Log;
public class UseNDK 
{
    static 
    {
        try
        {
            System.loadLibrary("add");
        }
        catch (UnsatisfiedLinkError e) 
        {
            System.err.println("public native code library failed to load.n" + e);
            Log.e("Tag",e.getMessage());
        }
    }
    public native int AddNumbers(int value1 , int value2);
}

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE    := add
LOCAL_LDLIBS := -llog 
LOCAL_SRC_FILES := add.cpp
                   add.h
include $(BUILD_SHARED_LIBRARY)

添加.cpp

#include <string.h>
#include <jni.h>
#include <stdlib.h>
#include <stdio.h>
#include <add.h>
#include <android/log.h>
JNIEXPORT jint JNICALL Java_com_example_compute_MainActivity_AddNumbers(JNIEnv* env, jobject obj, jint v1, jint v2)
{
     __android_log_print(ANDROID_LOG_ERROR, "MyTag", "The value is %d", 1+1);
     //return -1;
     return (V1 + V2);
}

添加 H

JNIEXPORT jint Java_com_example_compute_MainActivity_AddNumbers(JNIEnv *env, jobject obj, jint , jint );

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <EditText
        android:id="@+id/editV2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_marginTop="24dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editV2"
        android:layout_below="@+id/editV2"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="number" >
        <requestFocus />
    </EditText>
    <TextView
        android:id="@+id/textResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editV1"
        android:layout_marginTop="32dp"
        android:text="" />
    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="63dp"
        android:text="Add" />
</RelativeLayout>

日志猫错误

 D/dalvikvm(1237): Added shared lib /data/app-lib/com.example.compute-1/libadd.so 0xb3d45580
 D/dalvikvm(1237): No JNI_OnLoad found in /data/app-lib/com.example.compute-1/libadd.so 0xb3d45580, skipping init
D/gralloc_goldfish(1237): Emulator without GPU emulation detected.
D/dalvikvm(1282): Trying to load lib /data/app-lib/com.example.compute-2/libadd.so 0xb3d46408
Added shared lib /data/app-lib/com.example.compute-2/libadd.so 0xb3d46408
D/dalvikvm(1282): No JNI_OnLoad found in /data/app-lib/com.example.compute-2/libadd.so 0xb3d46408, skipping init
D/gralloc_goldfish(1282): Emulator without GPU emulation detected.
W/dalvikvm(1282): No implementation found for native Lcom/example/compute/UseNDK;.AddNumbers:(II)I
 D/AndroidRuntime(1282): Shutting down VM
W/dalvikvm(1282): threadid=1: thread exiting with uncaught exception (group=0xb3a94ba8)
E/AndroidRuntime(1282): FATAL EXCEPTION: main
E/AndroidRuntime(1282): Process: com.example.compute, PID: 1282
E/AndroidRuntime(1282): java.lang.UnsatisfiedLinkError: Native method not found: com.example.compute.UseNDK.AddNumbers:(II)I
E/AndroidRuntime(1282): at com.example.compute.UseNDK.AddNumbers(Native Method)
E/AndroidRuntime(1282): at com.example.compute.MainActivity.onClick(MainActivity.java:63)
 E/AndroidRuntime(1282): at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(1282): at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(1282): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(1282): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(1282): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1282): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1282): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1282): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1282): at om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1282): at dalvik.system.NativeStart.main(Native Method)
I/Process(1282): Sending signal. PID: 1282 SIG: 9

也许这是一个迟到的答案,但你可以像这样将 cpp 方法包装在 extern "c" 块中:

extern "c" {
JNIEXPORT jint JNICALL Java_com_example_compute_MainActivity_AddNumbers(JNIEnv* env, 
jobject obj, jint v1, jint v2)
{
 __android_log_print(ANDROID_LOG_ERROR, "MyTag", "The value is %d", 1+1);
 return (V1 + V2);
}
}

连同@John·维索斯基的回应

然后生成应用程序。

编号: 1

在 add.cpp 中,声明:

Java_com_example_compute_MainActivity_AddNumbers

但该方法实际上是在 UseNDK 中,所以它应该是:

Java_com_example_compute_UseNDK_AddNumbers