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:


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:


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 “” 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.


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.

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

  1. 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
    There is no plural ‘files’, and that one file is only 10kilobytes big

  2. 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 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>

  3. 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.


  4. 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, 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 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.

  5. 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 ~>
      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 ~>
      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…

  6. 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


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.