无法在具有多个风格的应用程序中构建功能模块

8

我正在使用Android Studio 3.1.1,Gradle版本为4.4,Gradle-Android插件版本为3.1.1。

我的项目中有两个渠道,分别为'a'和'b',但是由于以下错误,我无法构建项目:

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.

应用构建文件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'
}

基础特性 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"
}

到目前为止,我已经尝试了以下解决方案,但都没有成功:

编辑

根据评论和一个答案,我尝试为两个味道提供不同的applicationIdSuffix,但问题仍然存在:

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

    }

我认为你的两种口味没有区别,这就是为什么会出现错误的原因。如果没有区别,那么就没有必要定义它们。请在两种口味之间做出一些区别。 - UserSharma
仍然无法工作。请查看我上面的编辑。我有不同的Java类来处理两种不同的情况。 - BadCode
1
你是否将你的代码与 https://github.com/googlesamples/android-instant-apps/tree/master/flavors 上的 Google 示例进行比较? - TWL
@TWL:非常感谢!我完全忘记了口味样本。现在我明白问题所在了,我会把它作为答案发布。 - BadCode
3个回答

4
感谢用户TWL指向了我google samples for instant apps, with an example for flavors
我们需要在所有的功能模块、应用程序模块和即时应用程序模块中声明口味。截至今天,库模块可以跳过,在插件版本3.1.1中。换句话说,在所有功能和已安装/即时模块中都要有这个部分:
flavorDimensions "dim"
productFlavors{
    a{
        dimension "dim"
    }
    b{
        dimension "dim"
    }
}

2

顺带一提,你的模块之间不需要拥有匹配的构建类型。你可以使用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

看起来你没有指定ab包的名称差异。下面是我在API 25和com.android.tools.build:gradle:2.3.1中实现flavor功能的可行方法,在AS 2.3.3app级别的build.gradle中:

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 {
    ...
}

看一下差异

applicationIdSuffix ".nosync"
versionNameSuffix '-nosync'

nosyncsync版本之间。它将更改nosyncapk的包名称。


请查看我的编辑。这个也不起作用。不确定是因为Gradle插件3.1的原因,但无论如何我都不能回到2.3,否则其他代码可能会出问题。 - BadCode
我现在不使用AS 3.x,因为它需要gradle版本,而我正在AS 2.3.3下使用一些gradle功能,这些功能在AS 3.x中没有机会。也许你的情况也是如此。这是一个不支持的特性的例子:https://dev59.com/CFUL5IYBdhLWcg3w-cLr?rq=1 - isabsent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接