Ask Your Question
3

How to build OpenCV for Android using Visual Studio?

asked 2017-11-28 07:43:15 -0600

FERNman gravatar image

updated 2020-11-17 15:54:11 -0600

Hello,

Short story:

Can anyone describe the process of how to use OpenCV in a C++ Project for Android in Visual Studio?


Long story:

I'm currently trying to build an OpenCV native plugin for Unity using Visual Studio. As it should be as platform independent as possible (except Linux), it's necessary to build the DLL for Windows, Android etc. That's also the reason why I'd prefer to stick to Visual Studio instead of Android Studio as it's (theoretically) possible to develop for all of those platforms there.

I already managed to setup OpenCV for UWP and Windows, however, I don't really know how to do this for Android. I already created a Visual Studio Cross-Platform C++ Project that Outputs a dynamic library (.so). It's compiling as long as I don't do anything OpenCV related in there.

My first try was to just use the normal OpenCV library for C++, but it gives me errors like "cannot use throw with exceptions disabled" and a lot more.

As a next step, I downloaded the OpenCV4Android .zip from the releases page and unzipped it. I added "OpenCV-android-sdk\sdk\native\jni\include" as additional include directory and "\OpenCV-android-sdk\sdk\native\libs\PLATFORM" as additional library directory, where "PLATFORM" is the build configuration (x64, x86, amd, etc.).

I then added "libopencv_core.a" to additional dependencies and tried to compile the whole thing, however, it says "There is no such file or directory: libopencv_core.a". If I remove it from the additional dependencies, it says "Linker command failed with exit code -1".

Also, I get a lot of errors saying "builtin function is not available because vector types are not supported".

edit retag flag offensive close merge delete

Comments

https://github.com/opencv/opencv/tree...

(i havent tried, but i guess, you won't stand a chance with vs here, and that you need a recent andoid ndk)

berak gravatar imageberak ( 2017-11-28 08:03:55 -0600 )edit

Thanks. Still, this is using CMake if I'm right. CMake would be my next try, but don't you think it's possible to compile it using Visual Studio alone?

FERNman gravatar imageFERNman ( 2017-11-28 08:08:03 -0600 )edit

see https://github.com/opencv/opencv/blob...

it is using Cmake to generate the makefiles, and ninja as a make tool (if i read that correctly)

berak gravatar imageberak ( 2017-11-28 08:10:33 -0600 )edit

3 answers

Sort by » oldest newest most voted
6

answered 2017-12-12 02:22:59 -0600

FERNman gravatar image

Well, as I didn't get an answer I managed to figure out the solution myself (thanks to berak for the two links). If anyone experiences issues with this answer, please feel free to write a comment. If you figure out the solution yourself, even better, write a comment with the problem and the solution!

Prerequisites

  1. CMake
  2. MinGW
  3. Android NDK
  4. Visual Studio

Building OpenCV

  1. Download / clone OpenCV as usual
  2. Start CMake configuration
  3. Select "MinGW Makefiles" as generator and choose "Specify toolchain file for cross-compiling"
  4. Choose "Path_To_OpenCV/platforms/android/android.toolchain.cmake" as Toolchain file
  5. In the Build group of CMake, choose the modules you need from OpenCV.
  6. In the With group of CMake, make sure to include Carotene in your build. At least it didn't work for me when I didn't include it.
  7. Generate MinGW Makefiles
  8. Navigate to your build folder and execute "mingw32-make", then "mingw32-make install". If any of those two fail, try generating OpenCV once more, but this time without any tests selected. This fixed it for me (search for "Test" in CMake and disable anything that comes up)

Setting up Visual Studio

  1. Create a new Visual Studio Project from "Visual C++/Cross Platform/Android" and choose the type of Project you want.
  2. Add the path "Path_to_build_folder/install/sdk/native/jni/include" to "Additional Include directories" and both the "native/3rdparty/libs/armeabi-v7a/" and "native/libs/armeabi-v7a/" to "Additional Library Directories".
  3. In your projects property dialog change "Enable C++ Exceptions" to "Yes (-fexceptions)" under "C/C++ - Code Generation" and "Enable Run-Time Type Information" to "Yes (-frtti)" as well. You can find it under "C/C++ - Language". Also add "-std=c++11" as Additional Option in the Command Line Section of "C/C++".
  4. In the Linker section of your property page, add "-lm -lz" to the command line additional options. This ensures correct linking.
  5. Under "Linker/Input", add all your built modules and also the 3rdparty libs as "Library Dependencies". Important: Only their names, e.g. not "libopencv_core.a", but "opencv_core". The linker will figure out the rest by itself. Now comes my favourite part: As UNIX-style linkers always scan libraries from right to left, the order in which you add the libraries matters. Now you have two options: Either play around with the order of the libraries until you figure out the correct one, or (as found by me after searching for literally hours), just go to your OpenCV build directory and search for the file "OpenCV.mk". In there the line "OPENCV_MODULES" indicates the correct order of the modules. The 3rdparty libraries have to be added after the modules in the Library Dependencies field.
edit flag offensive delete link more

Comments

1

I created an account on purpose to thank you!

marcojoao gravatar imagemarcojoao ( 2018-10-02 06:11:01 -0600 )edit

Thanks a lot. Your solution works. As for me I had to do a few extra things

  1. add $(VS_NdkRoot)/sources/android/support/include to additional include directories
  2. Use complete library names (e.g. libopencv_core.a) instead of opencv_core
  3. copy all the *.a files to my project source folder as it was not finding them in native/libs/armeabi-v7a/"
muz gravatar imagemuz ( 2019-10-19 05:33:36 -0600 )edit
4

answered 2018-11-30 03:36:15 -0600

geon gravatar image

updated 2018-12-02 02:22:24 -0600

Good guide! I build ok and share my experience to fill some details here.

My Environment

  • CMake-3.13
  • MinGW:
    Basic Setup choose mingw32-base-bin and mingw32-gcc-g++-bin
  • Python-2.7.15:
    I got one warning when CMake started configuring because of my 3.x version python
  • opencv-4.0.0
  • opencv_contrib-4.0.0
  • Visual Studio 2017
  • android-ndk-r16b
  • android-sdk:
    my sdk-tools is too new,but android.toolchain.cmake only need old sdk-tools which version is 25.2.5.
    so i download and replace my sdk-tools dir from here

Building OpenCV

  • 1.Open CMake-GUI
  • 2.Add Entry:
  • ANDROID_SDK=<path>android-sdk dir
  • ANDROID_NDK=<path>android-ndk dir
  • ANDROID_ABI=<string>armeabi-v7a
  • ANDROID_NATIVE_API_LEVEL=<string>android-19
  • BUILD_ANDROID_PROJECTS=<Bool>false //i got a error when it was true.
  • For more info about these Android Parameters,go here image description
  • 3.Click Configure,Select "MinGW Makefiles" as generator and choose "Specify toolchain file for cross-compiling"
  • 4.Choose "Path_To_OpenCV/platforms/android/android.toolchain.cmake" as Toolchain file and wait some minutes.
  • 5.Search below names and set their value
  • BUILD_TESTS=<Bool>false
  • BUILD_PERF_TESTS=<Bool>false
  • OPENCV_EXTRA_MODULES_PATH=<path>"opencv_contrib/modules" dir
  • BUILD_opencv_world=<Bool>true //options,it's ture will package all modules to "libopencv_world.a".
  • 6.Click Configure again and wait some mintues
  • 7.Check output information,if all goes well,you will configure successfully and then click Generate.
  • 8.Navigate to your build folder and execute "mingw32-make", then "mingw32-make install" in build folder.

Setting up Visual Studio

  • 1.Create new solution:File->New->Project->Visual C++/Cross Platform/Android->Dynamaic Shared Library(Android).
  • 2.In "Solution platform" :Change "Debug x86" to "Release ARM".
  • 3.In top menu:Click Project and choose properties.
  • 4.Follow FERNman's answer do step 2,3,4. By the way,i don't have "libs" dir,use "staticlibs" dir replace.
  • 5.Under "Linker/Input",Be careful of "Library Dependencies",the lib file is made up of three parts:"lib"+"name"+".a",there only need "name":e.g.Transform "libopencv_world.a" into "opencv_world" manually and type it on "Library Dependencies"
  • put my setting below:
    image description
edit flag offensive delete link more
0

answered 2019-06-21 07:17:10 -0600

updated 2019-06-21 07:27:47 -0600

See this post, it makes things much easier [the last comment there] :) "Android NDK provides own CMake toolchain file since r15." (I was trying to follow the steps here and got into trouble when building. Till I came across this linked post)

edit flag offensive delete link more

Comments

Hi @ShmuelFine Can you elaborate ?

LiorA gravatar imageLiorA ( 2020-04-24 15:23:41 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-11-28 07:43:15 -0600

Seen: 6,046 times

Last updated: Jun 21 '19