【问题标题】:Android buildTypes vs. productFlavors errorAndroid buildTypes 与 productFlavors 错误
【发布时间】:2021-02-21 18:37:21
【问题描述】:

决定使用 Gradle 6.5 从 Android gradle plugin 3.5.x 切换到 gradle plugin 4.10 并卡住了错误。

这是我的 build.gradle

defaultConfig {

    applicationId "com.example.myapp
    targetSdkVersion targetSdkVersionVar
    multiDexEnabled true
}

flavorDimensions "version"

productFlavors {
    kitkat {
        sourceSets { "src/android/kitkat" }
        dimension "version"
        minSdkVersion minSdkKitkat
        targetSdkVersion targetSdkKitkat
        versionNameSuffix "-kitkat"
    }
    nougat {
        sourceSets { "src/android/nougat" }
        dimension "version"
        minSdkVersion minSdkNougat
        targetSdkVersion targetSdkVersionVar
        versionNameSuffix "-nougat"
    }
}


buildTypes {

    debug {
        multiDexKeepFile file('multidex-config.txt')
        multiDexKeepProguard file('multidex-config.pro')
        minifyEnabled false
        zipAlignEnabled true
        sourceSets {

            //main.java.srcDirs += 'src/main/kotlin'
            main {
                java.srcDirs = ['src/android/default/java/', 'src/common/java/']
                jni.srcDirs = [] //disable ndk support
                manifest.srcFile 'src/android/default/AndroidManifest.xml'
                resources.srcDirs = ['src/android/default']
                aidl.srcDirs = ['src/android/default']
                renderscript.srcDirs = ['src/android/default']
                res.srcDirs = ['src/android/default/res']
                assets.srcDirs = ['src/android/default/assets']
            }
            test {
                java.srcDirs = ['src/test/java']
            }
            androidTest {
                java.srcDirs = ['src/androidTest/java']
            }
        }
    }
    release {
        multiDexKeepFile file('multidex-config.txt')
        multiDexKeepProguard file('multidex-config.pro')
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        zipAlignEnabled true
        sourceSets {

            main {
                java.srcDirs = ['src/android/default/java/', 'src/common/java/']
                jni.srcDirs = [] //disable ndk support
                manifest.srcFile 'src/android/default/AndroidManifest.xml'
                resources.srcDirs = ['src/android/default']
                aidl.srcDirs = ['src/android/default']
                renderscript.srcDirs = ['src/android/default']
                res.srcDirs = ['src/android/default/res']
                assets.srcDirs = ['src/android/default/assets']
            }
            test {
                java.srcDirs = ['src/test/java']
            }
            androidTest {
                java.srcDirs = ['src/androidTest/java']
            }
        }
    }
}

尝试构建时收到以下错误消息:

Could not determine the dependencies of task ':app:checkKitkatReleaseLibraries'.
> Could not resolve all task dependencies for configuration ':app:kitkatReleaseReverseMetadataValues'.
   > Could not resolve project :app.
     Required by:
         project :app
      > The consumer was configured to find a usage of 'android-reverse-meta-data' of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'version' with value 'kitkat'. However we cannot choose between the following variants of project :app:
          - kitkatDebugAndroidTestCompile
          - kitkatDebugAndroidTestRuntime
          - kitkatDebugRuntime
          - kitkatDebugUnitTestCompile
          - kitkatDebugUnitTestRuntime
          - kitkatReleaseRuntime
          - kitkatReleaseUnitTestCompile
          - kitkatReleaseUnitTestRuntime
          - nougatDebugAndroidTestCompile
          - nougatDebugAndroidTestRuntime
          - nougatDebugRuntime
          - nougatDebugUnitTestCompile
          - nougatDebugUnitTestRuntime
          - nougatReleaseRuntime
          - nougatReleaseUnitTestCompile
          - nougatReleaseUnitTestRuntime
        All of them match the consumer attributes:
          - Variant 'kitkatDebugAndroidTestCompile' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'kitkatDebugAndroidTestRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'kitkatDebugRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'kitkatDebugUnitTestCompile' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'kitkatDebugUnitTestRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'kitkatReleaseRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'kitkatReleaseUnitTestCompile' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'kitkatReleaseUnitTestRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatDebugAndroidTestCompile' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatDebugAndroidTestRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatDebugRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatDebugUnitTestCompile' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatDebugUnitTestRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatReleaseRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatReleaseUnitTestCompile' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
          - Variant 'nougatReleaseUnitTestRuntime' capability myapp:app:unspecified:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'release')
                  - Doesn't say anything about its usage (required a usage of 'android-reverse-meta-data')
                  - Provides attribute 'org.jetbrains.kotlin.localToProject' with value 'local to :app' but the consumer didn't ask for it
                  - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
                  - Doesn't say anything about version (required 'kitkat')
        The following variants were also considered but didn't match the requested attributes:
          - Variant 'kitkatDebugApiElements' capability myapp:app:unspecified declares a component, as well as attribute 'version' with value 'kitkat':
              - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' and the consumer needed a usage of 'android-reverse-meta-data' of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release'
          - Variant 'kitkatDebugRuntimeElements' capability myapp:app:unspecified declares a component, as well as attribute 'version' with value 'kitkat':
              - Incompatible because this component declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' and the consumer needed a usage of 'android-reverse-meta-data' of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release'
          - Variant 'kitkatReleaseApiElements' capability myapp:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'version' with value 'kitkat':
              - Incompatible because this component declares an API of a component and the consumer needed a usage of 'android-reverse-meta-data' of a component
          - Variant 'kitkatReleaseRuntimeElements' capability myapp:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'version' with value 'kitkat':
              - Incompatible because this component declares a runtime of a component and the consumer needed a usage of 'android-reverse-meta-data' of a component
          - Variant 'nougatDebugApiElements' capability myapp:app:unspecified:
              - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'version' with value 'nougat' and the consumer needed a usage of 'android-reverse-meta-data' of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'version' with value 'kitkat'
          - Variant 'nougatDebugRuntimeElements' capability myapp:app:unspecified:
              - Incompatible because this component declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'version' with value 'nougat' and the consumer needed a usage of 'android-reverse-meta-data' of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'version' with value 'kitkat'
          - Variant 'nougatReleaseApiElements' capability myapp:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release':
              - Incompatible because this component declares an API of a component, as well as attribute 'version' with value 'nougat' and the consumer needed a usage of 'android-reverse-meta-data' of a component, as well as attribute 'version' with value 'kitkat'
          - Variant 'nougatReleaseRuntimeElements' capability myapp:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release':
              - Incompatible because this component declares a runtime of a component, as well as attribute 'version' with value 'nougat' and the consumer needed a usage of 'android-reverse-meta-data' of a component, as well as attribute 'version' with value 'kitkat'

在此之前,当我使用 gradle-tools v 3.x 时,它工作得非常好。

有什么想法、想法、提示、线索吗?

【问题讨论】:

  • 遇到了同样的问题,无法使用与使用者库具有不同变体的库依赖项。如果有人能解决或解决问题,那就太好了?
  • :app:checkKitkatReleaseLibraries 任务从何而来?从您的部分构建配置中不清楚(至少对我而言)。 minimal reproducible example 会很棒……

标签: android gradle android-gradle-plugin build.gradle


【解决方案1】:

好吧,为了使用不同的后端,我不会指定比调试和发布更多的构建类型。相反,我会使用其中一些技术:

  • 更多口味,
  • 自定义构建配置字段(文档here),
  • 结合多种产品风味(文档here)。

您可以使用BuildConfig 类在应用程序代码中访问构建类型、构建风格和自定义字段。

简单的方法

  • 构建类型:
    • debug
    • release
  • 口味:
    • dev
    • test
    • live

这会导致这些构建变体(您不必全部使用它们):

  • devDebug
  • devRelease
  • testDebug
  • testRelease
  • liveDebug
  • liveRelease

使用维度组合多种风格的方法

  • 风味维度:

    • backend
    • target
  • 构建类型:

    • debug
    • release
  • 口味:

  • target维度:

    • dev
    • test
    • live
  • backend维度:

    • production
    • test

这会导致这些构建变体(同样,您不必全部使用它们):

  • productionDevDebug
  • productionDevRelease
  • productionTestDebug
  • productionTestRelease
  • productionLiveDebug
  • productionLiveRelease
  • testDevDebug
  • testDevRelease
  • testTestDebug
  • testTestRelease
  • testLiveDebug
  • testLiveRelease

使用构建字段

在构建类型和构建风味声明中使用附加值,例如:

buildConfigField "boolean", "production_backend", "false"

或

buildConfigField "String", "backend", "\"production\""

关注此链接https://www.journaldev.com/21533/android-build-types-product-flavors

【讨论】:

  • 在我的特殊情况下无法理解如何使用您的建议...
【解决方案2】:

根据@Chriki 的评论,试图制定最小的可重现问题并突然找到了解决方案。魔术在声明中:

android {
    ...
    dynamicFeatures = [":app"]
}

每当我评论这一行时,一切正常。

附:这里应该是仇恨辐射来分级日志/调试消息,实际上没有那些日志/消息。最后我花了将近一个星期的时间试图找到解决方案,最终偶然发现。

如果@Chriki 会发布答案,我会奖励他。

【讨论】:

    【解决方案3】:

    您的问题似乎是您的主项目使用具有风味维度的库,而您的应用不知道要使用哪一个。您应该在 defaultConfig 块中使用 missingDimensionStrategy 来指定默认风格。例如:

    missingDimensionStrategy 'dimension', 'flavor1', 'flavor2'
    

    请查看this链接了解更多详情。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,但我正在使用多 gradle 文件项目,在每个项目上添加变体后它都可以正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        • 2015-05-25
        • 1970-01-01
        • 2021-10-08
        相关资源
        最近更新 更多