8

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:

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"
        }

    }
4
  • I think there is no difference in between your two flavors that why it is giving error if there is no difference then there is no need to define them. Please make some difference between two flavors.
    – UserSharma
    Apr 12, 2018 at 5:40
  • Still not working. Please see my edit above. I have different java classes for both flavors.
    – BadCode
    Apr 12, 2018 at 8:09
  • 1
    Have you compared yours against the google samples @ github.com/googlesamples/android-instant-apps/tree/master/…
    – TWL
    Apr 13, 2018 at 16:59
  • @TWL: Thanks so much! I had totally forgotten the flavor samples. I see the issue now; posting as an answer.
    – BadCode
    Apr 14, 2018 at 5:38

3 Answers 3

4

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"
    }
}
2

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

0

It seems you didn't specify difference in the names of a and b packages. Below is my working implementation of flavor feature for API 25 and com.android.tools.build:gradle:2.3.1 in the app level build.gradle of AS 2.3.3:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.00"
    }

    signingConfigs {
        config {
            storeFile file(STORE_PATH)
            keyAlias KEY_ALIAS
            keyPassword KEY_PASSWORD
            storePassword KEYSTORE_PASSWORD
        }
    }

    productFlavors {
        nosync {
            applicationIdSuffix ".nosync"
            versionNameSuffix '-nosync'
            signingConfig signingConfigs.config
        }
        sync {
            signingConfig signingConfigs.config
        }
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
        }
        debug {
            debuggable true
            signingConfig signingConfigs.config
        }
    }

dependencies {
    ...
}

Take a look on difference

applicationIdSuffix ".nosync"
versionNameSuffix '-nosync'

between nosync and sync flavors. It will change the package name of nosync apk.

2
  • Please see my edit. This doesn't work either. Not sure if its because of gradle plugin 3.1, but anyway I cant go back to 2.3 else other code may break.
    – BadCode
    Apr 12, 2018 at 8:11
  • I do not use AS 3.x now because it demands gradle version which has no opportunity for some gradle features I am using under AS 2.3.3. Probably your case is something like that. This is an example of unsupported feauture stackoverflow.com/questions/49530142/…
    – isabsent
    Apr 12, 2018 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.