【问题标题】:Unable to build feature modules in a multi-flavor app无法在多风格应用程序中构建功能模块
【发布时间】:2018-04-11 23:36:32
【问题描述】:

我在 Android Studio 3.1.1 上使用 Gradle 4.4 和 Gradle-Android 插件 3.1.1。

我有 2 种口味“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.

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

基础功能 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"
}

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

编辑

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

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

    }

【问题讨论】:

  • 我认为你的两种口味之间没有区别,为什么如果没有区别就会给出错误,那么就不需要定义它们。请区分两种口味。
  • 还是不行。请参阅我上面的编辑。对于这两种风格,我都有不同的 java 类。
  • 你有没有将你的与谷歌样本进行比较@github.com/googlesamples/android-instant-apps/tree/master/…
  • @TWL:非常感谢!我完全忘记了风味样品。我现在看到了这个问题;发布作为答案。

标签: android android-gradle-plugin build.gradle android-instant-apps


【解决方案1】:

感谢用户TWL 将我指向google samples for instant apps, with an example for flavors

我们还需要在所有功能模块、应用程序模块和即时应用程序模块中声明风味。从今天开始,使用插件版本 3.1.1 可以跳过库模块。换句话说,在所有功能和已安装/即时模块中都有此部分:

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

【讨论】:

    【解决方案2】:

    FWIW 你不必在你的模块中有匹配的风格。您可以告诉它回退到默认构建类型。

    通过使用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

    【讨论】:

      【解决方案3】:

      您似乎没有指定 ab 包的名称的区别。下面是我在AS 2.3.3app 级别build.gradle 中为API 25 和com.android.tools.build:gradle:2.3.1 实现flavor 功能的工作

      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 plugin 3.1,但无论如何我不能回到 2.3 否则其他代码可能会中断。
      • 我现在不使用 AS 3.x,因为它需要 gradle 版本,而我在 AS 2.3.3 下使用的某些 gradle 功能没有机会。可能你的情况是这样的。这是一个不支持的功能示例stackoverflow.com/questions/49530142/…
      猜你喜欢
      • 2018-01-06
      • 2019-09-03
      • 2011-11-19
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      相关资源
      最近更新 更多