Have a product flavor be a child of another

后端 未结 3 1232
灰色年华
灰色年华 2021-02-02 15:49

I\'m setting up the product flavors in my app and have run into one problem. Two of my product flavors are very similar to each other and only differ by a few resources, let\'s

相关标签:
3条回答
  • 2021-02-02 16:02

    I think you won't be able to do this in the android object directly, but you can use a configuration function to set common properties on a flavor. Put this after the android object:

    void configureWhiteLabelFlavor(flavor) {
        flavor.useJack = true
    }
    
    configureWhiteLabelFlavor(android.productFlavors.whiteLabelA)
    

    If needed, you can implement deep inheritance by calling other functions from this function.

    0 讨论(0)
  • 2021-02-02 16:11

    I found two solutions:

    1. Use flavor dimensions.

    android {
      ...
      flavorDimensions 'palette', 'color'
    
      productFlavors {
        blackAndWhite {
          dimension 'palette'
        }
        redAndWhite {
          dimension 'palette'
        }
        red {
          dimension 'color'
        }
        white {
          dimension 'color'
        }
      }
      variantFilter { variant ->
        def first = variant.getFlavors().get(0).name
        def second = variant.getFlavors().get(1).name
        if(!(first.equals('blackAndWhite') && second.equals('white')) ||
           !(first.equals('redAndWhite') && (second.equals('white') || second.equals('red')))) {
           variant.setIgnore(true);
        }
      }
      ...
    }
    

    Because of variantFilter we have 3 combinations instead of 4:

    blackAndWhite => white
    
    redAndWhite   => white
                  => red
    

    You can consider that as red extending redAndWhite and white extending blackAndWhite or redAndWhite.

    This answer and variant filter documentation were helpful.

    2. Edit source set.

    productFlavors{
        flavor1 {
        }
        flavor2 {
        }
    }
    
    sourceSets {
        flavor2 {
            java.srcDirs = sourceSets.flavor1.java.srcDirs
            res.srcDirs = sourceSets.flavor1.res.srcDirs
            resources.srcDirs = sourceSets.flavor1.resources.srcDirs
            aidl.srcDirs = sourceSets.flavor1.aidl.srcDirs
            renderscript.srcDirs = sourceSets.flavor1.renderscript.srcDirs
            assets.srcDirs = sourceSets.flavor1.assets.srcDirs
        }
    }
    

    Code example was shamelessly copied from this blog post. Big thanks to its author.

    0 讨论(0)
  • 2021-02-02 16:23

    I was looking for a similar thing in gradle and found Multi-flavor variants. I have an app that should have versions A and B, and each version has dev and pro environments, so I ended up with this in my gradle:

    flavorDimensions 'app', 'environment'
    
    productFlavors {
        versionA {
            flavorDimension 'app'
        }
        versionB {
            flavorDimension 'app'
        }
        pre {
            flavorDimension 'environment'
        }
        pro {
            flavorDimension 'environment'
        }
    }
    

    And in my build variants I have versionAPreDebug, versionAPreRelease, versionBPreDebug, versionBPreRelease, etc. I think what you need is something like that.

    0 讨论(0)
提交回复
热议问题