Unable to build feature modules in a multi-flavor

2020-04-06 01:40发布

问题:

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

    }

回答1:

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:

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.