OpenCVFor Unity / android Dynamic Lib gives DllNotFoundException

I compiled the android version of OpenCV (opencv4.5.3) and for my surprise I got the error:

Autoconnected Player DllNotFoundException: Unable to load DLL ‘opencvforunity’: The specified module could not be found.

I’m under ARMv7, I also tested ARM64 and didn’t work

It worked on Windows standalone/editor but for unity/android it doesnt.

QUESTION:

if I have unity compiling on ndk19 should I setup the build_sdk.py to build the android version targeting on ndk 19? or if I target to 18 (–config=ndk-18-api-level-21.config.py) it will be compatible with the ndk19 from unity?

For the compilation I’m using: ndk\19.2.5345600

I’m using this command line to compile:
python “C:\Users\muser.chav\Downloads\opencv4.5.3\opencv-4.53\platforms\android\build_sdk.py” “C:\Users\muser.chav\Downloads\opencv4.5.3\buildandroid2” “C:\Users\muser.chav\Downloads\opencv4.5.3\opencv-4.53” --ndk_path=“C:\Users\muser.chav\AppData\Local\Android\Sdk\ndk\19.2.5345600” --sdk_path=“C:\Users\muser.chav\AppData\Local\Android\Sdk” --extra_modules_path=“C:\Users\muser.chav\Downloads\opencv4.5.3\opencv_contrib-4.53\modules” --config=ndk-18.config.py

In the end I can properly compile the android version I get:
build\OpenCV-android-sdk\sdk\native\libs\armeabi-v7a\libopencv_java4.so
I copy this file to the Plugins folder of my project (and inside unity I import it and on the inspector I set it as a ARMv7 lib):
myunityproject\Assets\OpenCVForUnity\Plugins\Android\libs\armeabi-v7a\libopencv_java4.so

also the same with the extra Unity libopencvforunity.so:
myunityproject\Assets\OpenCVForUnity\Extra\dll_version\Android\libs\armeabi-v7a\libopencvforunity.so
To:
myunityproject\Assets\OpenCVForUnity\Plugins\Android\libs\armeabi-v7a\libopencv_java4.so

I’ve made the steps on the Readme.pdf but it doesnt work it keeps giving me DllNotFoundExceptio the steps are these:

How to use OpenCV Dynamic Link Library with customized build settings :
● Download OpenCV 4.5.3 repository ( git: opencv, opencv-contrib ).
Android

  1. Build the Android SDK with “opencv/platforms/android/build_sdk.py”. ( APP_STL
    := c++_static)
    python …/opencv/platforms/android/build_sdk.py …/build …/opencv
    –ndk_path=C://android-ndk --sdk_path=C://android-sdk
    –extra_modules_path=…/opencv_contrib/modules
    –config=ndk-18-api-level-21.config.py
  2. Copy the output file ( native\libs\arm64-v8a\libopencv_java4.so ) to
    “OpenCVForUnity\Plugins\Android\libs\arm64-v8a\”. Copy the output files (
    native\libs\arm64-v8a\libopencv_java4.so ) to
    “OpenCVForUnity\Plugins\Android\libs\armeabi-v7a\”. Copy the output files (
    native\libs\x86\libopencv_java4.so ) to
    “OpenCVForUnity\Plugins\Android\libs\x86\”.
  3. Copy “OpenCVForUnity\Extra\dll_version\Android\libs\” to
    “OpenCVForUnity\Plugins\Android\libs\”.

Is OpenCVFor Unity dynamic dll custom compilation compatible with the ndk that comes with Unity

e.g. Ndk → Pkg.Revision = 19.0.5232133

I’m getting a lot of compilation errors:


COPYFILES: Updated!
[12/22] Linking CXX shared library jni\armeabi-v7a\libopencv_java4.so
FAILED: jni/armeabi-v7a/libopencv_java4.so
cmd.exe /C "cd . && C:\NDK\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi16 --gcc-toolchain=C:/NDK/toolchains/llvm/prebuilt/windows-x86_
64 -fPIC --sysroot C:/NDK/toolchains/llvm/prebuilt/windows-x86_64/sysroot -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes
 -mfpu=vfpv3-d16 -fno-addrsig -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++    -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dt
or -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow
 -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments  -
fvisibility=hidden -fvisibility-inlines-hidden  -Wno-deprecated-declarations -Oz -DNDEBUG   -DNDEBUG  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--w
arn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now   -Wl,--as-needed -shared -Wl,-
soname,libopencv_java4.so -o jni\armeabi-v7a\libopencv_java4.so @CMakeFiles\opencv_java.rsp  && cmd.exe /C "cd /D C:\Users\felipe.chaves\Downloads\opencv4.5.2\buildandroid\o4a\modules
\java\jni && C:\NDK\toolchains\llvm\prebuilt\windows-x86_64\bin\llvm-strip.exe --strip-unneeded C:/Users/felipe.chaves/Downloads/opencv4.5.2/buildandroid/o4a/jni/armeabi-v7a/libopencv
_java4.so""
C:/Users/felipe.chaves/Downloads/opencv4.5.2/opencv-4.52/3rdparty/libpng/pngrutil.c:429: error: undefined reference to 'inflateValidate'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
  File "C:\Users\felipe.chaves\Downloads\opencv4.5.2\opencv-4.52\platforms\android\build_sdk.py", line 439, in <module>
    builder.build_library(abi, do_install)
  File "C:\Users\felipe.chaves\Downloads\opencv4.5.2\opencv-4.52\platforms\android\build_sdk.py", line 264, in build_library
    execute([self.ninja_path, "-j1" if (self.debug_info or self.debug) else "-j3", "install" if (self.debug_info or self.debug) else "install/strip"])
  File "C:\Users\felipe.chaves\Downloads\opencv4.5.2\opencv-4.52\platforms\android\build_sdk.py", line 30, in execute
    raise Fail("Child returned: %s" % retcode)
__main__.Fail: Child returned: 1

that’s in zlib (or is it libz on android ?) it somehow missed it
try to add -D BUILD_ZLIB to your cmake cmdline, this should build it from 3rdparty src, and link it properly
(core and imgcodecs modules depend on that)

(and sorry, no clue about android or unity here, cant really help)

(post deleted by author)

Hi @berak, yes I could compile for the android version 19.0.5232133 (the exact/same that comes with Unity)
I copied the folder to c:\ndk, setup environment variables.
Also copied the sdk that comes with unity and put to c:\sdk, just have to copy the cmake folder that is not present on the Unity version so I also had C:\SDK\cmake\3.6.4111459

but I had to put on the build-sdk.py internally:
cmd.append("-DBUILD_ZLIB=‘ON’")

The build command:
python “C:\Users\felipe.chaves\Downloads\opencv4.5.3\opencv-4.53\platforms\android\build_sdk.py” “C:\Users\felipe.chaves\Downloads\opencv4.5.3\buildandroid” “C:\Users\felipe.chaves\Downloads\opencv4.5.3\opencv-4.53” --ndk_path=%ANDROID_NDK_ROOT% --sdk_path=%ANDROID_SDK_ROOT% --extra_modules_path=“C:\Users\felipe.chaves\Downloads\opencv4.5.3\opencv_contrib-4.53\modules” --use_android_buildtools --config=ndk-19.config.py

It was possible to compile and get the libopencv_java4.so

BUT this doesn’t work with unity and android I still keep getting ‘DLLNotFoundException’ after importing it as a ARMv7 lib