异常 Ljava/lang/UnsatisfiedLinkError

Exception Ljava/lang/UnsatisfiedLinkError

我目前正在尝试在 AT&T Samsung Galaxy S3 上构建和 运行 这个项目:https://github.com/arrayfire/androidcl。问题是当我尝试 运行ning: ./ndk-build -C ~/Desktop/ArrayFire/androidcl/jni

然后我尝试 运行 它在 phone 上 "Run -> Run as -> Android Application."

我得到一个 "Unfortunately, droidcl has stopped working." 我是个菜鸟,但经过几天的研究,我认为这与 NDK 有关。在 LiveFeatureActivity 的第 19 行,"System.loadLibrary("JNIProcessor");"是我最大的怀疑。

所有文件都在 Git 上。帮助将不胜感激,因为我尝试 运行 它 3 天但无济于事。

这是 ndk-build 所说的:

./ndk-build -C ~/Desktop/ArrayFire/androidcl/jni
Android NDK: WARNING:/home/laptop/Desktop/ArrayFire/androidcl/jni/Android.mk:JNIProcessor: non-system libraries in linker flags: /usr/lib/libOpenCL.so    
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES    
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the    
Android NDK:     current module    
make: Entering directory `/home/laptop/Desktop/ArrayFire/androidcl/jni'
[armeabi] Compile++ arm  : JNIProcessor <= processor.cpp
[armeabi] SharedLibrary  : libJNIProcessor.so
arm-linux-androideabi-g++: error: /usr/lib/libOpenCL.so: No such file or directory
make: *** [/home/laptop/Desktop/ArrayFire/androidcl/obj/local/armeabi/libJNIProcessor.so] Error 1
make: Leaving directory `/home/laptop/Desktop/ArrayFire/androidcl/jni'

这是 Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := JNIProcessor
LOCAL_ARM_MODE := arm
LOCAL_SRC_FILES := processor.cpp
LOCAL_CPPFLAGS := -DARM -DOS_LNX -DARCH_32 -fexceptions
LOCAL_CPPFLAGS += -I$(LOCAL_PATH)/include
LOCAL_CPPFLAGS  += -fexceptions
LOCAL_LDLIBS := -ljnigraphics -llog $(LOCAL_PATH)/libs/libOpenCL.so
include $(BUILD_SHARED_LIBRARY) 

我尝试进入 Android.mk 文件并直接指向 /usr/lib/libOpenCl.so(在检查库是否存在后)但仍然不起作用。

这是 logcat 文件:

02-06 22:52:27.515: D/ActivityThread(3233): setTargetHeapUtilization:0.25
02-06 22:52:27.515: D/ActivityThread(3233): setTargetHeapIdealFree:8388608
02-06 22:52:27.515: D/ActivityThread(3233): setTargetHeapConcurrentStart:2097152
02-06 22:52:27.575: W/dalvikvm(3233): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/example/LiveFeatureActivity;
02-06 22:52:27.575: W/dalvikvm(3233): Class init failed in newInstance call (Lcom/example/LiveFeatureActivity;)
02-06 22:52:27.575: D/AndroidRuntime(3233): Shutting down VM
02-06 22:52:27.575: W/dalvikvm(3233): threadid=1: thread exiting with uncaught exception (group=0x40cb6300)
02-06 22:52:27.575: E/AndroidRuntime(3233): FATAL EXCEPTION: main
02-06 22:52:27.575: E/AndroidRuntime(3233): java.lang.ExceptionInInitializerError
02-06 22:52:27.575: E/AndroidRuntime(3233):  at java.lang.Class.newInstanceImpl(Native Method)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at java.lang.Class.newInstance(Class.java:1319)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2090)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.app.ActivityThread.access0(ActivityThread.java:142)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.os.Looper.loop(Looper.java:137)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at android.app.ActivityThread.main(ActivityThread.java:4931)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at java.lang.reflect.Method.invokeNative(Native Method)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at java.lang.reflect.Method.invoke(Method.java:511)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at dalvik.system.NativeStart.main(Native Method)
02-06 22:52:27.575: E/AndroidRuntime(3233): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load JNIProcessor: findLibrary returned null
02-06 22:52:27.575: E/AndroidRuntime(3233):  at java.lang.Runtime.loadLibrary(Runtime.java:365)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at java.lang.System.loadLibrary(System.java:535)
02-06 22:52:27.575: E/AndroidRuntime(3233):  at com.example.LiveFeatureActivity.<clinit>(LiveFeatureActivity.java:19)
02-06 22:52:27.575: E/AndroidRuntime(3233):  ... 15 more
02-06 22:52:43.723: I/Process(3233): Sending signal. PID: 3233 SIG: 9

您在 运行 应用程序时收到的 UnsatisfiedLinkError 只是因为您的模块未能构建,因此未包含在应用程序中。将 NDK 构建到 link 而不是 libOpenCL.so 应该可以解决这个问题。

您首先需要获得一个 libOpenCL.so 到 link 反对,您应该能够在目标设备上找到它(如果它不存在,那么这个应用程序将无法运行反正)。在我的设备上,OpenCL 库位于 /system/vendor/lib/libOpenCL.so 中,但您可能需要四处寻找 adb shell 才能找到它。将其复制到您的本地计算机 运行 adb pull /system/vendor/lib/libOpenCL.so.

如果您将此库放在 jni/ 目录中,那么您只需要在 LOCAL_LDLIBS 变量中包含 libOpenCL.so。否则,我通常只是在 NDK 平台目录中粘贴此库的副本(例如 $NDK_DIR/platforms/android-17/arch-arm/usr/lib/),然后通过将 -lOpenCL 添加到您的 LOCAL_LDLIBS variable 即可供所有应用程序使用。在这两种情况下,您可能会在 linker 标志中收到有关非系统库的警告,但忽略这些应该是安全的。

@Cookiez 不确定您是通过 Google 搜索偶然发现了这个存储库,还是在阅读了我写的关于它的博客 post 之后。如果您不知道 post,请通过 this post 了解如何在 Android 设备上获取代码 运行。如果您仍然无法获取该应用,请告诉我 运行。

普拉迪普