31

I'm trying to add the OpenCV module to a project, but I'm not able to solve some gradle issues. Here is my app.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 17
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.sveder.cardboardpassthrough"
        minSdkVersion 8
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile files('libs/cardboard.jar')
    compile project(':openCVLibrary')
}

Here's the build.gradle file of the OpenCV (3.0) module:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 17
    buildToolsVersion "23.0.2"

defaultConfig {
    minSdkVersion 8
    targetSdkVersion 21
}

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

As you can see, my app and OpenCV gradle files match, and yet whenever I try to build the project I have the error error: package android.hardware.camera2 does not exist. What am I doing wrong?

Thanks in advance.

user3059347
  • 529
  • 1
  • 9
  • 15

3 Answers3

89

Change compileSdkVersion 17 to minimum 21 (recommended latest). Because android.hardware.camera2 was added in API 21.

Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • The phone I'm developing with has an API version of 17...does this mean I have to switch to an older version of OpenCV? Is there no "compatiability" mode that allows me to revert to the old Camera API? – user3059347 Mar 24 '16 at 16:18
  • You are already targeting ` minSdkVersion 8`, so it would work on 17. – Rohit Arya Mar 24 '16 at 16:25
  • The problem is, it doesn't. It still complains about missing camera2 even though it should be defaulting to the older Camera API. – user3059347 Mar 24 '16 at 16:32
  • No, I am saying. change `compileSdkVersion` to 23, then also it would work on 17 because minSdkVersion is 8. `camera2` can't be imported because it was added in 21. – Rohit Arya Mar 24 '16 at 16:37
  • That did it! Thanks!! – user3059347 Mar 24 '16 at 16:39
33

Apart from build.gradle . Check settings in the Project Structure Ctrl +Alt+Shift+s . This solved my problem. enter image description here

Binil Jacob
  • 501
  • 6
  • 9
3

I tried to change the sdkversion as Rohit said, but the problem was there. But then I realized that I need to change the sdkversion for the OpenCV library. So press Shift+Ctrl+Alt+s, get to the properties tab of OpenCV library. Change the sdkVersion to 21 or later.Here is the screenshot, in case if you are confused with tabs