Android Studio 4 + OpenCV 4.1
- Download the latest OpenCV zip file from here (current newest version is 4.1.0) and unzip it in your workspace or in another folder.
- Create new Android Studio project normally. Click
File->New->Import Module
, navigate to/path_to_unzipped_files/OpenCV-android-sdk/sdk/java
, set Module name asopencv
, clickNext
and uncheck all options in the screen. - Enable
Project
file view mode (default mode isAndroid
). In theopencv/build.gradle
file changeapply plugin: 'com.android.application'
toapply plugin: 'com.android.library'
and replaceapplication ID "org.opencv"
withminSdkVersion 21 targetSdkVersion 28
(according the values inapp/build.gradle
). Sync project with Gradle files. - Add this string to the dependencies block in the
app/build.gradle
filedependencies { ... implementation project(path: ':opencv') ... }
- Select again
Android
file view mode. Right click onapp
module and gotoNew->Folder->JNI Folder
. Select change folder location and setsrc/main/jniLibs/
. - Select again
Project
file view mode and copy all folders from/path_to_unzipped_files/OpenCV-android-sdk/sdk/native/libs
toapp/src/main/jniLibs
. - Again in
Android
file view mode right click onapp
module and chooseLink C++ Project with Gradle
. Select Build Systemndk-build
and path toOpenCV.mk
file/path_to_unzipped_files/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
.path_to_unzipped_files
must not contain any spaces, or you will get error!
To check OpenCV initialization add Toast message in MainActivity
onCreate()
method:Toast.makeText(MainActivity.this, String.valueOf(OpenCVLoader.initDebug()), Toast.LENGTH_LONG).show();
If initialization is successful you will see
true
in Toast message else you will see false