You need to Build OpenCV for Android as Native Library.
So try these steps for example:
(little Suggestion, download the Master-Branch from opencv from Github)
Set environment variables for your OpenCV Directory and add the Android NDK to your Path Variable:
For Mac e.g.:
OPENCV_DIR=/home/declan/Documents/zone/mid/bin/android/opencv/opencv_github # where you want to put openCV on your system
export ANDROID_NDK=/home/declan/Documents/zone/mid/bin/android/google/sdk/ndk-bundle where is Google's NDK on your system
go to the Platforms directory cd ${OPENCV_DIR}/platforms
configure the build with following parameters:
scripts/cmake_android_arm.sh -DBUILD_SHARED_LIBS=ON -DANDROID_NATIVE_API_LEVEL=9 -DANDROID_STL=gnustl_shared
and start building:
cd build_android_arm
make -j8
When make finished continue with sudo make install
When everything worked fine, your files should be here:
${OPENCV_DIR}/platforms/build_android_arm/lib/armeabi-v7a
NOTE THATS HOW IT WORKS IN MAC! WHEN YOU NEED IT FOR VISUAL STUDIO BUILD IT WITH CMAKE (should be similar)
So after that you have your OpenCV Files built ready for Android.
Now you need to add the files to your Android Project. For that simply create a jni folder in <yourProject>/app/src/main/
in this folder you create 2 files: Android.mk and Application.mk
These are build files.
in them you can write the following:
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#OPENCV_CAMERA_MODULES:=off
#OPENCV_INSTALL_MODULES:=off
OPENCV_LIB_TYPE:=STATIC
ifdef OPENCV_ANDROID_SDK
ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)")
include ${OPENCV_ANDROID_SDK}/OpenCV.mk
else
include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk
endif
else
include /<hardcodedPathToYourOpenCV/opencv-master/platforms/build_android_arm/install/sdk/native/jni/OpenCV.mk
endif
LOCAL_SRC_FILES := <additional Files you created in c++>
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := <Library Name>
include $(BUILD_SHARED_LIBRARY)
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions -std=c++11
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
APP_MODULES := <LibraryName>
So far so good, but not finished yet! You have to add some parts into your build.gradle
file.
android{
[...]
ndk{
abiFilters 'armeabi-v7a'
}
}
externalNativeBuild{
ndkBuild{
path "src/main/jni/Android.mk/"
}
}
And this is it. After that, Android Studio should compile your OpenCV Library into your Android Studio Project with Native C++.