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

标签 android gradle build.gradle android-build-type android-studio-3.0

我在将我的项目迁移到最新的 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. 添加了一个 flavorDimensions 和 2 productFlavors
  4. 添加了 3 种构建类型并让一种构建类型继承自另一种
  5. 让继承的构建类型启用shr​​inkResources

最后一步产生了提到的错误,类似于这个问题: Gradle 4.0 Unable to find a matching configuration

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

我完整的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
参见:https://stackoverflow.com/a/46038946/4594990

关于Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject = 无法在项目中找到匹配的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44242945/

相关文章:

android - Oreo 的依赖项更改会出错

java - 清理 Android 主要 Activity 代码

javascript - Android webkit 浏览器中的 XPCOM 支持?

plugins - Gradle JavaExec Action 的非内部替代方案

java - 任务定义/gradle中的定义

android - Gradle 在构建 Flutter 应用程序时突然开始出错

gradle - 如何从 Gradle 构建中设置环境变量?

android - 如何在 Android Studio 中使用 gradle 实验性插件 0.2.0 将静态库(.a 文件)与 APK 链接

android - 无法加载 libfoo : findLibrary returned null

android - 导航组件在 onActivityResult 内导航不起作用