Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject = 无法在项目中找到匹配的配置

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

我在将我的项目迁移到最新的 Gradle 4.0 + Android Studio 3 版本时遇到问题,这给了我各种错误。渐渐地我设法把它们都整理出来了,除了这个。

    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.

为了确定问题:

  1. 我已经从 Android Studios 项目助手
  2. 创建了一个最小的应用程序项目
  3. 添加了一个空库模块,然后我将其添加到我的应用依赖项中。
  4. 添加了一个 flavorDimensions 和 2 个 productFlavors
  5. 添加了 3 种构建类型并让一种构建类型继承自另一种构建类型
  6. 让继承的构建类型启用shrinkResources

最后一步产生了上述错误,类似于这个问题:

有没有人知道这是怎么回事或解决这个问题的方法?我也会提交错误报告。

我完整的 gradle 文件:

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')
}

可能的解决方法是在所有缺少 buildTypes 的模块中创建,但是当 Google 计划为其创建解决方案时,它是疯狂的混乱代码。更多信息见:https://issuetracker.google.com/issues/62170415 作为我(但被版主删除)和你提到。

但是还有第二种(相同但更简洁)解决方案: 将此添加到您的顶级项目 build.gradle

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

编辑:2017-07-12

最终在 classpath 'com.android.tools.build:gradle:3.0.0-alpha6' 中修复。 您可以使用新的 DSL:https://issuetracker.google.com/issues/62241369

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

不要忘记在构建项目之前删除上述解决方法!

编辑:2017-07-18

有官方文档: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'
}

编辑:2017-09-06

buildTypeMatching 已从 AS beta 4 中删除。
现在使用 matchingFallbacks.
参见:

可能与

重复

确保您的所有模块构建配置 (buildTypes) 的确切数量

在我的 3.0 之前的设置中,我的所有 [=25] 中只有 调试{}发布{} =] 模块。我添加了一个类似于 :app 模块的配置。它对我来说很好。

如果您的应用包含库依赖项不包含的构建类型。

例如,您的应用包含 "staging" 构建类型,但依赖项仅包含 "debug" 和 "release" 构建类型。

你会得到类似

的错误

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

您可以通过添加

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

解决与依赖匹配相关的构建错误official docs

如果你到达这里,那么我的解决方案是:

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

        build {
            //   minifyEnabled false
            //   proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }