Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ':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?

like image 950
flackbash Avatar asked Apr 09 '19 21:04

flackbash


People also ask

Can you use OpenCV in Android Studio?

To work with the OpenCV Android library, you have to add it to your app module as a dependency. To easily do this on Android Studio, click on File -> Project Structure. When the project structure dialog opens, click on the app module or any other module that you want to use OpenCV library in.

Which OpenCV version should I use for Android studio?

Step-1: Android Studio Project Configuration. In this section, we are going to reproduce the project creation process in Android Studio step-by-step from scratch. For the application development, we use 4.0. 1 IDE version and opencv-4.3.


1 Answers

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.

like image 64
flackbash Avatar answered Sep 28 '22 20:09

flackbash