Search code examples
androidc++opencvvisual-studio-2019

OpenCV for Android via Visual Studio to Unity


I tried to compile a .so library using Visual Studio 2019 along with OpenCV Android in order to use this library in Unity. There are some answers on how to configure Visual Studio to use OpenCV Android (here or here) but none of these work for me. Below you can see my configurations.

Visual Studio 2019 (running on Windows 10)

android-ndk-r21e // also tried with android-ndk-r15c android-ndk-r16b and android-ndk-r17c

OpenCV Android 4.5.2 // also tried with OpenCV Android 4.0.0, 4.0.1 and 4.1.0

My settings in Visual Studio 2019 look as follows:

Configuration Properties

- General

  • Platform Toolset Clang 5.0 (also tried Clang 3.8 or GCC 4.9)

  • Configuration Type Dynamic Library (.so)

  • Target API Level Nougat 7.0 (android-24) (also tried different versions)

  • Use STL LLVM libc++ static library (c++_static) (also tried "GNU STL static library (gnustl_static)")

C/C++

- General

  • Additional Include Directories "Path to OpenCV_4_5_2_Android\sdk\native\jni\include"

  • Code Generation Enable C++ Exceptions "Yes(-fexceptions)"

  • Language C++17(-std=c++1z)

  • Precompiled Headers Not using Precompiled Headers

Linker

- General

  • Additional Library Directories Path to OpenCV_4_5_2_Android\sdk\native\libs\armeabi-v7a

- Input

  • Additional Dependencies Path to OpenCV_4_5_2_Android\sdk\native\libs\armeabi-v7a\libopencv_java4.so

My Source.cpp I try to compile is just a single function for testing purposes

#include <opencv2/core.hpp>
extern "C" float test(float a, float b)
{float c = a * b;   return c;}

Which gives me the following errors:

E0035 #error directive: This constructor has not been ported to this platform
E0020 identifier "__fp16" is undefined
use of undeclared identifier 'ANDROID_LOG_INFO'

The ANDROID_LOG_INFO error can be fixed when I add #include "android/log.h" at the top of the file that throws this error. But the other two errors still remain.


Solution

  • I had the exact same issue as you (though I used c++ 11) with the exact same setup, and struggled for days. I believe the errors you're seeing (like me) are from arm_neon.h. Very oddly, I was able to just build (not run) the .so successfully, even with those errors (I say "errors" because if you look at arm_neon.h, others pop up), so try it. Maybe it's some kind of IntelliJ/Intellisense mistake more than anything else where it's catching false negatives from some other toolchain setup.

    At the same time, I'm not 100% sure I was always able to build with that issue, so try these steps as well if you can't:

    1. use OpenCV 4.0.1 with Android NDK 16rb. The NDK matters when it comes to OpenCV builds, and this is the only supposed match that I know of.
    2. follow this tutorial from scratch: https://amin-ahmadi.com/2019/06/03/how-to-use-opencv-in-unity-for-android/
    3. if the downloaded OpenCV android SDK is still giving trouble, build OpenCV from the source using his other tutorial here: https://amin-ahmadi.com/2019/02/03/how-to-build-opencv-4-x-for-native-android-development/ and then repeat step 2.

    MAJOR EDIT: OpenCV 4.5.2 needs to be treated differently because it no longer uses toolchains with gnu c++. -When you build OpenCV from CMake, build with Android NDK 21e, and do not use the toolchain in OpenCV 4.5.2. Use the one inside the Android NDK's build folder (android-ndk-r21e\build\cmake). -When you build your .so from Visual Studio 2019, do not use the GNU STL, use the LLVM. GNU c++ is no longer part of Android NDKs, and you need to cut it out of the process entirely. -In the Linker Input, put the names of your library files (or file, if it's just the world one) in the Library Dependencies field, not the Additional Dependencies field. -Everything else is the same as in those common tutorials.