How to Use OpenCV in Unity for Android

If you’ve read my previous articles (especially this one) about how to use OpenCV in Unity and how to pass images between them, this post will feel like a breeze and you can just quickly skim through it. However, if you haven’t, I recommend doing so because in this post I’ll describe how to create an Android library that uses the OpenCV library and then include it in your Unity project, or in other words describe what we did in Unity for Windows this time in Unity for Android.



We’ll be using Visual Studio 2019 for this purpose, however creating libraries for Android should be quite a similar process for Visual Studio 2017 as well.

Start by making sure your Visual Studio installation has “Mobile development with C++”. Just run Visual Studio Installer and check the checkbox next to it as seen below:

After that you can run Visual Studio and try creating a “Dynamic Shared Library (Android)” project. Here’s a screenshot that shows setting a C++ and Android filter and then selecting the project type:



After the project is created, choose Release and ARM Configuration for building the project:

Now you need to open up Project Properties (Property Pages) by right clicking on the project from Solution Explorer and choosing Properties. We’re going to configure our project so that it can be built for Android.

First, under “Configuration Properties” / “General”, you need to set the following parameters:

  • Set “Target API Level” to 21 (Lollipop 5.0 – 5.0.2, android-21) or even higher if you can avoid building your project for really low Android versions.
  • Set “Use of STL” to “GNU STL static library (gnustl_static)” (shared would also work but try to do the same to match my configurations)

After that, switch to “VC++ Directories” / “Include Directories” and then add the OpenCV for Android include path to the list of paths already present.

OpenCV include files are usually located in a path similar to the following:

<opencv_for_android_root_path>/sdk/native/jni/include

If you wish to learn how to build OpenCV from sources for Android, you can follow this guide.

Now, switch to “C/C++”, then “Code Generation” and set “Enable C++ Exceptions” to “Yes (-fexceptions)”.



Next parameter in Properties that we need to change is to enable run-time info. To do this, switch to “C/C++”, then “Language” and set “Enable Run-Time Information” to “Yes (-frtti)”

If you don’t have any Precompiled Headers, switch to “C/C++”, then “Precompiled Headers” and set “Precompiled Header” to “Not using Precompiled Headers”.

The next parameter that needs to be set is related to the OpenCV Android libraries. Switch to “Linker”, then “General” and add OpenCV Android libs folder to “Additional Library Directories”. This folder is usually located in a path similar to this:

<opencv_for_android_root_path>/sdk/native/libs/armeabi-v7a

After adding the path to the libraries, we need to add the libraries as well. Switch to “Linker” and then “Input” and add OpenCV Android libs to “Library Dependencies”. It’s the *.so files in libs folder. Omit the starting “lib” and ending “.so” from the file name. For instance if there’s a file called “libopencv_world.so” in there, you need to add “opencv_world” to the list.

That concludes all required configurations. You can now build the library from the previous tutorial or any other library that uses OpenCV for Android and then use it as a plugin in Unity to process images.

The only thing you need to pay attention to in your source code is that you don’t need the following declspec definition in Android to be able to expose your function to the outside world, so simply remove it or add it using Compiler directives only when you are building for Windows.

__declspec(dllexport)

After building your project, you need to make sure it’s copied into “Plugins/Android” folder in your Unity project. The rest is identical to what we did here.



27 Replies to “How to Use OpenCV in Unity for Android”

  1. Okay, I tried again after compiling OpenCV myself from the source, and I have similar issues. I followed every step here. But the moment I so much as include a single file like:
    #include “opencv2/objdetect.hpp”
    I get a slew of errors, from “identifier _CharT is undefined” to “expected a declaration” to 1700 others. Many of them come from “basic_ios.h”, which says it’s part of the gnu ISO c++ library. Many others come from the openCV files themselves.

    Something’s missing here, at least for some users. I would be interested to see what toolchain settings are supposed to be in “VC++ directories” — my Dynamic Library solution automatically put LLVM toolchain settings in there.

  2. After completing the steps in your tutorial, and after adding the include line

    #include

    to the .cpp file I get the error “identifier “__fp16″ is undefined”. The error comes from the arm_neon.h file (that is an External Dependency). When I open arm_neon.h the number of errors increases to 728. Is there a conflict between arm_neon.h and the OpenCV code when it comes to __fp16? And what must be done to correct this issue?

    I used both pre-built OpenCV 4.5.2 and OpenCV 3.4.14.

    Thanks much. There is so little information on using Visual Studios for making an OpenCV library for Android so I appreciate that you wrote this tutorial.

    1. Late to the game, but I have the Same problem (with the arm neon errors). Did anyone ever resolve this? Building from the source does not fix the problem.

    2. Hey Eliyhuw, I have the exact same problem, and switching C++ versions makes it worse and gives me more severe errors. Were you able to resolve this yet? I suspect if you fixed that one error, hundreds of others would pop up (as they did for me when I force-fixed it).

  3. Amin,
    just a fyi, when you say “Switch to “Linker” and then “Input” and add OpenCV Android libs to “Library Dependencies”. It’s the *.so files in libs folder.”

    You are aware there is only 1 file in the armeabi-v7a directory called
    libopencv_java4.so
    There is no plural ‘files’, and that one file is only 10kilobytes big

  4. Thank you for this tutorial, but I am having some issues regarding the .so file generated. I followed the above steps and after I build the .so file it doesn’t respond to C# script, to be specific DllNotFoundException in C# code on Android Device(I checked it through Android Studio Logcat).
    I found where exactly the problem gets triggered, I started generating .so file and individually testing it on my device after following each step of this tutorial, the .so file returns the response and gets detected by C# code until I add the opencv_java4.so file to Linker>Library Dependencies in my project and generate .so file.
    The .so filer generated after that doesn’t get detected by C# code in Android.

    Thank you in advance for your response.

    1. This tutorial is all about Unity, C# and Native C++ Android development. No sure why you’re even trying to use OpenCV for Java?!

      1. because it is the name of the library file that goes into Linker>Input>Library Dependencies and present in this directory /sdk/native/libs/armeabi-v7a>

  5. Hi! Thanks for the tutorial!

    I followed your instructions to build a library for Android.
    I moved the .so files into my Plugins/Android folder.
    When I try to run it on my android phone I get the error “DllNotFoundException”.

    Can you please tell me what your player settings are in Unity?
    Do you use the IL2CPP backend or not?

    Thanks in Advanced.

    Kobe

  6. Hi Amin,

    thanks for your Tutorial.

    While I got OpenCV working in Unity on Windows, I have trouble getting it to work on Android.

    Everything is fine if I build a .so file like you described and I can call code from it within Unity. But as soon as i try to link the OpenCV Library (openCV 4.2, libopencv_java4.so) I can’t use it in Unity anymore. The linking seems fine since there are no errors in the c++ Code but Unity always throws a DllNotFoundException.

    Do you have an idea whats the problem?

    1. That looks like a deployment issue, definitely not linking issue.
      Have you copied all library files to where they should go?

      1. I did, the libopencv_java4.so was in the same folder as my generated .so file but it didnt connect. I got it working however.

        I build the same in AndroidStudio instead of VisualStudio (but not sure if that was any difference). And I also exchanged my android opencv library file. Previously I used the official prebuild libraries. Then I took a libary build by an similar project.

        Now everything is running fine. I still wonder why the prebuild android library wouldnt work tho.

          1. Dear Amin, I was also wondering if a prebuild opencv library will not work.
            I have tried using the prebuild library, but end up with the “DllNotFoundException” error. so, is “building the opencv from source” mandatory in this case?
            Thank you very much!

          2. Dear Amin, I would also like to know if “building opencv from source” is mandatory to implement your demo code.
            I have tried using the prebuild version, but that ended up with the DllNotFoundException error.
            Thank you very much!

          3. It’s not mandatory if you have an OpenCV build but you can give it a try.
            However, DllNotFoundException usually only means that you are not putting it at the right place or there’s a naming or path issue.

  7. Hello! Thank you so much for this tutorial!

    I followed your instructions so I built a library for Android for the Canny effect of your “How to Pass Images Between OpenCV and Unity” tutorial .
    I moved the .so file in my Plugins/Android folder.
    When I try to run it I get the error “DllNotFoundException: … “.

    Can you help me with that? Thanks in advance!

    1. I know you may have already got the answer but if you haven’t then try this.
      If you are using Visual Studio then you may have noticed there are many unnecessary code written. Remove all the code except include and extern “c”.
      Your *.cpp file should look like this ~>
      —————-
      #include
      extern “c”{
      void yourFunction()
      {
      //do something here
      }
      }
      ——————
      Then in unity place the libYourFileName .so file in Assets/Plugin/Android folder.
      And while importing the Dll import it as ~>
      [DllImport(“YpurFileName,”)]
      without “lib” prefix or “.so” suffix.

      One more thing, your output will not appear on Unity Editor. You have to test it on physical or emulator device.

      I hope it has helped you…

  8. Hi

    Thnaks for yout tutorial.
    I have tried your metod but im not able to build your example. I´m always having the same error: undefined reference to ‘cv::imread(std::string const&, int)’
    I have look several times in mi Project properties for any errors but I dont find anything wrong.

    Do you know wat can be wrong?

    Thank you very much

    Joel

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.