解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com

这个问题是Android studio升级到3.0之后,运行的时候会提示gradle要升级到3.5版本才能编译。于是我把我的gradle升级到了
 gradle-4.1-milestone-1 版本,是2017年7月份最新版本了。
于是我把主程序的build.gradle中的gradle版本改成了这个,具体指定哪个版本我也不知道,于是就写了个3.0+
 dependencies {
        classpath 'com.android.tools.build:gradle: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 
这个一个错误,意思是:所有的flavors都必须属于同一个风格。
=。=懵逼
去翻墙看了它提供的地址才知道:
 Plugin 3.0.0 includes a new dependency mechanism that automatically matches variants when consuming a library. This means an app's debug variant automatically consumes a library's debug variant, and so on. It also works when using flavors—an app's redDebug variant will consume a library's redDebug variant. To make this work, 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, assign each flavor to a named dimension, as shown in the sample below. Because dependency matching is now taken care of by the plugin, you should name your flavor dimensions carefully. For example, if all your app and library modules use the foo dimension, you'll have less control over which flavors are matched by the plugin. 
// Specifies a flavor dimension.
flavorDimensions "color"

productFlavors {
     red {
      // Assigns this product flavor to the 'color' flavor dimension.
      // This step is optional if you are using only one dimension.
      dimension "color"
      ...
    }

    blue {
      dimension "color"
      ...
    }
}

大致是说,Plugin 3.0.0之后有一种自动匹配消耗库的机制,便于debug variant 自动消耗一个库,然后就是必须要所有的flavor 都属于同一个维度。
为了避免flavor 不同产生误差的问题,应该在所有的库模块都使用同一个foo尺寸。
= 。=还是懵逼。说一堆依然不是很理解。
但是我们从中已经知道解决方案了:
在主app的build.gradle里面的
 defaultConfig {
 targetSdkVersion:***
minSdkVersion :***
versionCode:***
 versionName :***
//版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions "versionCode"
}

就直接解决这个问题。然后app 就可以happy的运行起来了。

  • 109
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 59
    评论
Android Gradle插件3.0.0版本开始,所有的flavor(即产品风味)必须属于一个命名的flavor dimension(即产品风味维度)。 在Android开发中,通常会使用多种不同的flavor来构建不同版本的应用,例如免费版和付费版,或者不同的地区特定版本等。在过去,我们可以直接通过在build.gradle文件中声明各种flavor来创建不同版本的应用,这些flavor之间没有固定的组织结构。 然而,为了更好地组织和管理多个flavor,从Androd Gradle插件3.0.0版本开始,引入了flavor dimensions的概念。flavor dimensions可以看作是一个属性集合,在这个集合中,可以包含多个相关的flavor。每个flavor dimension使用一个命名标识符表示,例如"version"或"region"。 要创建一个属于flavor dimensions的flavor,需要在build.gradle文件的android部分中指定各个flavordimension属性。例如: ``` flavorDimensions "version" productFlavors { free { dimension "version" // ... } paid { dimension "version" // ... } } ``` 在上述例子中,通过设置dimension为"version",我们将free和paid两个flavor分别归属于version这个flavor dimension下。 引入flavor dimensions的主要目的是为了更好地管理和组织多个flavor,可以更容易地定义和配置各种版本的应用。通过统一的flavor dimensions,可以避免各个flavor之间的混乱和冲突,提高项目的可维护性和可扩展性。 总结而言,所有的flavor现在都必须属于一个命名的flavor dimension,这样可以更好地组织和管理应用的不同版本,提高开发的效率和项目的可维护性。
评论 59
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值