[Solved] Including OpenCV in Android Studio project yields ‘Unresolved dependencies’ error

I’m trying to include OpenCV in my Android Studio project using this step by step guide. However, after adding OpenCV as module dependency (step 4 in the guide) the Gradle project sync fails with the following errors:

ERROR: Unable to resolve dependency for ':[email protected]/compileClasspath': Could not resolve project :openCVLibrary410.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':[email protected]/compileClasspath': Could not resolve project :openCVLibrary410.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':[email protected]/compileClasspath': Could not resolve project :openCVLibrary410.
Show Details
Affected Modules: app

I found this related question and tried adjusting the buildTypes in the openCV build.gradle by adding a debug field, but it did not change anything. I also tried to adjust the app’s build.gradle as follows

    buildTypes {
        release {
            ...
        }
        debug {
            matchingFallbacks = ['release']
        }
    }

(and several slightly different versions of this because I wasn’t entirely sure what I was doing there) but none helped. I also tried setting the build variant of the OpenCV library to release instead of debug, but this resulted in the following error:

Unable to find a matching variant of project :openCVLibrary410:
  - Variant 'debugApiElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
      - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
  - Variant 'debugBundleElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' but no value provided.
      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but no value provided.
      - Required org.gradle.usage 'java-api' and found incompatible value 'android-bundle'.
  - Variant 'debugMetadataElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Metadata'.
      - Required org.gradle.usage 'java-api' but no value provided.
  - Variant 'debugRuntimeElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
      - Required org.gradle.usage 'java-api' and found incompatible value 'java-runtime'.
  - Variant 'releaseApiElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'release'.
      - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
      - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
  - Variant 'releaseBundleElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' but no value provided.
      - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' but no value provided.
      - Required org.gradle.usage 'java-api' and found incompatible value 'android-bundle'.
  - Variant 'releaseMetadataElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'release'.
      - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Metadata'.
      - Required org.gradle.usage 'java-api' but no value provided.
  - Variant 'releaseRuntimeElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'release'.
      - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
      - Required org.gradle.usage 'java-api' and found incompatible value 'java-runtime'.

Any idea how to resolve this?

Solution #1:

I finally solved this by changing the line

apply plugin: 'com.android.application'

in the imported OpenCV module’s build.gradle file to

apply plugin: 'com.android.library'

and deleting the defaultConfig field in the same gradle file.
I found this solution here.

Respondent: flackbash
Solution #2:

Import version 3.4.6 slightly older than the latest version.
The latest version may not be stable yet.

The same symptoms occurred in my environment but were resolved using version 3.4.6.

Respondent: nova
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.