Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio and Gradle dependency integration

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?

like image 804
Rong den Lang beng Avatar asked May 18 '13 17:05

Rong den Lang beng


People also ask

What is Gradle dependencies in Android Studio?

The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well.

How do I sync Gradle with Android Studio?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

Does Android Studio use Gradle?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.

How do I sync Gradle with dependencies?

Simply open the gradle tab (can be located on the right) and right-click on the parent in the list (should be called "Android"), then select "Refresh dependencies".


3 Answers

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.

like image 134
rynojvr Avatar answered Oct 21 '22 20:10

rynojvr


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.

like image 36
Mahm00d Avatar answered Oct 21 '22 18:10

Mahm00d


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.

like image 34
Xi 张熹 Avatar answered Oct 21 '22 18:10

Xi 张熹