8

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 ':app@debug/compileClasspath': Could not resolve project :openCVLibrary410.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve project :openCVLibrary410.
Show Details
Affected Modules: app


ERROR: Unable to resolve dependency for ':app@debugUnitTest/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?

Boken
  • 4,825
  • 10
  • 32
  • 42
flackbash
  • 488
  • 2
  • 16

2 Answers2

14

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.

flackbash
  • 488
  • 2
  • 16
  • @flackbash Mate, thanks so much for this! I had the exact same problem when trying to add the openCV 3.4.3 module to my app module. To be honest with you, I'm an android novice and I'm still learning so would you care to explain why this solution ^^ fixed it? Thanks in advance! – blue2609 May 09 '19 at 11:47
  • I'm not sure about the specifics either but [this](https://stackoverflow.com/questions/27342458/apply-plugin-android-or-apply-plugin-com-android-application) or [this](https://developer.android.com/studio/projects/android-library) might help. – flackbash May 26 '19 at 16:45
  • 1
    Something additional to do is to remove this line applicationId "org.opencv" because a Gradle build error returns saying that library projects do not include applicationid. – Ahmed Gad Jun 09 '19 at 18:23
  • 1
    You're my hero. Seriously. Hours debugging this. – Mark Mayo Apr 17 '20 at 08:41
0

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.

nova
  • 1
  • 1