Creating a new OpenCV project in Android Studio

Creating a new OpenCV project in Android Studio

Return to OpenCV for Android Tutorials List

In this tutorial, we will create a new OpenCV project in Android Studio. Since you have everything downloaded already, we are ready to move on. By the end of this tutorial, you will have a new project set up, which you can use to build your projects upon. So lets do some CV!

Note: This tutorial has been tested on Android Studio 1.5 and 2.0

In short, the steps are, (2) Create simple project. (3) Import OpenCV SDK as a Module in Android Studio. (4) Set OpenCV Version. in Project (5) Fix Library association in project. (6) Done! Now actual OpenCV code can be written in future. The longer and detailed guided tutorial is given below.

  1. I beg you to open Android studio.
  2. Create a new project
    • I named it “OpenCV_Test”. (It could be different in the following GIF)
    • Minimum SDK is “API 19: Android 4.4 (KitKat).
    • Default Activity is “Basic Activity” (easily changeable later on)
    • Activity name is “MainActivity” (i.e. default settings)
    • I have shown the entire process in this GIF.
    • After pressing Finish, Gradle will perform some processes and eventually, you will be presented with your Android project.
    • Now, we can start importing OpenCV into the project.
  3. Import the OpenCV for Android SDK module in Android Studio.
    • To import the OpenCV SDK as a module in your project, go to File>New>Import Module.
    • Then Give path to your OpenCV as ..\OpenCV-android-sdk\sdk\java
    • You can chose to modify your module’s name.
    • Press Next and Finish the Dialogue.
    • The entire process is given here:
    • Note that in the end, there are some errors in the code as well as the console. This is because you need to fix the Version number and link the libraries with your own project. This is shown in the next two steps.
  4. In this step, we will fix the Android Project SDK information in the OpenCV build.gradle file.
    • In project explorer, switch to Project view.
    • Go to OpenCVLibrary300 > build.gradle file and open it.
      • I have updated the contents of the file with the following code
        apply plugin: ''
        android {
            compileSdkVersion 23
            buildToolsVersion "23.0.2"
            defaultConfig {
                minSdkVersion 19
                targetSdkVersion 23
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
      • Note that the android project’s SDK information can be obtained from the project’s own build.gradle file.
    • Once the Android SDK settings are updated, press “Try Again” button.
    • This entire process is given below:
    • Now. we need to fix library association.
  5. Looking good so far! We now need to make sure that the OpenCV for Android libraries are associated with our Android Studio project. Currently, Android Studio will not recognize the OpenCV code that we type in our files. We fix this as follows.
    • Go to File>Project Structure.
    • When window opens, under Modules, select app.
    • Click Dependencies.
    • Click +.
    • Select Module Dependencies.
    • (In my case) Select :openCVLibrary300.
    • Press Ok and Ok.
    • After a few seconds, the libraries will be associated with your Android project.
    • This entire process is shown here:
    • You can now write more OpenCV code in future in the same project.
  6. We did it! Project is ready for future OpenCV development.

This concludes this tutorial. You are now ready to start making your own Android Studio Project that consists of OpenCV SDK features limited only by your imagination. In the next tutorial, I will SHOW CAMERA on android app screen using OpenCV. Make sure that you read it. Do not forget to SHARE this post and write your opinions, ideas and views  in the  COMMENT  section below.

Thanks for reading :). Please like my Facebook page and follow my Twitter for more tutorials.

Return to OpenCV for Android Tutorials List

53 responses to “Creating a new OpenCV project in Android Studio”

  1. Hi, despite following your instructions to the T, I am unable to add the OpenCV library as a module dependency in Android Studio 3.4.1. Reply to me ASAP so I can get this issue fixed

    • you can set it manually by going to your gradle.built file where all your dependencies are.. use ” implementation project (‘your openCV Module name that you set while importing it’) and you are done…

  2. Hi, I followed the tutorial exactly as it says, and i’m using android studio 2.3. When I try to run the project it gives a “Cannot resolve symbol R” error and i’m unable to fix it.
    Please let me know of a solution as soon as possible.
    Thank you

  3. Hi, first of all, great tutorial, but I got a few questions…
    1) Is the OpenCV library a “native” library?
    2) Does it already have a CMakeLists.txt build script? or does it use ndk-build and includes an build script?

    Please respond ASAP, since I’m building an android app that can measure objects automatically, and I know OpenCV is one of things I need to do that

  4. Thanks a lot,You saved my day.Please post more.Please i am not able to run in my emulator.
    God bless you.

  5. Thanks a lot,you rock.Please continue sharing your knowledge,.God bless you.Please clear my doubts.Will this run in emulator or directly i have to run in my device.Thanks in advance

    • Thanks for the comment.

      Try to write the path in the field manually like ..


      I hope it solves the issue and lets you proceed. If not, then let me know.

  6. i followed your tutorial but when i perform the last step that is project structure and add module. no errors are removed. please tell me what to do

    • Thanks for the comment!

      I know when that happens. I myself was making a new project and it was missing something.

      See, it can be missing libraries or gradle or something I can’t know.

      Try to redo the entire process. It will work fine 🙂

      If there was any unclear point in the tutorial, then let me know. I will update and make it more clear to understand 🙂

  7. Hello!
    Followed your tutorial, but when i got to ‘run’ my program i got an exception once again which says:
    java.lang.UnsatisfiedLinkError: Couldn’t load opencv_java310 from loader dalvik.system.PathClassLoader[…]
    This is the exception i always seem to run into, i’ve tried to install opencv with all methods i could find on the net, but this exception keeps popping up, and i don’t know why, since opencv is where it is supposed to be and arrrghh

    Any help would be appriciated!

      • I wasn’t, now I installed it through the android studio, but it didn’t seem to fix the problem, maybe this is part of the reason why my app doesn’t seem to work, i wonder how many of these obvious, and stupid mistakes i made.

        Thanks for the answer though!

      • Welll…. Guess what, I’ve had these lines in my code: “static{
        since i had these in my original file, but it seems , that they caused these issues, without them my app runs just fine…

        Thanks once again!
        Btw awesome tutorial, really like how you made gifs for well… not-so-clever-people… like me 🙂

  8. Thank you for posting this. I have a lot experience with OpenCv with python wrappings but trying to move over to making a simple android app was driving me insane. All the tutorials, even on the OpenCV site still show how to do start working with Eclipse IDE. The even without the animated gifs this tutorial was a god send. The animated gifs was just added butter.

  9. Hello,
    I need to put the minSdkVersion 9 is this a problem does it affect the openCV functionalities??

    • Thanks for the comment Labib 🙂

      It will prevent you from using latest android functions. OpenCV will still be providing you the latest functionalities. You can go ahead and try it.

      Just make sure that version numbers match in both gradle and downloaded version.

      Be sure to check out the next tutorial to show camera on your phone.

      Thanks for the comment. 🙂

  10. I followed these steps in Android Studio 2 and OpenCV 3, it worked. The only thing that I have done differently is the buildToolsVersion in “build.gradle(Module: app). I downgraded to:
    buildToolsVersion “23.0.2”
    This is so that build errors are fixed. Thanks for this good posting.

  11. Thank you! I finally after two days of struggling managed to install it. When will you publish new articles? 🙂

    • Teresa, I am glad that it worked. I will be writing more on OpenCV for Android tutorials very soon. Let me know what you wish to see and I will see what I can write about it 🙂

      • Well, I have a question- after following your tutorial I tried to import some samples to a project and encountered some difficulties… Did you try out samples from openCV or you started with your own programs? 🙂

        • I just wrote the next tutorial in the series, it is about implementing the sample into the same project.

          You can find it HERE

          Now that you have mentioned about the difficulties, I will now implement every sample one by one.

          Thanks for reading

  12. good evening,

    I’ve a problem at the point 5, after I click ok.
    The grandle not sink because I’ve the error

    Error:Configuration with name ‘default’ not found.

    How do I resolve it?



Leave a Reply to codeonion Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: