Search code examples
androidgradleandroid-productflavors

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


Solution

  • 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.