Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject =无法在项目中找到匹配的配置 [英] Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject = Unable to find a matching configuration in project

查看:1475
本文介绍了Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject =无法在项目中找到匹配的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题将我的项目迁移到最新的Gradle 4.0 + Android Studio 3版本,这给我带来了各种各样的错误。

 无法解析配置的所有依赖关系::app:forGoogleCoverageRuntimeClasspath 。 
>无法在项目中找到匹配配置:mylibrary:
- 配置'debugApiElements':
- 必需的com.android.build.gradle.internal.dependency.AndroidTypeAttr'AndroidTypeAttr {name = Aar}'并找到兼容值'AndroidTypeAttr {name = Aar}'。
- 必需的com.android.build.gradle.internal.dependency.BuildTypeAttr'BuildTypeAttr {name = coverage}',并找到不兼容的值'BuildTypeAttr {name = debug}'。
- 找到com.android.build.gradle.internal.dependency.VariantAttr'VariantAttr {name = debug}',但不是必需的。
- 运行时所需的org.gradle.api.attributes.Usage',发现'compile'不兼容的值。
- 所需商店'ProductFlavorAttr {name = forGoogle}',但未提供任何值。
- 配置'debugRuntimeElements':
- 必需的com.android.build.gradle.internal.dependency.AndroidTypeAttr'AndroidTypeAttr {name = Aar}',并找到兼容值'AndroidTypeAttr {name = Aar}'。
- 必需的com.android.build.gradle.internal.dependency.BuildTypeAttr'BuildTypeAttr {name = coverage}',并找到不兼容的值'BuildTypeAttr {name = debug}'。
- 找到com.android.build.gradle.internal.dependency.VariantAttr'VariantAttr {name = debug}',但不是必需的。
- 运行时所需的org.gradle.api.attributes.Usage'并找到兼容值'for runtime'。
- 所需商店'ProductFlavorAttr {name = forGoogle}',但未提供任何值。
- 配置'releaseApiElements':
- 必需的com.android.build.gradle.internal.dependency.AndroidTypeAttr'AndroidTypeAttr {name = Aar}',并找到兼容值'AndroidTypeAttr {name = Aar}'。
- 必需的com.android.build.gradle.internal.dependency.BuildTypeAttr'BuildTypeAttr {name = coverage}',并找到不兼容的值'BuildTypeAttr {name = release}'。
- 找到com.android.build.gradle.internal.dependency.VariantAttr'VariantAttr {name = release}',但不是必需的。
- 运行时所需的org.gradle.api.attributes.Usage',发现'compile'不兼容的值。
- 所需商店'ProductFlavorAttr {name = forGoogle}',但未提供任何值。
- 配置'releaseRuntimeElements':
- 必需的com.android.build.gradle.internal.dependency.AndroidTypeAttr'AndroidTypeAttr {name = Aar}',并找到兼容值'AndroidTypeAttr {name = Aar}'。
- 必需的com.android.build.gradle.internal.dependency.BuildTypeAttr'BuildTypeAttr {name = coverage}',并找到不兼容的值'BuildTypeAttr {name = release}'。
- 找到com.android.build.gradle.internal.dependency.VariantAttr'VariantAttr {name = release}',但不是必需的。
- 运行时所需的org.gradle.api.attributes.Usage'并找到兼容值'for runtime'。
- 所需商店'ProductFlavorAttr {name = forGoogle}',但未提供任何值。

为了确定问题:


  1. 我从Android Studios项目助理创建了一个最小应用项目

  2. 添加了一个空的库模块,然后添加到我的应用依赖项中。
  3. >
  4. 添加了一个flavorDimensions和2个productFlavors
  5. 添加了3个构建类型,并让一个构建类型从另一个继承

  6. 让继承的构建类型启用 shrinkResources

步骤产生提到的错误,这是类似于这个问题:
Gradle 4.0无法找到匹配的配置



有没有人知道这里有什么问题或解决方案?我还会提交一个错误报告。



我的完整gradle文件:

  apply plugin:'com.android.application'

apply plugin:'kotlin-android'

android {
compileSdkVersion 25
buildToolsVersion 25.0.2
defaultConfig {
applicationIdgradletest.test
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName1.0
testInstrumentationRunnerandroid.support.test.runner.AndroidJUnitRunner
}

flavorDimensionsstore

productFlavors {
forAmazon {
维度商店
}

for Google {
维度商店
}
}

buildTypes {

debug {
debuggable true
minifyEnabled false
}

release {
minifyEn能够真正
可调试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'
}
}


}

依赖项{
编译fileTree(dir:'libs' ,包括:['* .jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
exclude group:'com.android。支持',模块:'support-annotations'
})
compileorg.jetbrains.kotlin:kotlin-stdlib-jre7:$ kotlin_version
compile'c​​om.android.support:appcompat -v7:25.3.1'
testCompile junit:junit:4.12'
compile'c​​om.android.support.constraint:constraint-layout:1.0.2'

实现项目(':mylibrary')
}


解决方案

可能的解决方法是在所有缺少buildType的模块中创建,当谷歌计划创造一种解决方案时,疯狂的混乱代码。更多信息: https://issuetracker.google.com/issues/62170415 as me(但是你可以提到。



但是还有第二个(相同但很干净)的解决方案:
将它添加到您的顶级项目中 build.gradle

 子项目{
afterEvaluate {project - >
if(project.hasProperty(android)){
android {
buildTypes {
YOUR_MISSING_BUILD_TYPES {
BUILD_TYPE_PARAMS_OR_EMPTY
}
}



$ b code

$ b

编辑:2017-07-12



它最终被固定在 classpath'com.android.tools.build:gradle:3.0 0.0-alpha6'
您可以使用新的DSL: https://issuetracker.google.com/issues/62241369

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

不要忘记删除编译:2017-07-18


$ b $上面的解决方法在构建项目之前完成!

有官方文档: https://issuetracker.google.com/issues/62241369


要解决此错误,您需要指定Android插件应匹配的
mylibrary中的哪个生成类型该应用的分段
构建类型。您可以使用
应用程序的build.gradle文件中的buildTypeMatching属性来执行此操作,如下所示:




  //将以下内容添加到使用者的build.gradle文件中。 
android {
...
//告诉Android插件使用库的'debug'构建类型
//当'暂存'构建类型不可用时。您可以包含
//额外的构建类型,并且该插件将'staging'与您从您指定的那个找到的
//第一个构建类型相匹配。也就是说,
//如果'mylibrary'不包含'debug'构建类型,则
//插件会将'staging'与生产者的'release'构建类型匹配。
buildTypeMatching'staging','debug','release'
}

编辑:2017-09-06



buildTypeMatching 已从AS beta 4中删除。
现在使用 matchingFallbacks

请参阅: https://stackoverflow.com/a/46038946/4594990


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

解决方案

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

这篇关于Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject =无法在项目中找到匹配的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆