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

Error while loading opencv_java4.so file by dynamic delivery feature

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

FATAL EXCEPTION: main E AndroidRuntime: Process: com.example.com, PID: 7470 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found E AndroidRuntime: at java.lang.Runtime.loadLibrary0(Runtime.java:1016) E AndroidRuntime: at java.lang.System.loadLibrary(System.java:1669) E AndroidRuntime: at com.google.android.play.core.splitinstall.SplitInstallHelper.loadLibrary(Unknown Source:0) E AndroidRuntime: at org.opencv.com.example.com.TempActivity.onResume(TempActivity.kt:615) E AndroidRuntime: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1434) E AndroidRuntime: at android.app.Activity.performResume(Activity.java:7304) E AndroidRuntime: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3993) E AndroidRuntime: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4033) E AndroidRuntime: at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977) E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) E AndroidRuntime: at android.os.Looper.loop(Looper.java:193) E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6923) E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) E AndroidRuntime: at 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()
    }

然后在onResume

中加载openCV
 override fun onResume() {
    super.onResume()

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

这是我在 gradle 中用于 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());
};

好的,当我偶然发现这个 Github issue 时,我正在拼命寻找解决方案。 所以显然 Android 在我尝试加载 opencv_java4 之前要求我加载 libc++_shared.so 文件。

这是我对正在加载 opencv

的 Activity 所做的更改
 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")
}

成功了!