1

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

致命异常:主要 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: at java.lang.System.loadLibrary(System.java:1669) E AndroidRuntime: at com.google.android.play.core.splitinstall.SplitInstallHelper.loadLibrary(Unknown Source: 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(Native Method) 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()
    }

然后在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})

假人.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());
};
4

1 回答 1

0

好的,所以当我偶然发现这个Github 问题时,我正在拼命寻找解决方案。显然,在我尝试加载opencv_java4之前,Android 要求我加载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")
}

它奏效了!

于 2020-04-03T06:17:10.163 回答