1. Make sure you have Android SDK up to date, with NDK installed
2. Download latest OpenCV SDK for Android from OpenCV.org and decompress the zip file.
3. Create a new Android Studio project
- Check Include C++ Support
![image 1]()
- Choose empty Activity
- In C++ Support, you can check -fexceptions and -frtti
![image 2]()
4. Import OpenCV library module
- New -> Import Module
- Choose the YOUR_OPENCV_SDK/sdk/java folder
![image 3]()
- Unckeck replace jar, unckeck replace lib, unckeck create gradle-style
![image 4]()
5. Set the OpenCV library module up to fit your SDK
app/build.gradle
![image 5]()
opencv/build.gradle
![image 6]()
Edit openCVLibrary/build.gradle to match your app/build.gradle e.g:
compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
}
6. Add OpenCV module dependency in your app module
File -> Project structure -> Module app -> Dependencies tab -> New module dependency -> choose OpenCV library module
![image 7]()
![image 16]()
7. Make a jni folder by right clicking on app/src/main and click Change Folder Location box after that rename folder fron jni to jniLibs
1st step
![image 8]()
2nd step
![image 9]()
3rd step
![image 10]()
8. Copy all files from your opencv directory YOUR_OPENCV_SDK/sdk/native/libs that you have downloaded and paste them in jniLibs folder
1st step
![image 11]()
2nd step
![image 12]()
9. Set the app build.gradle
Add abiFilters
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
![image 13]()
10. Configure the CMakeLists.txt file
Copy these three lines and paste after the cmake_minimum_required
include_directories(YOUR_OPENCV_SDK/sdk/native/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
![image 14]()
Go to the end of CMakeLists.txt and write lib_opencv to the target_link_libraries list
![image 15]()
All is done now enjoy coding with opencv...
![Final Image]()