android studio 3.0 Declare flavor dimensions

/ android / 没有评论 / 1002浏览

Question

今天给自己一个android project升级到android studio 3.0的时候报了下面了这个错误。

Error:All flavors must now belong to a named flavor dimension.
Learn more at 
https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

在android 2.3的时候项目构建都是成功的,可以正常运行,在android studio 3.0的时候就有问题,难道是3.0 gradle程序有什么新的特性?于是Google一下,找到了答案。

https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html

Answer

下面内容摘自文档网页,我觉得有的词翻译成中文怪怪的,就直接上原文了。
The plugin now requires that all flavors belong to a named flavor dimension—even if you intend to use only a single 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.

To resolve this error, you need to first declare one or more dimensions using the flavorDimensions property. After that, assign each flavor to one of the dimensions you declared, as shown in the sample below. Because the plugin automatically matches dependencies for you, you should name your flavor dimensions carefully. Doing so gives you more control over which code and resources from your local dependencies are matched with each version of your app.

// Specifies two flavor dimensions.  **要声明这个**
flavorDimensions "tier", "minApi"

productFlavors {
     free {
      // Assigns this product flavor to the "tier" flavor dimension. 
      // Specifying this property is optional if you are using only 
      // one dimension.
      dimension "tier"  //**每一个flavor要声明这个**
      ...
    }

    paid {
      dimension "tier"
      ...
    }

    minApi23 {
        dimension "minApi"
        ...
    }

    minApi18 {
        dimension "minApi"
        ...
    }
}