Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.