Search code examples
javaandroidclassloader

java.lang.UnsatisfiedLinkError: Couldn't load native_sample from loader


The errors:

Process: com.example.syafiq.opencvoi, PID: 7760
java.lang.UnsatisfiedLinkError: Couldn't load native_sample from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.example.syafiq.opencvoi-13.apk,libraryPath=/data/app-lib/com.example.syafiq.opencvoi-13]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
at com.example.syafiq.opencvoi.Sample3Native$1.onManagerConnected(Sample3Native.java:79)
at org.opencv.android.AsyncServiceHelper$3.onServiceConnected(AsyncServiceHelper.java:319)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1114)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1131)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

The sample3Native.java line 79 were:

 public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");

                // Load native library after(!) OpenCV initialization
                System.loadLibrary("native_sample");

And the AsyncServiceHelper.Java line 319 were

mUserAppCallback.onManagerConnected(status);

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include ../../sdk/native/jni/OpenCV.mk
LOCAL_MODULE    := native_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS +=  -llog -ldl
include $(BUILD_SHARED_LIBRARY)

And application.mk

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi armeabi-v7a
LOCAL_ARM_NEON := true

There's no errors in the codes. I've tried several solution but yet the result are still the same, The codes was obtained from open source website. I'm not good enough with android studio, and I'm still learning. I hope you guys can help me to solve this error. I really appreciate your help and consideration to help me and solve my error. I appreciate your time :)


Solution

  • From my perspective something wrong with versions of your "native_sample" library. As it's written in documentation

    Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.

    It's possible that there are both versions in your classpath, and jvm loads wrong version. So it finds library but during class-loading process find inconsistency, probably required method was added in later lib version.

    I suggest trying this:

    System.load(String path) //with an absolute path to needed lib
    

    Also see: Difference between System.load() and System.loadLibrary in Java