将表格3.4.3更新为4.1.1后无法检测到OpenCV库

时间:2019-08-28 19:16:52

标签: android opencv opencv4android

我已经将OpenCV模块依赖项从3.4.3更新为4.1.1,现在我收到消息了

  

找不到OpenCV Manager软件包

尽管我已包含所有libopencv_java4.so本机库。

在以前的版本(3.4.x)中,仅当我省略包含本机import UIKit class TabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() setupTabBar() tabBar.barTintColor = UIColor(red: 0.1255, green: 0.1608, blue: 0.149, alpha: 1) } func setupTabBar() { let homeController = UINavigationController(rootViewController: HomeViewController()) homeController.tabBarItem.image = UIImage(named: "homeglyph_green") homeController.tabBarItem.selectedImage = UIImage(named: "homeglyph_lightgreen") let statsController = UINavigationController(rootViewController: StatsViewController()) statsController.tabBarItem.image = UIImage(named: "statsglyph_green") statsController.tabBarItem.selectedImage = UIImage(named: "statsglyph_lightgreen") viewControllers = [homeController, statsController] guard let items = tabBar.items else { return } for item in items { item.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0) } } } 库时才需要包管理器,但是对于此版本(4.1.1)似乎没有任何区别,要求使用OpenCV每次都是经理。

我不希望该应用依赖于单独的OpenCV管理器。我该如何解决该错误?

错误日志为:

*.so

编辑

该问题与模块配置无关,而是由OpenCV本机库崩溃导致的,由于某些原因,以前的3.4.x版本不会发生这种情况。发生此错误后,OpenCV模块的重新初始化失败,触发此错误。

1 个答案:

答案 0 :(得分:2)

我创建了一个Android C ++ / C项目,我意识到要使用 openCV 4.1.1 ,我们需要在 gradle 中强制使用 CMAKE 。强>。因此,如果我们创建一个 dummy CMAKE 和一个 dummy-lib ,则会将 libc ++ _ shared.so 库添加到APK。

然后,首先在 /中创建带有 CMakeLists.txt dummy-lib.cpp 文件的 cpp 文件夹。 app / src / main / 目录。

enter image description here

CMakeLists.txt 是一个虚拟文件,放在其中:

cmake_minimum_required(VERSION 3.4.1)


add_library( # Sets the name of the library.
        dummy-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        dummy-lib.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.
        dummy-lib

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

dummy-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());
};

然后,在应用程序的 gradle 文件中添加:

android {
    ....

    defaultConfig {

        ...

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
                arguments "-DANDROID_STL=c++_shared"
            }
        }

    }

   ...

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }



}

因此,创建项目,一切就完成了!

如果您分析生成的 APK ,则可以在其中看到库 libc ++ _ shared.so

enter image description here

最后,sample Android Project