10

I'm following this tutorial but with Android Studio. I have done the following steps:

Creating a new project in Android Studio

Adding the OpenCV-2.4.5-sdk/sdk/java as a module

Right clicked on my main module->Change Module settings-> added the above opencv module as a dependency

For my MainActivity I used the following code (stripped down from one of the samples):

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

public class MainActivity extends Activity implements CvCameraViewListener {
    private CameraBridgeViewBase mOpenCvCameraView;

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i("Yay", "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
    }

    @Override
    public void onCameraViewStarted(int width, int height) {

    }

    @Override
    public void onCameraViewStopped() {

    }

    @Override
    public Mat onCameraFrame(Mat inputFrame) {
        return null;
    }
}

Android Studio's text editor could identify the packages I have imported (like org.opencv.android) but when I compiled I got these errors:

Gradle: error: package org.opencv.android does not exist
Gradle: error: package org.opencv.android does not exist
Gradle: error: package org.opencv.android.CameraBridgeViewBase does not exist

Does anyone know why this happens?

Shima Erfan
  • 335
  • 1
  • 5
  • 19
Rong den Lang beng
  • 101
  • 1
  • 1
  • 4

7 Answers7

12

I was having a similar issue with my Android Studio on OS X. I write the code, and Android Studio would see the .jar's I was referencing, and had declared dependencies on, but when it came time for Gradle to build the project, no go.

I opened up the 'build.gradle' file, and had to manually add dependencies. For example, here is my complete file:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/gson-2.2.4.jar')
    compile files('libs/jsoup-1.7.2.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 16
    }
}

As you can guess, the lines I added are the ones referencing Gson and Jsoup.

rynojvr
  • 567
  • 2
  • 5
  • 17
  • I'm currently looking into how to do multi-project configuration. – rynojvr May 19 '13 at 18:43
  • 1.) Make sure the added module is at the same directory level as the original module. – rynojvr May 19 '13 at 23:33
  • 2.) Verify that the 'build.gradle' file in your project, the one I talked about above, contains the line "compile project(':')" Be sure to include the preceding colon. – rynojvr May 19 '13 at 23:35
  • In the base directory that contains your modules, be sure the 'settings.gradle' file states "include '', ''" – rynojvr May 19 '13 at 23:37
  • Hi, thanks for your help. I still do not quite understand the steps though. Currently whenever I create a new project in Android Studio (say, OpenCVTest) it create a root project named OpenCVTest, then the java project inside with the same name (OpenCVTest). The root project also contains the settings.gradle file, although the java project is the one that contains the build.gradle. Should I add my dependency module inside the root project, or outside (but in the same directory)? If I add it inside, I can't add the dependency (via Change Module settings). Sorry since I have never used Gradle. – Rong den Lang beng May 20 '13 at 04:34
  • Oh and for the 2) step you described above, where should the "compile project" be added? I put it in the "dependencies" block but it could not compile. – Rong den Lang beng May 20 '13 at 04:35
  • @rynojvr I am stuck with the same issue for an hours. Would you please update the code with steps you mentioned in comment. I would really appreciate it. – Varundroid May 20 '13 at 04:37
  • It took me three hours to find this answer. **Thank you!** – Matt May 21 '13 at 14:14
  • @Rong did you have any luck with this? Im having the exact same issue, the code does recognize the imported module but gradle just wont find the org.opencv package and wont let me build the project. – Gustavo May 21 '13 at 23:17
  • @Gustavo sorry no, currently I import an existing Eclipse project to skip out Gradle. Then it works just like in Eclipse (just add opencv as a module dependency). I have never used Gradle so I don't know how it works either. – Rong den Lang beng May 22 '13 at 11:56
  • Well, then I'll keep trying, if I get to any solution. I'll get in touch with you. – Gustavo May 22 '13 at 16:03
  • I get `A problem occurred configuring the project ':OpenCV' - Main manifest missing from /.../OpenCV/src/main/AndroidManifest.xml` Any ideas? – fredley Jan 13 '14 at 12:18
4

Inspired by this answer, these are the steps I did:

  • Put the OpenCV 2.4.5 jar file (in my case, 'opencv-library-2.4.5.jar') into the libs folder of your project
  • Right click it and select Add as library
  • Type this in the dependencies part of build.gradle file: compile files('libs/opencv-library-2.4.5.jar')
  • Do a clean build. It can be done inside the android studio, but I also ran the gradlew.bat included inside the project folder

Now, the project should be built and run just fine.

Note: In later versions of OpenCV (e.g 2.4.7), the library can be found in ...\build\java.

Community
  • 1
  • 1
Mahm00d
  • 3,881
  • 8
  • 44
  • 83
1

I ran into a similar problem.

In my case, it was Android Studio failed to add the Gradle dependency for me. I had to add it myself in my project's gradle file:

compile project(":openCVLibrary2410")

It solved the problem. I hope this helps.

Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
1

More recent I solved the above described problem by:

  1. right clicking on the project,
  2. open module settings (F4)
  3. open the dependencies tab pane
  4. click +, add module dependency (with compile option)

then everything worked fine. The opencv library is an other module in my project

Michiel
  • 11
  • 1
0

I've found a workaround and now everything is working.

I had to build a Jar file (openCV.jar) and then I added it as a library instead of a module, finally in the gradle.build file I added this line

compile files('libs/openCV.jar').

So finally I get to use the library code and my build is Ok.

I don't know if this good. But that's how I did it. Hope it helps.

Gustavo
  • 301
  • 2
  • 5
  • 21
0

Trying to Add to the Dependency in Android Studio is fruitless there is a bug.

Try this instead:

  • Download OpenCV-2.4.5-android-sdk.zip from OpenCV site

    • Extract to where ever as OpenCV-2.4.5-android-sdk, mine happened to be

      /home/anthony/Documents/OpenCV-2.4.5-android-sdk/

    • Open IntelliJ and choose Import

    • Select the folder to import

      /home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/

      yours will be a little different, don't worry, just chose where you extracted OpenCV-2.4.5-android-sdk

    • Once the Import wizard finishes, build the app with menu

Build -> Rebuild Project

  • Close Project

  • Create New or Open existing project
  • Then

File->Import Module

  • This time select

/home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/XXX.iml

mine was sdk.iml, but yours could be anything but there will be only one

iml file Select iml file to import module

You can now start using OpenCV functions, start by typing

import org.

once you type the period IntelliJ should drop a list of options one of which is

opencv

Now OpenCV is correctly integrated in your IDE

The rest is up to you.

Anthony Keane
  • 519
  • 7
  • 14
  • I just tried following these steps, and I'm not able to have Android Studio auto-complete. Still trying to figure out why. – Israel Lopez Jul 20 '13 at 05:30
0

in my intellij IDE, i fixed as follows:
- point to your app (inside your project) and click F4 (Modules setting is focused by default)
- select Android-Gradle
- select tab Dependencies
- add (select + button in the right top) dependency : you can select add by maven, file or ... depend on you. In my case, I add new opencsv from maven.
- click OK to finish, your project rebuild automatically.

DONE.

trungtn
  • 1
  • 3