Gradle: Multi-Dimension Flavor ApplicationId

后端 未结 3 1581
名媛妹妹
名媛妹妹 2021-01-05 03:05

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:

flavorDim         


        
相关标签:
3条回答
  • 2021-01-05 03:09

    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)
            } ...
        }
    
    0 讨论(0)
  • 2021-01-05 03:19

    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.

    0 讨论(0)
  • 2021-01-05 03:22
    def appId = "my.custom.package"
    
    if (appId == "some.package") { 
         ..... Change conditions based on the flavors maybe defined more variables
    }
    
    defaultConfig {
            applicationId "${appId}"
           ...............
    }
    
    0 讨论(0)
提交回复
热议问题