java.lang.UnsatisfiedLinkError: dlopen failed: lib

2019-08-25 10:57发布

问题:

I am working on an android app that is supposed to detect if a face is in front of the camera and then perform some action based on it. I am using open cv for the face detection but I need some costume cpp functionality. So I am trying to import the open cv stuff into my own cpp file myLib.cpp. I then want to call the function defined in myLib in my mainActivity.

When I build the project everything works fine but as soon as I run it on my device (Oneplus x - Android 22) it immediately crashes with the following error message.

04-12 10:28:38.494 11114-11114/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.lunaticcoding.opencvtest, PID: 11114 java.lang.UnsatisfiedLinkError: dlopen failed: library "libopencv_java3.so" not found at java.lang.Runtime.loadLibrary(Runtime.java:371) at java.lang.System.loadLibrary(System.java:988) at com.lunaticcoding.opencvtest.MainActivity.<clinit>(MainActivity.java:19) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1606) at android.app.Instrumentation.newActivity(Instrumentation.java:1066) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424) at android.app.ActivityThread.access$900(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:139) at android.app.ActivityThread.main(ActivityThread.java:5298) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

my opencv build gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27



    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
            }
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    sourceSets {
        main {
            jni.srcDirs = [jni.srcDirs, 'src/sdk/native/jni/include']
            jniLibs.srcDirs = [jniLibs.srcDirs, 'src/sdk/native/3rdparty/libs', 'src/sdk/native/libs']
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

my Opencv cMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)
set(OpenCV_DIR "/Users/lunaticcoding/Documents/OpenCV-android-sdk/sdk/native/jni")
find_package(OpenCV REQUIRED java)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})

The problem is that the libopencv_java3.so does not get created in my build. So the problem must be in the cmakelists of the opencv project. Does anyone know how to export the libopencv_java3.so file into my app build?

回答1:

You have to install OpenCV Manager on your device.

And initiate manager like this

OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);

BaseLoaderCallback is

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

See reference page