Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject = Unable to find a matching configuration in project

我怕爱的太早我们不能终老 提交于 2019-12-30 08:13:14

问题


I've problems to migrate my project to the newest Gradle 4.0 + Android Studio 3 version, which gives me all kind of errors. Little by little I managed to sort them all out except this one.

    Could not resolve all dependencies for configuration ':app:forGoogleCoverageRuntimeClasspath'.
   > Unable to find a matching configuration in project :mylibrary:
       - Configuration 'debugApiElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=coverage}' and found incompatible value 'BuildTypeAttr{name=debug}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=debug}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found incompatible value 'for compile'.
           - Required store 'ProductFlavorAttr{name=forGoogle}' but no value provided.
       - Configuration 'debugRuntimeElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=coverage}' and found incompatible value 'BuildTypeAttr{name=debug}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=debug}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found compatible value 'for runtime'.
           - Required store 'ProductFlavorAttr{name=forGoogle}' but no value provided.
       - Configuration 'releaseApiElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=coverage}' and found incompatible value 'BuildTypeAttr{name=release}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=release}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found incompatible value 'for compile'.
           - Required store 'ProductFlavorAttr{name=forGoogle}' but no value provided.
       - Configuration 'releaseRuntimeElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=coverage}' and found incompatible value 'BuildTypeAttr{name=release}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=release}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found compatible value 'for runtime'.
           - Required store 'ProductFlavorAttr{name=forGoogle}' but no value provided.

In order to nail down the problem:

  1. I've created a minimum app project from Android Studios project assistant
  2. added an empty library module which I then add to my app dependencies.
  3. added one flavorDimensions and 2 productFlavors
  4. added 3 build types and let one build type inherit from another
  5. let the inherited build type enable shrinkResources

The last step produces the mentioned error, which is similar to this question: Gradle 4.0 Unable to find a matching configuration

Has anybody an idea what's the matter here or an solution to this problem? I'll file a bug report too.

My complete gradle file:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "gradletest.test"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    flavorDimensions "store"

    productFlavors {
        forAmazon {
            dimension "store"
        }

        forGoogle {
            dimension "store"
        }
    }

    buildTypes {

        debug {
            debuggable true
            minifyEnabled false
        }

        release {
            minifyEnabled true
            debuggable false
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        coverage.initWith(buildTypes.debug)
        coverage {
            testCoverageEnabled true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    implementation project(':mylibrary')
}

回答1:


Possible workaround is create in all modules missing buildTypes, but it's crazy messing code when Google planed create a sollution for it. More info in: https://issuetracker.google.com/issues/62170415 as me (but deleted by moderator) and you mention.

But there is second (same but much cleaner) solution: add this to your top project build.gradle

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                buildTypes {
                    YOUR_MISSING_BUILD_TYPES {
                       BUILD_TYPE_PARAMS_OR_EMPTY
                    }
                }
            }
        }
    }
}

EDIT: 2017-07-12

It's finally fixed in classpath 'com.android.tools.build:gradle:3.0.0-alpha6'. You can use new DSL: https://issuetracker.google.com/issues/62241369

android {
  buildTypeMatching 'staging', 'debug'
  productFlavorMatching 'color', 'blue', 'cyan'
}

Don't forgot to remove above workaround before build project!

EDIT: 2017-07-18

There is official documentation: https://issuetracker.google.com/issues/62241369

To resolve this error, you need to specify which build type from "mylibrary" the Android plugin should match to the app's "staging" build type. You can do this with the buildTypeMatching property in the app's build.gradle file, as shown below:

// Add the following to the consumer's build.gradle file.
android {
    ...
    // Tells the Android plugin to use a library's 'debug' build type
    // when a 'staging' build type is not available. You can include
    // additional build types, and the plugin matches 'staging' to the
    // first build type it finds from the one's you specify. That is,
    // if 'mylibrary' doesn't include a 'debug' build type either, the
    // plugin matches 'staging' with the producer's 'release' build type.
    buildTypeMatching 'staging', 'debug', 'release'
}

EDIT: 2017-09-06

buildTypeMatching has been removed from AS beta 4.
now use matchingFallbacks.
see: https://stackoverflow.com/a/46038946/4594990




回答2:


IF your app includes a build type that a library dependency does not.

For example, your app includes a "staging" build type, but a dependency includes only a "debug" and "release" build type.

You will get error like

Unable to resolve dependency for ':app@staging/compileClasspath': Could not resolve project :library. Open File Show Details

You can resolve this error by adding

buildTypes {
        staging {
            proguardFile getDefaultDexGuardFile('dexguard-release.pro')
            proguardFile 'dexguard-rules.pro'
            matchingFallbacks = ['debug', 'release'] //add this line
        }
    }

Resolve build errors related to dependency matching official docs




回答3:


Possible duplicate of Gradle 4.0 Unable to find a matching configuration

Make sure you have the exact number of build configurations (buildTypes) in all of your modules.

In my pre-3.0 setup, I was having only debug{} and release{} in all of my com.android.library modules. I added one more configuration similar to that of :app module. It builds fine for me.



来源:https://stackoverflow.com/questions/44242945/android-studio-3-gradle-4-0-shrinkresources-libraryproject-unable-to-fin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!