flavorDimensions gradle错误 - Android Studio 3.0 Canary 1

时间:2017-05-22 13:22:13

标签: android android-studio android-gradle

我想将我的项目升级到Android Studio 3.0 Canary 1和gradle:3.0.0-alpha1插件,但是我对包含项目的正确风味定义有疑问。

我遵循了Google的官方迁移指南:https://developer.android.com/studio/preview/features/new-android-plugin-migration.html

在我的主应用build.gradle中,我按照迁移指南的建议定义了“myflavor” flavorDimensions

import subprocess
child = subprocess.Popen(["xargs", "ls"], stdin=subprocess.PIPE, universal_newlines=True)
filepath = '/Users/haofly'
child.communicate(filepath)

在我的included:util模块(生产者)中,我试图在没有flavorDimensions,productFlavors和buildTypes定义的情况下包含它。我收到以下gradle错误:

android {
    flavorDimensions "myflavor"

    productFlavors {
        dev {
            dimension "myflavor"
            minSdkVersion 21
        }
        prod {
            dimension "myflavor"
            minSdkVersion 16
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }

        release {
            debuggable false

            signingConfig signingConfigs.release

            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
}

dependencies {
    compile project(':util')
}

在main:app中定义相同的flavorDimensions,productFlavors和buildTypes:util时,我收到以下错误消息:

Error:Could not resolve all dependencies for configuration ':app:prodDebugCompileClasspath'.
  Cannot choose between the following configurations on project :util:
    - debugApiElements
    - releaseApiElements
  All of them match the consumer attributes:
    - Configuration 'debugApiElements':
        - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' but no value provided.
        - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' but wasn't required.
        - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=debug}' but no value provided.
        - Found com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=debug}' but wasn't required.
        - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=debug}' but wasn't required.
        - Required myflavor 'ProductFlavorAttr{name=prod}' but no value provided.
        - Required org.gradle.api.attributes.Usage 'for compile' and found compatible value 'for compile'.
    - Configuration 'releaseApiElements':
        - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' but no value provided.
        - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' but wasn't required.
        - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=debug}' but no value provided.
        - Found com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=release}' but wasn't required.
        - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=release}' but wasn't required.
        - Required myflavor 'ProductFlavorAttr{name=prod}' but no value provided.
        - Required org.gradle.api.attributes.Usage 'for compile' and found compatible value 'for compile'.

在没有收到错误的情况下包含my:util lib的正确方法是什么?

4 个答案:

答案 0 :(得分:64)

看起来像是Gradle构建过程中的一个错误,我不太清楚为什么它无法正常工作。我被困在这一段时间但我能够通过改变

来解决这个问题
compile project(':util')

compile project(path: ':util', configuration: 'default')

请告诉我这是否适合您!

答案 1 :(得分:9)

我在使用Canary 2时遇到了类似的问题,但是提出的解决方案并没有奏效。显然,有一种方法可以使用本地库或项目中的实现:

// This is the old method and no longer works for local
// library modules:
// debugCompile project(path: ':foo', configuration: 'debug')
// releaseCompile project(path: ':foo', configuration: 'release')

// Instead, simply use the following to take advantage of
// variant-aware dependency resolution. You can learn more about
// the 'implementation' configuration in the section about
// new dependency configurations.
implementation project(':foo')

Reference from Studio Documentation

答案 2 :(得分:-1)

来自https://issuetracker.google.com/issues/65867056

根build.gradle中应该只有一个buildScript块。 如果您使用的是复合构建,由于Gradle中的限制,我们的插件目前不支持此功能。

编辑:我想现在这适用于最新版本的gradle

答案 3 :(得分:-1)

我使用gradle 4.3(使用Android Studio 3.0)来解决这个问题

要在Android Studio 3.0中使用

  1. 在此处下载二进制文件:https://gradle.org/releases/
  2. 在Android Studio中的首选项...
  3. 下配置本地gradle二进制文件

    enter image description here

    1. 然后在您的gradle文件中,将编译替换为实施 api ,具体取决于您的要求。
    2. 的引用: https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html

      另外:确保外部模块(即util)与您的应用程序具有相同的风格,否则您将收到此处列出的错误:Gradle 4.0 Unable to find a matching configuration