Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject =无法在项目中find匹配的configuration

我有问题将我的项目迁移到最新的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项目助理创build了一个最小的应用程序项目
  2. 添加了一个空的库模块,然后添加到我的应用程序依赖项。
  3. 添加了一个flavorDimensions和2个productFlavors
  4. 添加了3个构buildtypes,让一个构buildtypes从另一个inheritance
  5. 让inheritance的构buildtypes启用shrinkResources

最后一步产生提到的错误,这是类似于这个问题: Gradle 4.0无法find匹配的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') } 

Solutions Collecting From Web of "Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject =无法在项目中find匹配的configuration"

可能的解决方法是在缺lessbuildTypes的所有模块中创build,但是当Google计划创build一个解决scheme时,这是疯狂的混乱代码。 更多信息: https : //issuetracker.google.com/issues/62170415作为我(但由主持人删除),你提到。

但是还有第二个(相同但更清洁)的解决scheme:将其添加到您的顶级项目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

为了解决这个错误,你需要指定Android插件应该匹配到应用程序的“暂存”构buildtypes的“mylibrary”中的哪个构buildtypes。 您可以使用应用程序的build.gradle文件中的buildTypeMatching属性来执行此操作,如下所示:

 // 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

如果您的应用程序包含库依赖关系不包含的构buildtypes。

例如,您的应用程序包含“暂存”构buildtypes,但依赖项仅包含“debugging”和“发布”构buildtypes。

你会得到像这样的错误

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

解决与依赖项匹配官方文档相关的构build错误

Gradle 4.0可能的重复无法find匹配的configuration

确保在所有模块中都有确切的构buildconfiguration(buildTypes)数量

在我3.0之前的设置中,我只在所有的com.android.library模块中debugging释放{} 。 我添加了一个类似于:app模块的configuration。 它对我来说很好。