18

I imported the opencv android library to my android studio and the Camera2Renderer class has a lot of compiler errors because the android.hardware.camera2 classes can't be imported.

enter image description here

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
Victory Omole
  • 693
  • 1
  • 6
  • 15
  • 2
    show the logs for the build - your error can be from many possible sources: wrong target API, bad XML file, project file naming issues, etc. – Jim Dec 25 '15 at 08:06
  • I have a feeling that it is from the wrong target API. I'm also new to android development. Are the logs for the build displayed in event log? – Victory Omole Dec 25 '15 at 10:12
  • Do you target **android-21**? – Alex Cohn Dec 25 '15 at 13:52
  • Usually, yes. You can also go to the "terminal" window and you should be able to type "gradle build" or on windows "gradlew build" and see the build output – Jim Dec 25 '15 at 23:34

4 Answers4

37

I solved the problem. Jim was right, I did not have the correct target API. For the next person who has this problem and finds this thread, the solution is that you have to make sure that the build.gradle files for your project and your OpenCV match.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Victory Omole
  • 693
  • 1
  • 6
  • 15
5

I am working with openCVLibrary3.2.0 and trying to run its sample and faced same issue but I changed gradle files for both mysampleApp and openCVLibrary320 module as below

This is my app build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "org.opencv.face"
        minSdkVersion 9
        targetSdkVersion 21
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_5
            targetCompatibility JavaVersion.VERSION_1_5
        }

        ndk {
            moduleName "native_sample"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
}

dependencies {
    compile project(path: ':openCVLibrary320')
}

and this my OpenCV library module build.gradle file

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 21
    }

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

Note: things to notice are that compileSdkVersion, buildToolsVersion, minSdkVersion, and targetSdkVersion these must be same for all gradle files

I changed the compileSdkVersion from 14 to 23 and buildToolsVersion to "23.0.2" this solved the camera2 import related issue

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Muhammad Natiq
  • 213
  • 5
  • 14
1

I am working on openCVLibrary330 trying to run its sample and faced same issue but i changed gradle files for openCVLibrary330 module as below

This is my app build.gradle

apply plugin: 'com.android.library'

android {
  compileSdkVersion 23

  buildToolsVersion "26.0.2"

  defaultConfig {
    minSdkVersion 8
    targetSdkVersion 21
  }

  buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
  }
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
0

I had the same problem. But, as many people have suggested I didn't had to change the gradle files to match exactly the same. I changed my compileSdkVersion and buildToolsVersion to the same on both gradle files, the rest are different. Its working fine now.