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.
      Android-Studio-Create-New-Project
    • 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:
      OpenCV-Android-Import-Module-Android-Studio
    • 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
      • 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:
      OpenCV-Android-Update-DefaultProject-SDK-Information
    • 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:
      OpenCV-SDK-library-association
    • 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

Codeonion contributes to the honorable mission of “helping people learn and earn online”. Whether your day job is creative or not, life is better when you learn and apply creative skills regularly. To do this is satisfying in its own right and a practical investment in your future.

Comments (52)

  1. Yusaf Osei-Bonsu says:

    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

  2. Shelly says:

    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. Yusaf Osei-Bonsu says:

    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 Android.mk 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. Charles Kim says:

    When will be the next uploads? this thread is so helpful 🙂

  5. Anonymous says:

    Thanks . The way you guided us was very nice 🙂

  6. varun says:

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

  7. varun says:

    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

    1. codeonion says:

      I suggest you try on an actual Android Device. Because, then you will be doing everything for the right product.

  8. Anonymous says:

    it is really useful. Thnak for your share!

  9. Anonymous says:

    Thank You SO much. I literally searched the entire web for something like this>

    1. codeonion says:

      Thank you very much for your kind words. My happiness level for you infinite.

  10. this tutorial is one of the best and simplest explanation to run.. thanks a lot mate

    1. codeonion says:

      It was a pleasure for me. I am glad it helps people.

  11. Sumeet Batra says:

    When I try to import module I get “select modules to import” and it will not let me import the java folder in opencv sdk.

    1. codeonion says:

      Thanks for the comment.

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

      d:\some_folder\..\OpenCV-android-sdk\sdk\java

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

    2. Anonymous says:

      had the same error: check if your sdk folder is sane. Download again if it’s not

  12. Anonymous says:

    Thank you ! it was very helpfull

  13. karthi says:

    It as really use full post. Thank you.

  14. sundi says:

    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

    1. codeonion says:

      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 🙂

  15. Patrycja says:

    Hi! That’s a good article! I’ve been looking for something like this sometime ago. And I found this one: http://blog.zaven.co/opencv-in-android-studio-project/ It is really good and it was so helpful. Maybe it can be useful for you or other users as well.

  16. Coldus says:

    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!
    Thanks

    1. codeonion says:

      Are you using NDK?

      1. Coldus says:

        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!

      2. Coldus says:

        Welll…. Guess what, I’ve had these lines in my code: “static{
        OpenCVLoader.initDebug();
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        }”
        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 🙂

  17. Shawn says:

    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.

    1. codeonion says:

      Thank you very much. I am very happy to read this. So much positive energy that I can’t comprehend.

  18. Labib says:

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

    1. codeonion says:

      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. 🙂

  19. Chi says:

    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.

    1. codeonion says:

      Thanks for reading. I am glad you fixed it 🙂

  20. Teresa says:

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

    1. codeonion says:

      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 🙂

      1. Teresa says:

        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? 🙂

      2. codeonion says:

        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

  21. Alexander says:

    When will your camera app be ready?

    1. codeonion says:

      I have moved it up my list. You will find it soon. Subscribe, like and follow 🙂

  22. Anonymous says:

    Thanks for this tutorial, you helped a lot seriously speaking

    1. codeonion says:

      Thanks for your kind words, they mean a lot to me 🙂

  23. Piero says:

    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?

    thank,

    Piero

  24. Rashmi says:

    Amazing tutorial that worked!! Many thanks!!

    1. codeonion says:

      I am happy that it is useful to you :). May you be successful!

  25. Anonymous says:

    good tutorial, it saved my day

    1. codeonion says:

      I am so glad that it helped. Thanks!

  26. Anonymous says:

    right on the mark very fluid tutorial

    1. codeonion says:

      Thank you for the kind words, it means a lot to me 🙂

      The next tutorials will cover more concepts of OpenCV on Android 🙂

  27. Anonymous says:

    Gracias, Thanks for writing so clearly.

    1. codeonion says:

      Thank you! It meant a lot!

  28. Anonymous says:

    Thanks,

    i guess you saved me from a few painful hours!

    1. codeonion says:

      As somebody once saved mine 🙂

  29. Anonymous says:

    Best tutorial. Please keep making more

    1. codeonion says:

      Thanks for the kind words. I will be making more 🙂

Leave a Reply

Your email address will not be published.

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