Gradle:多维风味ApplicationId [英] Gradle: Multi-Dimension Flavor ApplicationId

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

问题描述

我有一个相对复杂的项目,每个应用程序需要两个风格维度。

  flavorDimensionsshape,color

productFlavors {

blue {
flavorDimensioncolor
}

red {
flavorDimensioncolor
}

green {
flavorDimensioncolor
}


square {
flavorDimensionshape
}

circle {
flavorDimensionshape
}

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



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



我真正想要做的是设置两个applicationids在色彩维度上,然后根据形状维度在构建时选择正确的维度。我已经尝试过定义变量,但没有取得任何成功,他们只是被最后一个变体覆盖。

解决方案

我从这里找到了一个解决方案: Gradle Android插件 - 添加自定义flavor属性?



如果您将类似这样的类添加到您的gradle文件中,您可以将自定义属性添加到productFlavor中

  class AppIdExtension {
String squareId
String circleId

AppIdExtension(String sId,String cId){
squareId = sId
circleId = cId
}

public void setSquareId(String id){
squareId = id
}

public String getSquareId(){
squareId
}

public void setCircleId(String id){
circleId = id
}

publi c String getCircleId(){
circleId
}

}

然后,通过在 android {} 部分的开始处添加以下代码,将此扩展添加到每种风格中。

  productFlavors.whenObjectAdded {flavor  - > 
flavor.extensions.create(shapeIds,AppIdExtension,,)
}

然后在您的产品风格中,您可以为每种形状类型设置值

 蓝色{
flavorDimensioncolor
platformIds.squareIdyourAppId
platformIds.circleIdyourAppId
}

然后,最后在 productFlavors {} 部分之后添加以下内容:

  android.variantFilter {variant  - > 
def applicationId =
def flavors = variant.getFlavors()

if(flavors [0] .name.equals(square)){
applicationId = flavors [1] .platformIds.squareId
} else if(flavours [0] .name.equals(circle)){
applicationId = flavors [1] .platformIds.circleId
}

variant.getDefaultConfig()。applicationId applicationId

}

这可能不是实现它的最优雅或有效的方式,但它对我来说是完美的。现在我可以将所有的ID添加到productFlavor部分,然后variantFilter根据第一种风格设置正确的applicationId。


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.

解决方案

I found a solution following from this example here: Gradle Android Plugin - add custom flavor attribute?

If you add a class like this to your gradle file you can add custom attributes to a productFlavor

class AppIdExtension {
    String squareId
    String circleId

    AppIdExtension(String sId, String cId){
        squareId = sId
        circleId = cId
    }

    public void setSquareId(String id){
        squareId = id
    }

    public String getSquareId(){
        squareId
    }

    public void setCircleId(String id){
        circleId = id
    }

    public String getCircleId(){
        circleId
    }

}

You then add this to extension to each flavor by adding the following code at the start of your android { } section

productFlavors.whenObjectAdded { flavor ->
    flavor.extensions.create("shapeIds", AppIdExtension, "", "")
}

Then inside your product flavor you can set the values for each shape type

blue {
        flavorDimension "color"
        platformIds.squareId "yourAppId"
        platformIds.circleId "yourAppId"
    }

Then finally after your productFlavors { } section you add the following:

android.variantFilter { variant ->
    def applicationId = ""
    def flavors = variant.getFlavors()

  if(flavors[0].name.equals("square")){
      applicationId = flavors[1].platformIds.squareId
   } else if(flavors[0].name.equals("circle")){
      applicationId = flavors[1].platformIds.circleId
   }

   variant.getDefaultConfig().applicationId applicationId

}

This may not be the most elegant or efficient way of achieving it, but it is working perfectly for me. Now I can add all of my Ids in the productFlavor section and then the variantFilter sets the correct applicationId depending on the first flavor.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆