Android Studio Plugin 3.0.0 Gradle — flavorDimensions()

最近把Android Stuido 升级到了3.2.1 遇到了很多问题,其中 flavorDimensions 就是其中众多问题中的一个,看来得花些时间学习学习这些新技术了,再好的的脑子也不如一个烂笔头,故,做一下笔记记录一下。

文档:

  • Andro
    id Plugin DSL Reference 官方文档
  • Android Developer 开发文档
  • 千里逐梦 CSDN博客 Android Gradle(3)— FlavorDimensions,构建变体 写得很详细,可以参考一下。

解释

flavorDimensions() 是在Android plugin 3.0.0及更高版本中新增加方法,主要用来定义产品维度。官方Andro
id Plugin DSL Reference 定义如下:

void flavorDimensions(String... dimensions)

Specifies the names of product flavor dimensions for this project.

When configuring product flavors with Android plugin 3.0.0 and higher, you must specify at least one flavor dimension, using the flavorDimensions property, and then assign each flavor to a dimension. Otherwise, you will get the following build error:

Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.

By default, when you specify only one dimension, all flavors you configure automatically belong to that dimension. If you specify more than one dimension, you need to manually assign each flavor to a dimension, as shown in the sample below.

翻译:指定此项目的产品风味维度的名称。 在使用Android plugin 3.0.0或更高版本配置产品风味时,必须使用flavorDimensions属性指定至少一个风味维度,然后将每个风味分配给一个维度。否则,您将得到以下构建错误:

Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.

默认情况下,当您只指定一个维度时,您配置的所有类型都自动属于该维度。如果您指定了一个以上的维度,那么您需要手动将每种口味分配给一个维度(也就是每一个渠道里都要指定一个dimension 定义的维度),如下面的示例所示。

android {
    ...
    // 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', 'version'

    productFlavors {
      demo {
        // Assigns this product flavor to the 'version' flavor dimension.
        dimension 'version'
        ...
    }

      full {
        dimension 'version'
        ...
      }

      minApi24 {
        // Assigns this flavor to the 'api' dimension.
        dimension 'api'
        minSdkVersion '24'
        versionNameSuffix "-minApi24"
        ...
      }

      minApi21 {
        dimension "api"
        minSdkVersion '21'
        versionNameSuffix "-minApi21"
        ...
      }
   }
}

你可能感兴趣的:(Android Studio Plugin 3.0.0 Gradle — flavorDimensions())