Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot detect OpenCV libs after update form 3.4.3 to 4.1.1

I have updated OpenCV module dependencies from 3.4.3 to 4.1.1 and now I get the message

OpenCV Manager package was not found

Despite I have included all libopencv_java4.so native libraries.

With previous version (3.4.x) the package manager was required only if I omit to include native *.so libraries, but with this version (4.1.1) seems doesn't make any difference, asking for OpenCV Manager every time regardless.

I don't want that app depends on a separate OpenCV Manager. How could I fix this error?

The error log is:

OpenCV error: Cannot load info library for OpenCV
W/System.err: java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found
W/System.err:     at java.lang.Runtime.loadLibrary0(Runtime.java:1071)
W/System.err:     at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
W/System.err:     at java.lang.System.loadLibrary(System.java:1667)
W/System.err:     at org.opencv.android.StaticHelper.loadLibrary(StaticHelper.java:64)
W/System.err:     at org.opencv.android.StaticHelper.initOpenCVLibs(StaticHelper.java:95)
W/System.err:     at org.opencv.android.StaticHelper.initOpenCV(StaticHelper.java:39)
W/System.err:     at org.opencv.android.OpenCVLoader.initDebug(OpenCVLoader.java:107)
W/System.err:     at com.mysite.myapp.OpenCVTestActivity.onResume(OpenCVTestActivity.java:144)
W/System.err:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1446)
W/System.err:     at android.app.Activity.performResume(Activity.java:7939)
W/System.err:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4195)
W/System.err:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
W/System.err:     at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
W/System.err:     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:107)
W/System.err:     at android.os.Looper.loop(Looper.java:214)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7356)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

EDIT

The problem isn't related to module configuration, but is caused by a crash of the OpenCV native library, that for some reason doesn't happen with previous 3.4.x version. After this error the re-initialization of OpenCV module fails triggering this error.

like image 845
AndreaF Avatar asked Aug 28 '19 19:08

AndreaF


1 Answers

I created an Android C++/C project and I realized that to use openCV 4.1.1 we need to force using the CMAKE in the gradle. So if we create a dummy CMAKE and a dummy-lib, the library libc++_shared.so is added to the APK.

Then, first create a cpp folder with a CMakeLists.txt and dummy-lib.cpp files inside in the /app/src/main/ directory.

enter image description here

The CMakeLists.txt is a dummy file, inside put this:

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})

In the dummy-lib.cpp add:

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

Then, in the gradle file of the app add:

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"
        }
    }



}

So, Make the project and all is done!

If you analyze the generated APK, you can see the library libc++_shared.so inside!

enter image description here

Lastly, the sample Android Project.

like image 152
jnfran92 Avatar answered Nov 11 '22 02:11

jnfran92