我有一个包含本地方法的libmath.so文件,需要在我的安卓代码中调用这些本地方法。我创建了一个示例安卓应用程序,并将libmath.so文件添加到libs/armeabi文件夹中,然后运行“右键单击鼠标” -> Android工具 -> 添加本地支持。现在,在应用程序的jni文件夹中创建了以下文件及其内容。
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmath
LOCAL_SRC_FILES := libmath.cpp
include $(BUILD_SHARED_LIBRARY)
libmath.cpp
#include <jni.h>
当我将项目运行为Android应用程序时,控制台显示如下:
17:58:41 **** Build of configuration Default for project math ****
"F:\\Vinay\\Softwares\\android-ndk-r10d\\ndk-build.cmd" all
[armeabi] Compile++ thumb: math <= libmath.cpp
[armeabi] StaticLibrary : libstdc++.a
[armeabi] SharedLibrary : libmath.so
[armeabi] Install : libmath.so => libs/armeabi/libmath.so
17:58:53 Build Finished (took 11s.695ms)
以下是我加载库的代码:
public class MathJni {
static {
System.loadLibrary("math");
}
public native String calc(String paramString);
}
public static final MathJni math = new MathJni();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s= math.calc("help");
}
当我在模拟器上运行安卓应用程序后,启动应用程序时出现以下错误。
02-06 07:38:36.900: D/dalvikvm(831): Trying to load lib /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848
02-06 07:38:36.900: D/dalvikvm(831): Added shared lib /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848
02-06 07:38:36.910: D/dalvikvm(831): No JNI_OnLoad found in /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848, skipping init
02-06 07:38:37.540: W/dalvikvm(831): No implementation found for native Lcom/example/math/MathJni;.calc:(Ljava/lang/String;)Ljava/lang/String;
02-06 07:38:37.540: D/AndroidRuntime(831): Shutting down VM
02-06 07:38:37.550: W/dalvikvm(831): threadid=1: thread exiting with uncaught exception (group=0xb3a20ba8)
02-06 07:38:37.570: E/AndroidRuntime(831): FATAL EXCEPTION: main
02-06 07:38:37.570: E/AndroidRuntime(831): Process: com.example.math, PID: 831
02-06 07:38:37.570: E/AndroidRuntime(831): java.lang.UnsatisfiedLinkError: Native method not found: com.example.math.MathJni.calc:(Ljava/lang/String;)Ljava/lang/String;
02-06 07:38:37.570: E/AndroidRuntime(831): at com.example.math.MathJni.calc(Native Method)
02-06 07:38:37.570: E/AndroidRuntime(831): at com.example.math.MainActivity.onCreate(MainActivity.java:16)
可能出现此错误的原因是什么?