Linking C++ OpenCV to an app in Android Studio [migrated]

Solution 1:

I finally managed. Here comes the recipe (validated under Windows).

Make sure to download the OpenCV Android SDK and copy it somewhere.

The project must be created as a Native C++ app (Phone and tablet). A cpp folder is automatically created with a native-lib.cpp source file and a CMakeLists.txt.

Add the following lines to the CMakeLists file, after the project line:

set(OpenCV_DIR $ENV{OPENCV_ANDROID}/sdk/native/jni)
find_package(OpenCV REQUIRED)

You must define the environment variable OPENCV_ANDROID and make it point to the folder .../OpenCV-android-sdk that you copied. (Alternatively, you can hard-code the path in the set command.)

Then insert at the bottom

target_link_libraries(app_name ${OpenCV_LIBS})

where OpenCV_LIBS points to the folder .../OpenCV-android-sdk/sdk/native/libs, and app_name is the name you gave when creating your app.

Finally, cross fingers and build.