Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 392
bmooij Avatar asked May 29 '15 09:05

bmooij


People also ask

How do I get the current flavor in Gradle?

There is no "current flavor". The Gradle build file is building an object model of the build process. It is not an interpreted script only being used once a build is underway and a "current flavor" is known.

How would you define different dependencies for different product flavors?

To define a flavor specific dependency you can use proCompile instead of compile in your dependency section. When you run gradle properties you get an overview of automatic created configurations.

When would you use product flavor in your build setup?

123 let's go Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.

What is a buildType in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.


1 Answers

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.

like image 93
Peter Avatar answered Sep 21 '22 06:09

Peter