152

I want to use OpenCV library in my app with Android Studio. I followed instructions found here but I get error

Configuration with name 'default' not found

What can be wrong?

I use Android Studio 1.0 with gradle 2.2.1.

Hakan Fıstık
  • 11,376
  • 8
  • 74
  • 105
Bartosz Bialecki
  • 4,221
  • 10
  • 39
  • 63

10 Answers10

322

The below steps for using Android OpenCV sdk in Android Studio. This is a simplified version of this(1) SO answer.

  1. Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file.
  2. Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive.
  3. Update build.gradle under imported OpenCV module to update 4 fields to match your project build.gradle a) compileSdkVersion b) buildToolsVersion c) minSdkVersion and d) targetSdkVersion.
  4. Add module dependency by Application -> Module Settings, and select the Dependencies tab. Click + icon at bottom, choose Module Dependency and select the imported OpenCV module.
    • For Android Studio v1.2.2, to access to Module Settings : in the project view, right-click the dependent module -> Open Module Settings
  5. Copy libs folder under sdk/native to Android Studio under app/src/main.
  6. In Android Studio, rename the copied libs directory to jniLibs and we are done.

Step (6) is since Android studio expects native libs in app/src/main/jniLibs instead of older libs folder. For those new to Android OpenCV, don't miss below steps

  • include static{ System.loadLibrary("opencv_java"); } (Note: for OpenCV version 3 at this step you should instead load the library opencv_java3.)
  • For step(5), if you ignore any platform libs like x86, make sure your device/emulator is not on that platform.

OpenCV written is in C/C++. Java wrappers are

  1. Android OpenCV SDK - OpenCV.org maintained Android Java wrapper. I suggest this one.
  2. OpenCV Java - OpenCV.org maintained auto generated desktop Java wrapper.
  3. JavaCV - Popular Java wrapper maintained by independent developer(s). Not Android specific. This library might get out of sync with OpenCV newer versions.
larsaars
  • 837
  • 1
  • 12
  • 25
kiranpradeep
  • 9,842
  • 3
  • 42
  • 68
  • 1
    I am getting errors following the second step, Shall I delete the `javadocs` directory ?.It's also asking me for installing `android sdk 14` though I am targetting devices that support `android sdk 15` – Romantic Electron Jan 14 '15 at 05:34
  • 1
    @RomanticElectron Hope you have downloaded OpenCV4Android 2.4.10 from link https://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.4.10/OpenCV-2.4.10-android-sdk.zip/download and not that java desktop version ? – kiranpradeep Jan 21 '15 at 06:36
  • I am using `OpenCV4Android 2.4.9` , I have completed step 4 but I am unable to find the `sdk/native` directory in my `sdk` directory.Am I missing something? – Romantic Electron Jan 21 '15 at 08:54
  • @RomanticElectron I just re-downloaded that file and rechecked. `native` folder is present. Not sure why it is missing for you. – kiranpradeep Jan 21 '15 at 08:58
  • 2
    Ok I got it, I thought you were talking about the `sdk` installation directory on my system while you were talking about the directory which comes with `Android Open CV SDK`.Ok completed step 6. Where do I include `static{ System.loadLibrary("opencv_java"); }`? – Romantic Electron Jan 21 '15 at 09:09
  • @RomanticElectron where did you add include static{ System.loadLibrary("opencv_java"); } ??? In the activity ?? – Ashok Varma Mar 02 '15 at 06:30
  • @AshokVarma No I didn't , it's not required – Romantic Electron Mar 02 '15 at 06:47
  • @RomanticElectron i done the process and i was able to import OpenCV. but i tried opencv.android.utils.matToBitmap it failed. Should i need to setup NDK also ?? – Ashok Varma Mar 12 '15 at 17:46
  • 5
    @RomanticElectron where do you include static{ System.loadLibrary("opencv_java"); } ? You say "Thanks I've got it" but there seems to be no message in between. Then you say it's not needed. Could you explain a little more? Thanks. – puntofisso Aug 18 '15 at 16:45
  • @puntofisso It should be working fine without it ? Isn't it ? It was required in older versions perhaps , the newer work fine without it. – Romantic Electron Aug 19 '15 at 22:24
  • I've followed your instructions and when I've done third step I've tried to do fourth step, but I couldn't do anything on my module 'android' (It's sample ZXing app). So I've restarted Android Studio and instead of module 'android' there was openCV module, which is even more confusing. The old module is gone and I don't know what to do. – ZZ 5 Oct 15 '15 at 14:05
  • 9
    for anyone having the `android-14` error after importing the module in step 2, you need to manually edit the `build.gradle` in the openCV directory **in your project** as shown in step 3. – paradite Nov 21 '15 at 16:37
  • 1
    If you still have problem watch this video [link](https://www.youtube.com/watch?v=JIHfqzTdOcQ) – Somir Saikia Mar 25 '16 at 10:01
  • why do we write `System.loadLibrary("opencv_java3")` while the *.so* file is named *libopencv_java3*? – Muhammad Babar Sep 28 '16 at 13:39
  • 1
    where should the < static{ System.loadLibrary("opencv_java"); } > be included? – Libathos Dec 07 '16 at 13:40
  • This works great. Note that if you are only planning to support certain architectures, you can copy only the libraries for them. In case a user with a different architecture tries to run the app, I think (not yet tested) the app will ask to install the OpenCV manager. – Jose Gómez Feb 04 '17 at 22:34
  • The only problem, though, is that your repository will grow with more than 1000 OpenCV files. It'd be great if we could just add a jar or something, instead of all the separate files. – Jose Gómez Feb 04 '17 at 22:35
  • Some links are broken. Please fixed broken the links. (This may help other users) – Sudarshan Mar 24 '17 at 18:03
  • Although this method packages the jni libs into the APK, it does not help for native C/C++ coding as a ```externalNativeBuild``` or ```ndk``` block is not found in ```build.gradle``` file. Recent updates in Android Studio 2.3.0 has received that kind of integration. Please check my answer for more information: http://www.stackoverflow.com/a/43886764/2949966 – ahasbini May 11 '17 at 11:15
  • note at step 5: copy the `sdk/native/libs` folder, not the `sdk/native` folder (the phrasing in the answer makes this easy to overlook) – Ziarno Sep 23 '17 at 10:16
  • If I want to write C++ code for OpenCV on Android Studio, should I do the steps you wrote above? – user8663682 Jan 23 '18 at 10:58
  • 1
    I can't understand this step `include static{ System.loadLibrary("opencv_java"); } ` where should I add this? – user8663682 Jan 23 '18 at 12:28
  • Also in this step `Update build.gradle under imported OpenCV module to update 4 fields to match your project build.gradle a) compileSdkVersion b) buildToolsVersion c) minSdkVersion and d) targetSdkVersio ` there is no numbers to change. I use android studio v2.3 and only have this line with numbers ` classpath 'com.android.tools.build:gradle:2.3.0' ` – user8663682 Jan 23 '18 at 12:30
  • I think, Step#5 need be done under openCV module, not our "app" module. – Jose Jithin Stanly Feb 02 '18 at 07:30
  • great post.!Thank you. – VP4Android Jun 23 '18 at 19:18
  • @ahasbini i'm really in need of help with this question, can you help me, please? https://stackoverflow.com/questions/61216402/how-to-improve-image-segmentation-using-the-watershed – Carlos Diego Apr 28 '20 at 17:02
  • @Kiran or can someone help me in this matter, I really need to do this. https://stackoverflow.com/questions/61876457/image-segmentation-using-segment-seeds-watershed-in-android – Tecnologia da Net May 18 '20 at 19:47
  • while importing `/OpenCV-android-sdk/sdk/java` android studio not showing module name text box for edit, result module name import as `java`. After that in dependencies there no java module showing, even after successfully importing the module – Mayur Satav Jul 06 '20 at 05:54
147

Integrating OpenCV v3.1.0 into Android Studio v1.4.1, instructions with additional detail and this-is-what-you-should-get type screenshots.

Most of the credit goes to Kiran, Kool, 1", and SteveLiles over at opencv.org for their explanations. I'm adding this answer because I believe that Android Studio's interface is now stable enough to work with on this type of integration stuff. Also I have to write these instructions anyway for our project.

Experienced A.S. developers will find some of this pedantic. This answer is targeted at people with limited experience in Android Studio.

  1. Create a new Android Studio project using the project wizard (Menu:/File/New Project):

    • Call it "cvtest1"
    • Form factor: API 19, Android 4.4 (KitKat)
    • Blank Activity named MainActivity

      You should have a cvtest1 directory where this project is stored. (the title bar of Android studio shows you where cvtest1 is when you open the project)

  2. Verify that your app runs correctly. Try changing something like the "Hello World" text to confirm that the build/test cycle is OK for you. (I'm testing with an emulator of an API 19 device).

  3. Download the OpenCV package for Android v3.1.0 and unzip it in some temporary directory somewhere. (Make sure it is the package specifically for Android and not just the OpenCV for Java package.) I'll call this directory "unzip-dir" Below unzip-dir you should have a sdk/native/libs directory with subdirectories that start with things like arm..., mips... and x86... (one for each type of "architecture" Android runs on)

  4. From Android Studio import OpenCV into your project as a module: Menu:/File/New/Import_Module:

    • Source-directory: {unzip-dir}/sdk/java
    • Module name: Android studio automatically fills in this field with openCVLibrary310 (the exact name probably doesn't matter but we'll go with this).
    • Click on next. You get a screen with three checkboxes and questions about jars, libraries and import options. All three should be checked. Click on Finish.

      Android Studio starts to import the module and you are shown an import-summary.txt file that has a list of what was not imported (mostly javadoc files) and other pieces of information. enter image description here

      But you also get an error message saying failed to find target with hash string 'android-14'.... This happens because the build.gradle file in the OpenCV zip file you downloaded says to compile using android API version 14, which by default you don't have with Android Studio v1.4.1. enter image description here

  5. Open the project structure dialogue (Menu:/File/Project_Structure). Select the "app" module, click on the Dependencies tab and add :openCVLibrary310 as a Module Dependency. When you select Add/Module_Dependency it should appear in the list of modules you can add. It will now show up as a dependency but you will get a few more cannot-find-android-14 errors in the event log.

  6. Look in the build.gradle file for your app module. There are multiple build.gradle files in an Android project. The one you want is in the cvtest1/app directory and from the project view it looks like build.gradle (Module: app). Note the values of these four fields:

    • compileSDKVersion (mine says 23)
    • buildToolsVersion (mine says 23.0.2)
    • minSdkVersion (mine says 19)
    • targetSdkVersion (mine says 23)
  7. Your project now has a cvtest1/OpenCVLibrary310 directory but it is not visible from the project view:

enter image description here

Use some other tool, such as any file manager, and go to this directory. You can also switch the project view from Android to Project Files and you can find this directory as shown in this screenshot: enter image description here

Inside there is another build.gradle file (it's highlighted in the above screenshot). Update this file with the four values from step 6.

  1. Resynch your project and then clean/rebuild it. (Menu:/Build/Clean_Project) It should clean and build without errors and you should see many references to :openCVLibrary310 in the 0:Messages screen.

    enter image description here

    At this point the module should appear in the project hierarchy as openCVLibrary310, just like app. (Note that in that little drop-down menu I switched back from Project View to Android View ). You should also see an additional build.gradle file under "Gradle Scripts" but I find the Android Studio interface a little bit glitchy and sometimes it does not do this right away. So try resynching, cleaning, even restarting Android Studio.

    You should see the openCVLibrary310 module with all the OpenCV functions under java like in this screenshot:

    enter image description here

  2. Copy the {unzip-dir}/sdk/native/libs directory (and everything under it) to your Android project, to cvtest1/OpenCVLibrary310/src/main/, and then rename your copy from libs to jniLibs. You should now have a cvtest1/OpenCVLibrary310/src/main/jniLibs directory. Resynch your project and this directory should now appear in the project view under openCVLibrary310.

    enter image description here

  3. Go to the onCreate method of MainActivity.java and append this code:

    if (!OpenCVLoader.initDebug()) {
        Log.e(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), not working.");
    } else {
        Log.d(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), working.");
    }
    

    Then run your application. You should see lines like this in the Android Monitor: enter image description here (I don't know why that line with the error message is there)

  4. Now try to actually use some openCV code. In the example below I copied a .jpg file to the cache directory of the cvtest1 application on the android emulator. The code below loads this image, runs the canny edge detection algorithm and then writes the results back to a .png file in the same directory.

    Put this code just below the code from the previous step and alter it to match your own files/directories.

    String inputFileName="simm_01";
    String inputExtension = "jpg";
    String inputDir = getCacheDir().getAbsolutePath();  // use the cache directory for i/o
    String outputDir = getCacheDir().getAbsolutePath();
    String outputExtension = "png";
    String inputFilePath = inputDir + File.separator + inputFileName + "." + inputExtension;
    
    
    Log.d (this.getClass().getSimpleName(), "loading " + inputFilePath + "...");
    Mat image = Imgcodecs.imread(inputFilePath);  
    Log.d (this.getClass().getSimpleName(), "width of " + inputFileName + ": " + image.width());
    // if width is 0 then it did not read your image.
    
    
    // for the canny edge detection algorithm, play with these to see different results
    int threshold1 = 70;
    int threshold2 = 100;
    
    Mat im_canny = new Mat();  // you have to initialize output image before giving it to the Canny method
    Imgproc.Canny(image, im_canny, threshold1, threshold2);
    String cannyFilename = outputDir + File.separator + inputFileName + "_canny-" + threshold1 + "-" + threshold2 + "." + outputExtension;
    Log.d (this.getClass().getSimpleName(), "Writing " + cannyFilename);
    Imgcodecs.imwrite(cannyFilename, im_canny);
    
  5. Run your application. Your emulator should create a black and white "edge" image. You can use the Android Device Monitor to retrieve the output or write an activity to show it.

The Gotchas:

  • If you lower your target platform below KitKat some of the OpenCV libraries will no longer function, specifically the classes related to org.opencv.android.Camera2Renderer and other related classes. You can probably get around this by simply removing the apprpriate OpenCV .java files.
  • If you raise your target platform to Lollipop or above my example of loading a file might not work because use of absolute file paths is frowned upon. So you might have to change the example to load a file from the gallery or somewhere else. There are numerous examples floating around.
Wodin
  • 2,905
  • 1
  • 22
  • 47
ssimm
  • 1,830
  • 3
  • 14
  • 30
  • 2
    16-Feb edit: jniLibs now in the openCVLibrary310 directory, not the main app directory. Either works but it seems cleaner to me to keep them in the opencv part. Plus *more* screenshots and the "Gotchas" part. – ssimm Feb 16 '16 at 12:33
  • I think some import statements are missing, otherwise very good job! +1 – Allan Nørgaard Mar 09 '16 at 15:20
  • I don't mind revising it. Please tell me what to add. – ssimm Mar 09 '16 at 15:38
  • 2
    I followed your answer but failed. **1**i got the error `OpenCV error: Cannot load info library for OpenCV` **2** `java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/www.deven.com.opencv-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libopencv_java3.so` What these error about i done step by step as you written. – Devendra Singh Mar 16 '16 at 07:04
  • 2
    @DevendraSingh I got the same error as you got but I realised I had missed step 9. – aasu Apr 13 '16 at 14:05
  • This answer might be a good addition to the "Documentation Beta". I looked at the opencv topic, but I couldn't tell how or what to do there. Seems to me we need separate topics: opencv general, opencv android, etc. – Dale Aug 07 '16 at 19:45
  • Great answer.Specially by adding liberaries into app. Only missing thing is about adding android camera permission – Majid Hojati Oct 31 '16 at 18:06
  • Although this method packages the jni libs into the APK, it does not help for native C/C++ coding as a ```externalNativeBuild``` or ```ndk``` block is not found in ```build.gradle``` file. Recent updates in Android Studio 2.3.0 has received that kind of integration. Please check my answer for more information: http://stackoverflow.com/a/43886764/2949966 – ahasbini May 11 '17 at 11:09
  • Anyone able to fix the "cannot load info" error? I know the library loads anyway! – Pranav Nandan Nov 27 '17 at 16:07
  • 2
    what if we want to code in c++ for opencv on android studio? – user8663682 Jan 23 '18 at 12:51
  • @ssimm i'm really in need of help with this question, can you help me, please? https://stackoverflow.com/questions/61216402/how-to-improve-image-segmentation-using-the-watershed – Carlos Diego Apr 28 '20 at 17:03
40

For everyone who felt they want to run away with all the steps and screen shots on the (great!) above answers, this worked for me with android studio 2.2.1:

  1. Create a new project, name it as you want and take the default (minSdkVersion 15 is fine).

  2. Download the zip file from here: https://sourceforge.net/projects/opencvlibrary/files/opencv-android/ (I downloaded 3.2.0 version, but there may be a newer versions).

  3. Unzip the zip file, the best place is in your workspace folder, but it not really matter.

  4. Inside Android Studio, click File->New-> Import Module and navigate to \path_to_your_unzipped_file\OpenCV-android-sdk\sdk\java and hit Ok, then accept all default dialogs.

  5. In the gradle file of your app module, add this to the dependencies block:

     dependencies {
         compile project(':openCVLibraryXYZ')
         //rest of code
     }
    

Where XYZ is the exact version you downloaded, for example in my case:

    dependencies {
        compile project(':openCVLibrary320')
        //rest of code
    }
yshahak
  • 4,704
  • 1
  • 26
  • 36
  • 1
    Nice and simple, thnx ;-) Works also for the version 3.2.0. – blacharnia Mar 22 '17 at 18:24
  • 3
    Thank you, it's indeed the easier solution. One point from me. The new OpenCV module couldn't be compiled. The answer here helped me to fix this issue http://stackoverflow.com/a/40302788/408780 – Tima Mar 31 '17 at 14:58
  • just to add to what @blacharnia said, just make sure you use 'compile project(':openCVLibrary320')' in the cradle dependencies section instead of 310 – Abraham Philip May 19 '17 at 00:15
  • Maybe more generally, to add to the above comments, use "compile project(':openCVLibraryXYZ')" where XYZ is the version number of openCV Android SDK without the 'dots' - this also works now for version 3.4.0 -> i.e. use "compile project(':openCVLibrary340')" – Mick Jan 16 '18 at 19:42
  • @Mick thanks, I edited the answer according to your suggestion. – yshahak Jan 17 '18 at 13:59
  • @yshahak or can someone help me in this matter, I really need to do this. https://stackoverflow.com/questions/61876457/image-segmentation-using-segment-seeds-watershed-in-android – Tecnologia da Net May 18 '20 at 19:45
14

Android Studio 3.4 + OpenCV 4.1

  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.

  2. 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 as opencv, click Next and uncheck all options in the screen.

  3. Enable Project file view mode (default mode is Android). In the opencv/build.gradle file change apply plugin: 'com.android.application' to apply plugin: 'com.android.library' and replace application ID "org.opencv" with

    minSdkVersion 21
    targetSdkVersion 28
    

    (according the values in app/build.gradle). Sync project with Gradle files.

  4. Add this string to the dependencies block in the app/build.gradle file

    dependencies {
        ...
        implementation project(path: ':opencv')
        ...
    }
    
  5. Select again Android file view mode. Right click on app module and goto New->Folder->JNI Folder. Select change folder location and set src/main/jniLibs/.

  6. Select again Project file view mode and copy all folders from /path_to_unzipped_files/OpenCV-android-sdk/sdk/native/libs to app/src/main/jniLibs.

  7. Again in Android file view mode right click on app module and choose Link C++ Project with Gradle. Select Build System ndk-build and path to OpenCV.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.

Plo_Koon
  • 2,785
  • 3
  • 32
  • 39
  • Above you say " replace application ID "org.opencv" with ....." but there is nothing following the "with". Can you please clarify? Do you mean to say "remove the applicationId entry and add two entries as follows:" – Dale Sep 19 '19 at 18:51
  • @Dale Yes, you should replace `application ID "org.opencv"` string with two strings: `minSdkVersion 21` and `targetSdkVersion 28` (according the values in app/build.gradle file) – Plo_Koon Sep 20 '19 at 19:51
  • I followed all the steps as you described . But getting error E/art: dlopen("/data/app/com.example.objectsegmentation-1/lib/arm64/libopencv_java4.so", RTLD_LAZY) failed: dlopen failed: library "libc++_shared.so" not found – Masthan Jan 07 '20 at 06:40
2

This worked for me and was as easy as adding a gradle dependancy:

https://bintray.com/seesaa/maven/opencv#

https://github.com/seesaa/opencv-android

The one caveat being that I had to use a hardware debugging device as arm emulators were running too slow for me (as AVD Manager says they will), and, as described at the repo README, this version does not include x86 or x86_64 support.

It seems to build and the suggested test:

static {
    OpenCVLoader.initDebug();
}

spits out a bunch of output that looks about right to me.

Naumdev
  • 187
  • 1
  • 9
  • This was from a fresh project in android studio. I did not import anything, did not download anything, etc. Just add the gradle dependency and sync. – Naumdev Dec 27 '18 at 16:23
2
Download

Get the latest pre-built OpenCV for Android release from https://github.com/opencv/opencv/releases and unpack it (for example, opencv-4.4.0-android-sdk.zip).

Create an empty Android Studio project

Open Android Studio. Start a new project. 

enter image description here

Keep default target settings.

Use "Empty Activity" template. Name activity as MainActivity with a corresponding layout activity_main. Plug in your device and run the project. It should be installed and launched successfully before we'll go next.

 Add OpenCV dependency

Go to File->New->Import module

and provide a path to unpacked_OpenCV_package/sdk/java. The name of module detects automatically. Disable all features that Android Studio will suggest you on the next window.

enter image description here

enter image description here

Configure your library build.gradle (openCVLibrary build.gradle)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

Implement the library to the project (application build.gradle)

 implementation project(':openCVLibrary330')
shohruh Maxmudov
  • 363
  • 3
  • 13
1

In your build.gradle

repositories {
  jcenter()
}

implementation 'com.quickbirdstudios:opencv:4.1.0'

More information

Amir Hossein Ghasemi
  • 7,952
  • 4
  • 43
  • 42
Raymond Chenon
  • 8,990
  • 10
  • 64
  • 97
0

Anybody facing problemn while creating jniLibs cpp is shown ..just add ndk ..

Aklesh Singh
  • 553
  • 6
  • 9
0

It took me a long time to figure out how it works. The method I used does not require Gradle.

Disclaimer: I do not know how good or bad this method is. So use it on your own.

  1. Download Both "OpenCV4Android" and "AndroidNDK" (Download NDK to found libc++_shared.so as I say later)
  2. Extract "OpenCV4Android" somewhere and import one of the examples like this one "opencv-4.5.1-android-sdk\OpenCV-android-sdk\samples\tutorial-1-camerapreview" to android studio.
  3. After importing, go to "OpenCV4Android" sdk again "OpenCV-android-sdk\sdk\java\src\org\opencv" and copy this folder with all subfolders to your "tutorial-1-camerapreview" sample project like section (1) of this image:

enter image description here

  1. Now you need JNI Libraries and also libc++_shared.so. Make a folder in your sample project and name it "Libs"(this name is not important but you have to configure it later). Go to "OpenCV4Android" sdk again and copy all folders of "opencv-4.5.1-android-sdk\OpenCV-android-sdk\sdk\native\libs" to your new "libs" folder in your sample project as section (2) of above picture. Also extract ndk and go to this address "android-ndk\android-ndk-r20b\sources\cxx-stl\llvm-libc++\libs" and copy libc++_shared.so to your libs directory.

Note that for both libraries you must place at least four files with the same name but in folders for different CPU architectures.

  1. Important part. You must load these libs somewhere. I put them in Activity. Check section (3) of above image.
    static {
            System.loadLibrary("opencv_java4");
    }
  1. For last step open "Project Structure" and define your Libs folder like this image:

enter image description here

And for last TIP make an APK directly if you don't want to emulate your application.

enter image description here

Shamshirsaz.Navid
  • 798
  • 2
  • 10
  • 23
0

On the latest openCV version 4.5.2, you need to import the whole sdk folder, not sdk/java, other wise when you go to add the dependency, OpenCV doesn't show up. full description here:

Cant add openCV module as a dependency to my app

Siavash
  • 7,009
  • 10
  • 43
  • 60