Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to build feature modules in a multi-flavor app

I am using Gradle 4.4 with Gradle-Android plugin 3.1.1 on Android Studio 3.1.1.

I have 2 flavors 'a' and 'b' and I am unable to build my project due to the following error:

Cannot choose between the following configurations of project :app:
  - aDebugMetadataElements
  - bDebugMetadataElements
All of them match the consumer attributes:
  - Configuration 'aDebugMetadataElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
      - Found com.android.build.api.attributes.VariantAttr 'aDebug' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Metadata' and found compatible value 'Metadata'.
      - Found dim 'a' but wasn't required.
  - Configuration 'bDebugMetadataElements':
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
      - Found com.android.build.api.attributes.VariantAttr 'bDebug' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Metadata' and found compatible value 'Metadata'.
      - Found dim 'b' but wasn't required.

app build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "me.xyz.flavors"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug{
            testCoverageEnabled true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "dim"
    productFlavors{
        a{
            dimension "dim"
        }
        b{
            dimension "dim"
        }

    }


}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':base')
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

base-feature build.gradle:

apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion 27
    baseFeature true
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        aDebug {
            testCoverageEnabled true
        }
        bDebug {
            testCoverageEnabled true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    application project(':app')
    api 'com.android.support:appcompat-v7:27.1.1'
    api 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    api "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

I have tried the following solutions thus far with no luck:

  • AS 3.0 test-only module Cannot choose between the following configurations of project :abcMobile:

  • Single flavor module based on multi flavor library in Gradle

  • Android Studio 3.0 Error. Migrate dependency configurations for local modules

EDIT:

Based on the comments and one answer, I tried giving a different applicationIdSuffix to both flavors, but the problem persists:

productFlavors{
        a{
            applicationIdSuffix ".a"
            dimension "dim"
        }
        b{
            applicationIdSuffix ".b"
            dimension "dim"
        }

    }
like image 235
BadCode Avatar asked Apr 11 '18 23:04

BadCode


2 Answers

Thanks to user TWL who pointed me towards google samples for instant apps, with an example for flavors.

We need flavor declarations in all the feature modules, application module and instant-app module as well. Library modules can be skipped as of today with plugin version 3.1.1. In other words, have this section in all feature and installed/instant modules:

flavorDimensions "dim"
productFlavors{
    a{
        dimension "dim"
    }
    b{
        dimension "dim"
    }
}
like image 96
BadCode Avatar answered Sep 20 '22 15:09

BadCode


FWIW you don't have to have matching flavors in your modules. You can tell it to fallback to the default build types.

By using matchingFallbacks

buildTypes {
    aDebug {
        testCoverageEnabled true
        matchingFallbacks = ['debug']
    }
    bDebug {
        testCoverageEnabled true
        matchingFallbacks = ['debug']
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        // This one already looks for 'release' which is added by default, so no need for fallback
    }
}

https://developer.android.com/studio/build/dependencies#resolve_matching_errors

like image 37
Blundell Avatar answered Sep 23 '22 15:09

Blundell