Android Studio 3 + Gradle 4.0 + shrinkResources + libraryProject =无法在项目

时间:2017-05-29 12:50:02

标签: 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. 添加了一个空的库模块,然后我将其添加到我的应用程序依赖项中。
  4. 添加了一个flavorDimensions和2个productFlavors
  5. 添加了3种构建类型,让一种构建类型继承自另一种构建类型
  6. 让继承的构建类型启用shrinkResources
  7. 最后一步产生上述错误,与此问题类似: 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')
    }
    

4 个答案:

答案 0 :(得分:7)

可能的解决方法是在缺少buildTypes的所有模块中创建,但是当Google计划为其创建一个sollution时,它会疯狂地搞乱代码。更多信息请参阅: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

  

要解决此错误,您需要指定来自哪种构建类型   “mylibrary”Android插件应该与应用程序的“升级”相匹配   构建类型。您可以使用中的buildTypeMatching属性执行此操作   app的build.gradle文件,如下所示:

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

答案 1 :(得分:3)

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

例如,您的应用包含“暂存”构建类型,但依赖项仅包含“调试”和“发布”构建类型。

您将收到类似

的错误

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

您可以通过添加

来解决此错误
<div id="xxx12312" [style.background-color]="dynamicColor">1. Some data</div>
<div id="yyadsfas" [style.background-color]="dynamicColor">2. Some more data</div>
<div id="00012123" [style.background-color]="dynamicColor">3. Some even more data</div>

解决与依赖项匹配official docs

相关的构建错误

答案 2 :(得分:0)

Gradle 4.0 Unable to find a matching configuration

可能重复

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

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

答案 3 :(得分:0)

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

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

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