Android gradle:在产品口味之间共享依赖项 [英] Android gradle: Sharing dependencies between product flavors
问题描述
我的应用程序中有3种产品口味(flavor1,flavor2,flavor3)。 flavour1和flavour2共享一些与广告相关的依赖项。
I have 3 product flavors (flavor1, flavor2, flavor3) in my app. flavor1 and flavor2 share some of the dependencies related to ads.
是否可以将与广告相关的依赖项捆绑到gradle维度或配置中,并将其添加到flavor1和flavor2中,而无需在build.gradle中复制compileFlavor1和compileFlavor2行?
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
}
推荐答案
共享风味之间的依赖关系的解决方案(已针对实现
更新,没有w替换了 compile
):
Solution for sharing dependencies between flavors (updated for implementation
which has now replaced compile
):
由于实现
, flavor1Implementation
, flavor2Implementation
等实际上都是配置,您实际上可以在构建过程中的这些步骤之间应用继承关系。
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.
因此,在这种情况下,您可以指定共享的依存关系,以及仅对 flavor1
的依存关系:
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'
}
...然后简单地允许 flavor2Implementation
从 flavor1Implementation
继承:
...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
)
}
(最后,请注意,共享按照原始问题,应继续使用 sourceSets
处理风味之间的 code 。)
(Finally, just a note that sharing code between flavors should be continued to be handled with sourceSets
as per the original question.)
这篇关于Android gradle:在产品口味之间共享依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!