通过动态交付功能加载opencv_java4.so文件时出错

时间:2020-04-01 14:06:15

标签: android opencv unsatisfiedlinkerror dynamic-delivery

我正在使用Android的动态投放功能,因为openCV会使应用程序尺寸过大。问题是当我尝试使用SplitInstallHelper.loadLibrary(this, "opencv_java4")加载opencv时。我给我这个错误。

致命异常:主要 E AndroidRuntime:进程:com.example.com,PID:7470 E AndroidRuntime:java.lang.UnsatisfiedLinkError:dlopen失败:未找到库“ libc ++ _ shared.so” E AndroidRuntime:位于java.lang.Runtime.loadLibrary0(Runtime.java:1016) E AndroidRuntime:位于java.lang.System.loadLibrary(System.java:1669) E AndroidRuntime:位于com.google.android.play.core.splitinstall.SplitInstallHelper.loadLibrary(未知源:0) E AndroidRuntime:位于org.opencv.com.example.com.TempActivity.onResume(TempActivity.kt:615) E AndroidRuntime:位于android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1434) E AndroidRuntime:位于android.app.Activity.performResume(Activity.java:7304) E AndroidRuntime:位于android.app.ActivityThread.performResumeActivity(ActivityThread.java:3993) E AndroidRuntime:位于android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4033) E AndroidRuntime:位于android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) E AndroidRuntime:位于android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) E AndroidRuntime:位于android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) E AndroidRuntime:位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1977) E AndroidRuntime:位于android.os.Handler.dispatchMessage(Handler.java:106) E AndroidRuntime:位于android.os.Looper.loop(Looper.java:193) E AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:6923) E AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法) E AndroidRuntime:位于com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) E AndroidRuntime:位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

我的 onCreate 看起来像这样

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    try {
        SplitInstallHelper.loadLibrary(this, "opencv_java4")
    } catch (exception: Exception) {
        exception.printStackTrace()
    }

然后将 openCV 库加载到 onResume

 override fun onResume() {
    super.onResume()

    if (!OpenCVLoader.initDebug()) {
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback)
    } else {
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS)
    }
}

这是我 openCV 模块的 defaultConfig

defaultConfig {
    externalNativeBuild {
        cmake {
            cppFlags ""
            arguments "-DANDROID_ARM_NEON=TRUE",'-DANDROID_STL=c++_shared'
            targets "opencv_jni_shared"
        }
    }

    ndk {
        abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
    }
}

一些资源要求更改CMakeList.txt和dummy.cpp文件的

CmakeList.txt

cmake_minimum_required(VERSION 3.6)

# dummy target to bring libc++_shared.so into packages
add_library(opencv_jni_shared STATIC dummy.cpp)

find_library( # Sets the name of the path variable.
    log-lib

    # Specifies the name of the NDK library that
    # you want CMake to locate.
    log)

    target_link_libraries( # Specifies the target library.
    opencv_jni_shared

    # Links the target library to the log library
    # included in the NDK.
    ${log-lib})

dummy.cpp

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
whatever(
    JNIEnv *env,
    jobject /* this */){
std::string hello = "Hello";
return env->NewStringUTF(hello.c_str());
};

1 个答案:

答案 0 :(得分:0)

好的,所以当我偶然发现这个Github issue时,我拼命寻找解决方案。 所以很明显,Android要求我先加载 libc ++ _ shared.so 文件,然后再尝试加载 opencv_java4

这是我对加载 opencv

的“活动”所做的更改
 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    SplitCompat.installActivity(this)
//This is the line I added before loading "opencv_java4"
    SplitInstallHelper.loadLibrary(this, "c++_shared")

    SplitInstallHelper.loadLibrary(this, "opencv_java4")
}

成功了!