bmooij
bmooij

Reputation: 142

Gradle flavor based on other flavor

Is it possible that a flavor is based on a other flavor?

For example build.gradle:

productFlavors {
    flavor1 {
        flavorBase "main"
    }
    flavor2 {
        flavorBase "main"
    }
    flavor3 {
        flavorBase "main"
    }
    flavor4 {
        flavorBase "flavor3"
    }
    flavor5 {
        flavorBase "flavor3"
    }
}

enter image description here

Upvotes: 5

Views: 441

Answers (2)

Peter
Peter

Reputation: 186

I ran into the same issue and needed something like a base flavor and two inherited flavors from that.

The best solution I ended up with after a few hours of trying out a lot of things was:

  1. Create your additional flavors (flavor 4 and 5) same as you did for 1 and 2.
  2. Create folders for those and create a base folder for common code/ressources (e.g. named flavor 3; the same way as you would do for your other flavors).
  3. Now define additional source sets for your child flavors

    flavor4 {
        res.srcDirs = ['src/flavor3/res', 'src/flavor4/res']
        java.srcDirs = ['src/flavor3/java', 'src/flavor4/java']
        resources.srcDirs = ['src/flavor3/java', 'src/flavor4/java']
    }
    flavor5 {
        res.srcDirs = ['src/flavor3/res', 'src/flavor5/res']
        java.srcDirs = ['src/flavor3/java', 'src/flavor5/java']
        resources.srcDirs = ['src/flavor3/java', 'src/flavor5/java']
    }
    

So in flavor3 (which is in that sense not a flavor but I keep the naming as it was) you can define common code and resources. The only downside is, that its not possible do have the same class in the base folder and in the child folders.

Upvotes: 4

Médéric
Médéric

Reputation: 948

You could use flavor dimensions to group features. For example:

flavorDimensions "color", "function", "data"
productFlavors {

    color1 {
        flavorDimension "color"
        // Something
    }
    color2 {
        flavorDimension "color"
        // Something else
    }

    functions1 {
        flavorDimension "function"
        // Something
    }  
    functions2 {
        flavorDimension "function"
        // Something else
    }

    data1 {
        flavorDimension "data"
        // Something
    }  
    data2 {
        flavorDimension "data"
        // Something else
    }    
}

Then you can factorize your code using these dimension like that : Color1Function2Data1 or Color2Function1Data1...

Upvotes: 1

Related Questions