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

Liked it? Take a second to support codeonion on Patreon!

48 Replies to “Creating a new OpenCV project in Android Studio”

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

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

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

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

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

Leave a Reply

Your email address will not be published.