All flavors must now belong to a named flavor dimension

这个其实是更新Gradle插件3.0.1之后的一个坑。

https://developer.android.com/studio/build/build-variants#flavor-dimensions

某些情况下,您可能希望组合多个产品风味中的配置。例如,您可能希望基于 API 级别为“完整”和“演示”产品风味创建不同的配置。为此,您可以通过适用于 Gradle 的 Android 插件创建产品风味组,称为风味维度。构建您的应用时,Gradle 会将您定义的每个风味维度中的产品风味配置与构建类型配置组合来创建最终构建变体。Gradle 不会组合属于相同风味维度的产品风味。提示:要根据 ABI 和屏幕密度创建不同版本的应用,您应配置 APK 拆分,而不是使用产品风味。

下面的代码示例使用 flavorDimensions 属性创建一个“模式”风味维度以组织“完整”和“演示”产品风味,以及一个“api”风味维度以基于 API 级别组织产品风味配置:

android {
  ...
  buildTypes {
    debug {...}
    release {...}
  }

  // Specifies the flavor dimensions you want to use. The order in which you
  // list each dimension determines its priority, from highest to lowest,
  // when Gradle merges variant sources and configurations. You must assign
  // each product flavor you configure to one of the flavor dimensions.
  flavorDimensions "api", "mode"

  productFlavors {
    demo {
      // Assigns this product flavor to the "mode" flavor dimension.
      dimension "mode"
      ...
    }

    full {
      dimension "mode"
      ...
    }

    // Configurations in the "api" product flavors override those in "mode"
    // flavors and the defaultConfig {} block. Gradle determines the priority
    // between flavor dimensions based on the order in which they appear next
    // to the flavorDimensions property above--the first dimension has a higher
    // priority than the second, and so on.
    minApi24 {
      dimension "api"
      minSdkVersion '24'
      // To ensure the target device receives the version of the app with
      // the highest compatible API level, assign version codes in increasing
      // value with API level. To learn more about assigning version codes to
      // support app updates and uploading to Google Play, read Multiple APK Support
      versionCode 30000 + android.defaultConfig.versionCode
      versionNameSuffix "-minApi24"
      ...
    }

    minApi23 {
      dimension "api"
      minSdkVersion '23'
      versionCode 20000  + android.defaultConfig.versionCode
      versionNameSuffix "-minApi23"
      ...
    }

    minApi21 {
      dimension "api"
      minSdkVersion '21'
      versionCode 10000  + android.defaultConfig.versionCode
      versionNameSuffix "-minApi21"
      ...
    }
  }
}
...

Gradle 创建的构建变体数量等于每个风味维度中的风味数量与您配置的构建类型数量的乘积。在 Gradle 为每个构建变体或对应 APK 命名时,属于较高优先级风味维度的产品风味首先显示,之后是较低优先级维度的产品风味,再之后是构建类型。以上面的构建配置为例,Gradle 可以使用以下命名方案创建总共 12 个构建变体:

构建变体: [minApi24, minApi23, minApi21][Demo, Full][Debug, Release]
对应 APK: app-[minApi24, minApi23, minApi21]-[demo, full]-[debug, release].apk
例如,
构建变体: minApi24DemoDebug
对应 APK: app-minApi24-demo-debug.apk

除了可以为各个产品风味和构建变体创建源集目录外,您也可以为每个产品风味组合创建源集目录。例如,您可以创建 Java 源并将其添加到 src/demoMinApi24/java/ 目录中,Gradle 仅会在构建组合了这两种产品风味的变体时使用这些源。与属于各个产品风味的源集相比,您为产品风味组合创建的源集拥有更高的优先级。要详细了解源集和 Gradle 如何合并源,请阅读有关如何创建源集的部分。

你可能感兴趣的:(All flavors must now belong to a named flavor dimension)