Android Studio and Gradle dependency integration

45,291

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Share:
45,291
Rong den Lang beng
Author by

Rong den Lang beng

Updated on February 27, 2020

Comments

  • Rong den Lang beng
    Rong den Lang beng over 4 years

    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?

  • rynojvr
    rynojvr about 11 years
    I'm currently looking into how to do multi-project configuration.
  • rynojvr
    rynojvr about 11 years
    1.) Make sure the added module is at the same directory level as the original module.
  • rynojvr
    rynojvr about 11 years
    2.) Verify that the 'build.gradle' file in your project, the one I talked about above, contains the line "compile project(':<PROJECT_NAME>')" Be sure to include the preceding colon.
  • rynojvr
    rynojvr about 11 years
    In the base directory that contains your modules, be sure the 'settings.gradle' file states "include '<PROJECT_NAME>', '<ORIGINAL_PROJECT>'"
  • Rong den Lang beng
    Rong den Lang beng about 11 years
    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
    Rong den Lang beng about 11 years
    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.
  • Varundroid
    Varundroid about 11 years
    @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.
  • Matt
    Matt about 11 years
    It took me three hours to find this answer. Thank you!
  • Gustavo
    Gustavo about 11 years
    @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.
  • Rong den Lang beng
    Rong den Lang beng about 11 years
    @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.
  • Gustavo
    Gustavo about 11 years
    Well, then I'll keep trying, if I get to any solution. I'll get in touch with you.
  • Israel Lopez
    Israel Lopez almost 11 years
    I just tried following these steps, and I'm not able to have Android Studio auto-complete. Still trying to figure out why.
  • MikeWallaceDev
    MikeWallaceDev almost 11 years
    Hi @Gustavo, I would like to try this approach. How exactly did you build the openCV.jar?
  • fredley
    fredley over 10 years
    I get A problem occurred configuring the project ':OpenCV' - Main manifest missing from /.../OpenCV/src/main/AndroidManifest.xml Any ideas?