Reputation: 676
I have 3 product flavors (flavor1, flavor2, flavor3) in my app. flavor1 and flavor2 share some of the dependencies related to ads.
Is there a way to bundle the ad related dependencies to a gradle dimension or configuration and add that to flavor1 and flavor2 without duplicating the compileFlavor1 and compileFlavor2 lines in my build.gradle?
This is part of my current gradle. This works. But, I'm wondering if there is a better way to do this? So, I don't have to repeat the ad dependencies for each flavor.
android {
productFlavors {
flavor1 {}
flavor2 {}
flavor3 {}
}
sourceSets {
main {}
flavor1.java.srcDir 'common/ads/java' <--- shared sourceSet
flavor2.java.srcDir 'common/ads/java'
flavor3.java.srcDir 'common/no_ads/java'
}
}
dependencies {
compile 'dependency1'
compile 'dependency2'
compileFlavor1 'dependency3' <----- Same list
compileFlavor1 'dependency4'
compileFlavor1 'dependency5'
compileFlavor2 'dependency3' <------ Same list
compileFlavor2 'dependency4'
compileFlavor2 'dependency5
}
Upvotes: 4
Views: 1957
Reputation: 3989
I couldn't find a neat way to do this (flavor inheritance is not an option for me), so I wrote a little utility for it.
Simply add these functions before your dependencies
block:
/** Adds [dependency] as a dependency for the flavor [flavor] */
dependencies.ext.flavorImplementation = { flavor, dependency ->
def cmd = "${flavor}Implementation"
dependencies.add(cmd, dependency)
}
/** Adds [dependency] as a dependency for every flavor in [flavors] */
dependencies.ext.flavorsImplementation = { flavors, dependency ->
flavors.each { dependencies.flavorImplementation(it, dependency) }
}
You use the utility like this:
dependencies {
...
def myFlavors = ["flavor1", "flavor2", "flavor3"]
flavorsImplementation(myFlavors, "com.example.test:test:1.0.0")
flavorsImplementation(myFlavors, project(':local'))
...
}
The key to this utility is gradle's dependencies.add
API, which takes 2 parameters:
implementation
, api
, flavor1Implementation
. This can be a string, which allows us to use string manipulation to dynamically create this value."com.example.test:test:1.0.0"
, project(':local')
.Using this API, you can add dependencies dynamically, with quite a lot of flexibility!
Upvotes: 0
Reputation: 5479
Solution for sharing dependencies between flavors (updated for implementation
which has now replaced compile
):
Since implementation
, flavor1Implementation
, flavor2Implementation
, etc. are all in fact themselves Configurations you can actually apply an inheritance relationship between these steps in the build process.
Therefore in this case you can specify your shared dependencies, and dependencies for flavor1
only:
dependencies {
implementation 'dependency1'
implementation 'dependency2'
flavor1Implementation 'dependency3'
flavor1Implementation 'dependency4'
flavor1Implementation 'dependency5'
}
...And then simply allow flavor2Implementation
to inherit from flavor1Implementation
:
configurations.flavor2Implementation.extendsFrom(flavor1Implementation)
This can also be used to define more complex relationships between flavors, and multiple inheritance is supported, e.g:
configurations {
flavor3Implementation.extendsFrom(
flavor1Implementation,
flavor2Implementation
)
}
(Finally, just a note that sharing code between flavors should be continued to be handled with sourceSets
as per the original question.)
Upvotes: 4
Reputation: 5575
This is what we did to share directories between flavors:
sourceSets {
main {
java.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
}
production {
java.srcDirs = ['src/main/java', 'src/shared/java']
res.srcDirs = ['src/main/res', 'src/shared/res']
assets.srcDirs = ['src/main/assets', 'src/shared/assets']
}
logger {
java.srcDirs = ['src/main/java', 'src/shared/java', 'src/mock/java']
res.srcDirs = ['src/main/res', 'src/shared/res']
assets.srcDirs = ['src/main/assets', 'src/shared/assets']
}
nowav {
java.srcDirs = ['src/main/java', 'src/nowav/java', 'src/mock/java']
res.srcDirs = ['src/main/res', 'src/nowav/res']
assets.srcDirs = ['src/main/assets', 'src/nowav/assets']
}
}
Upvotes: 2