"Could not resolve all task dependencies for configuration" 有两个维度,尽管有 matchingFallbacks

"Could not resolve all task dependencies for configuration" with two dimensions, despite having matchingFallbacks

我知道这是一个常见问题,但似乎没有任何内容适用于此问题。

我有一个 Android 项目和它所依赖的一堆库。该项目有一个维度“env”,可以是“mock”、“debug”、“release”。该库具有相同的维度,但只有“调试”或“发布”。 “模拟”在“调试”上有后备。

这是一个简化的设置:

应用的 build.gradle:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        coreLibraryDesugaringEnabled true
    }
    buildTypes {
        release {
        }
        staging {
            matchingFallbacks = ['debug']
        }
        debug {
        }
    }
    flavorDimensions 'env'

    productFlavors {
        mock {
            dimension 'env'
            matchingFallbacks = ['dev']
        }
        dev {
            dimension 'env'
        }
        prod {
            dimension 'env'
        }
    }
    sourceSets {
        dev {
            java.srcDirs = ['src/main/java', 'src/live/java', 'src/dev/java']
        }
        staging {
            java.srcDirs = ['src/main/java', 'src/live/java', 'src/staging/java']
        }
        prod {
            java.srcDirs = ['src/main/java', 'src/live/java', 'src/prod/java']
        }
        androidTest {
            java.srcDirs = ['src/main/java', 'src/mock/java', 'src/androidTest/java']
        }
        androidTestMockDebug {
            java.srcDirs = ['src/main/java', 'src/mock/java', 'src/androidTest/java']
        }
    }
dependencies {
    implementation project(path: ':myLibrary:myLibrary')

该库在子目录 myLibrary/myLibrary 中,还有一个 myLibrary/sample 我不想包含在应用程序中。

图书馆的 build.gradle:

android {
    buildTypes {
        debug {
        }
        release {
        }
    }

    flavorDimensions 'env'

    productFlavors {
        dev {
            dimension 'env'
        }
        prod {
            dimension 'env'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

当我尝试构建 mockDebug 时出现此错误(这是一个摘录,完整的错误很长):

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':myApp:hiltJavaCompileMockDebug'.
> Could not resolve all task dependencies for configuration ':myApp:hiltCompileOnlyMockDebug'.
   > Could not resolve project :myLibrary:myLibrary.
     Required by:
         project :myApp
      > No matching variant of project :myLibrary:myLibrary was found. The consumer was configured to find a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'env' with value 'mock' but:
          - Variant 'devDebugApiElements' capability myApp.myLibrary:myLibrary:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug':
              - Incompatible because this component declares an API of a component, as well as attribute 'env' with value 'dev' and the consumer needed a runtime of a component, as well as attribute 'env' with value 'mock'
          - Variant 'devDebugRuntimeElements' capability myApp.myLibrary:myLibrary:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug':
              - Incompatible because this component declares a component, as well as attribute 'env' with value 'dev' and the consumer needed a component, as well as attribute 'env' with value 'mock'
          - Variant 'devReleaseApiElements' capability myApp.myLibrary:myLibrary:unspecified:
              - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'env' with value 'dev' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'env' with value 'mock'
          - Variant 'devReleaseRuntimeElements' capability myApp.myLibrary:myLibrary:unspecified declares a runtime of a component:
              - Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'env' with value 'dev' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'env' with value 'mock'
          - Variant 'prodDebugApiElements' capability myApp.myLibrary:myLibrary:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug':
              - Incompatible because this component declares an API of a component, as well as attribute 'env' with value 'prod' and the consumer needed a runtime of a component, as well as attribute 'env' with value 'mock'
          - Variant 'prodDebugRuntimeElements' capability myApp.myLibrary:myLibrary:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug':
              - Incompatible because this component declares a component, as well as attribute 'env' with value 'prod' and the consumer needed a component, as well as attribute 'env' with value 'mock'
          - Variant 'prodReleaseApiElements' capability myApp.myLibrary:myLibrary:unspecified:
              - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'env' with value 'prod' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'env' with value 'mock'
          - Variant 'prodReleaseRuntimeElements' capability myApp.myLibrary:myLibrary:unspecified declares a runtime of a component:
              - Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'env' with value 'prod' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'env' with value 'mock'

如果我在 devDebug 中编译,这一切工作正常,但是当我在 mockDebug 中编译时,这里会出现这些错误。

这似乎是 Gradle 中的错误。

我在 com.android.tools.build:gradle:7.1.2(任何 7.1.* 版本)和 7.2 beta 版本中都有这个问题。返回 7.0.4 解决问题。