Android buildTypes vs. productFlavors error

Viewed 12772

Decided to switch to from Android gradle plugin 3.5.x to gradle plugin 4.10 with Gradle 6.5 and stuck with mistake.

Here's my 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']
            }
        }
    }
}

Trying to build I receive following error messages:

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'

Before that, when I was using gradle-tools v 3.x - it was working really fine.

Any thoughts, ideas, hints, clues?

4 Answers

It seems that issue you have is that your main project uses your library that has flavor dimensions, and your app doesn't know which one to use. You should Use missingDimensionStrategy in the defaultConfig block to specify the default flavor. For example :

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

Please check this link for more details.

Following to @Chriki's comment tried to formulate minimal reproducible question and suddenly found a solution. Magic was in statement:

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

Whenever I have commented this line, everything works.

P.S. Here are should be hateness radiation to gradle logs/debug messages, actually absense of those logging/messaging. In the end I spent nearly a week trying to find a solution and in the end found by accident.

If @Chriki would post answer I will reward bounty to him.

This error happens in the following case. You have these modules and dependencies:

                  app (free/paid)
                   |
featureA       featureB          featureC
   |               |                |
       \                      /
             libraryX (free/paid)

app module has 2 flavors: free, paid. None of feature modules have those 2 flavors but libraryX has it! Now when feature modules depend on libraryX, they must explicitly tell gradle if they want freeLibraryXDebug or paidLibraryXDebug. But feature modules can't say freeImplementation(project(":libraryX")) or paidImplementation(project(":libraryX")) because they don't have those flavors themselves.

There are two ways to solve these:

  1. feature modules always choose one specific flavor, using missingDimensionStrategy in defaultConfig as described here. Problem with this approach is feature modules always depend on one specific flavor (free/paid) no matter what app module flavor is selected (from AndroidStudio "Build Variants" tab on left or from terminal command)

  2. feature modules also declare 2 flavors (free, paid), then use implementation(project(":libraryX")). Selected flavor for app module will cascade down to feature modules and also to libraryX module, everything automatically. Problem with this is boilerplate, if you have tens of feature modules, they all need to declare 2 flavors in gradle file. To eliminate bilerplate and code duplication, you can extract productFlavors declaration to separate gradle file and apply it as plugin in every feature module. Example codes for gradle kotlin dsl and discussion here.

I couldn't find a way to choose libraryX's flavor by looking at app module's selected flavor, without introducing flavors to feature modules in the middle.

I had the same issue but i'm working with multi gradle files project , after adding the variant on each one it worked fine.

Related