Search code examples
androidgradlejava-native-interfacelibraries

Error after including a 2nd JNI library to my Android project (OpenCV)


I'm trying to add OpenCV to an existing Android project of mine but while merging them I ran into the following error:

12-08 16:15:21.951 22052-22052/ai.inbi.face_recognition_robot E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: ai.inbi.wonderful_face_recognition_robot, PID: 22052
                                                                                      java.lang.UnsatisfiedLinkError: Couldn't load uvcNative from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/ai.inbi.wonderful_face_recognition_robot-1.apk"],nativeLibraryDirectories=[/data/app-lib/ai.inbi.wonderful_face_recognition_robot-1, /vendor/lib, /system/lib]]]: findLibrary returned null
                                                                                          at java.lang.Runtime.loadLibrary(Runtime.java:358)
                                                                                          at java.lang.System.loadLibrary(System.java:526)

I tried to manually comment out all references to OpenCV but unless I delete all OpenCV files from my libs folder the existing library (com.qihancloud.opensdk) fails to find it's own library functions. The folder structure of my libraries is like this: project folder structure

If I remove the OpenCV library files before compiling everything works well. Also my standalone OpenCV project is working but as soon as I try to combine them it fails to execute.

My app build.gradle file can be seen here: https://pastebin.com/Z7hPH3vy

And my CMakeLists.txt https://pastebin.com/gGwgWtxP


Solution

  • The error means that the file libuvcNative.so has not been installed with your APK. This can happen for a wild variety of root causes.

    Your case is exactly same as described here. The QihanOpenSDK_1.1.8.0.aar only has an armeabi version of libuvcNative.so. The fix is to change line #15 of build.gradle to read

     abiFilters 'armeabi'
    

    But I must confess that your CMakeLists.txt puzzled me. For me,

    set_target_properties(lib_qihan PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR} /libs/QihanOpenSDK_1.1.8.0.aar)
    

    does not work. To be sincere,

    set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR} /libs/${ANDROID_ABI}/libopencv_java3.so)
    

    also does not match my books. For the latter, I believe that you simply added a space while copy/pasting the script.

    For the former, I know a special trick to have an so file from an imported aar used in native build.

    I believe that your working version not only discards all OpenCV libraries, but also does not build the libnative-lib.so. This way, cmake does never look for libuvcNative.so, but still this native lib is deployed with the APK because the aar is a compiled dependency of your app. I guess that some of the QihanOpenSDK classes explicitly calls

    System.loadLibrary("uvcNative")
    

    If your libnative-lib.so does not use external symbols from libuvcNative.so, you don't need the trick I mentioned above and don't need to mention the QihanOpenSDK at all in your CMakeLists.txt.