本文介绍了Gradle:多维风味 ApplicationId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对复杂的项目,每个应用都需要两个风格维度.我在下面的例子中更简单地重写了它:

flavorDimensions形状"、颜色"产品风味{蓝色 {风味维度颜色"}红色的 {风味维度颜色"}绿色 {风味维度颜色"}正方形 {风味维度形状"}圆圈 {风味维度形状"}

我希望能够为每个变体设置不同的 applicationId,例如:squareblue 将具有与 circleblue 不同的 applicationId.我无法在颜色维度中设置 applicationId,因为它对于每个形状都是相同的.在上面的示例中,我需要有 6 个不同的 applicationId.这些 ID 也不遵循任何模式,它们可以是任何东西.

我在这里看到了答案:如何使用 flavorDimensions 为每个风味组合设置不同的 applicationId? 但这意味着我需要手动设置它,这对我的项目来说是不可行的,因为变体的数量(1000s).>

我真正想做的是在颜色维度上设置两个 applicationid,然后在构建时根据形状维度选择正确的一个.我试过定义变量,但没有成功,它们只是被最后一个变体覆盖.

解决方案

Gradle 有一个 extras 属性 内置,因此您可以在不定义类的情况下执行此操作.

看起来像这样,可能有一个或两个错字:

productFlavors {蓝色 {风味维度颜色"ext.squareId = "yourAppId"ext.circleId = "yourAppId"}android.applicationVariants.all { 变体 ->def 口味 = variant.getFlavors()if (flavors[0].name.equals("square")){variant.mergedFlavor.setApplicationId(flavors[1].ext.squareId)} ...}

I have a relatively complicated project that requires two flavor dimensions for each app. I've rewritten it much more simply in the example below:

flavorDimensions "shape", "color"

productFlavors {

     blue {
         flavorDimension "color"
     }

     red {
         flavorDimension "color"
     }

     green {
         flavorDimension "color"
     }


     square {
         flavorDimension "shape"
     }

     circle {
         flavorDimension "shape"
     }

I want to be able to set a different applicationId for each variant, eg: squareblue would have a different applicationId to circleblue. I can't set the applicationId in the color dimension because it would be the same for each shape. I would need to have 6 different applicationIds in the above example. These Ids also don't follow any pattern, they could be anything.

I've seen the answer here: How to set different applicationId for each flavor combination using flavorDimensions? but that would mean I need to set it up manually, which isn't feasible for my project, due to the number of variants (1000s).

What I really want to do is set two applicationids on the color dimension, then it picks the correct one, depending on the shape dimension, when it's built. I've tried defining variables but haven't had any success with that, they just get overwritten by the last variant.

解决方案

Gradle has an extras property built in, so you could do this without defining a class.

Would look something like this, might have made a typo or two:

productFlavors {
    blue {
        flavorDimension "color"
        ext.squareId = "yourAppId"
        ext.circleId = "yourAppId"
    }

    android.applicationVariants.all { variant ->
        def flavors = variant.getFlavors()
        if (flavors[0].name.equals("square")){
            variant.mergedFlavor.setApplicationId(flavors[1].ext.squareId)
        } ...
    }

这篇关于Gradle:多维风味 ApplicationId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 18:29