有没有办法在Android Studio(build.gradle)中通过多种口味编译依赖项?
我有2个flavorGroups,每个2个变种。在4种可能的组合中,我希望能够依赖于lib,只要我是最新的和免费的。 latestCompile或freeCompile有效,但latestFreeCompile没有。这是我的build.gradle的相关部分:
android {
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
versionCode 15
versionName "1.9." + versionCode
}
flavorGroups 'sdk', 'cost'
productFlavors {
latest {
flavorGroup 'sdk'
minSdkVersion 8
}
sdk7 {
flavorGroup 'sdk'
minSdkVersion 7
versionName android.defaultConfig.versionName + ".sdk7"
}
free {
flavorGroup 'cost'
}
pro {
flavorGroup 'cost'
}
}
}
dependencies {
// this works:
freeCompile files('libs/StartAppInApp-2.2.1.jar')
// and I would like something like this:
latestFreeCompile 'com.google.android.gms:play-services:4.1.32' // minSdkVersion:8
}
如果我愿意的话:
latestCompile 'com.google.android.gms:play-services:4.1.32'
然后它也将被包含在latestPro中(不需要) 如果我使用:
freeCompile 'com.google.android.gms:play-services:4.1.32'
然后它也将包含在sdk7Free中(虽然它需要SDK 8)
答案 0 :(得分:1)
我遇到了同样的问题。我在build.gradle中使用了一些gradle代码解决了这个问题:
// global variables
ext {
buildType = ""
groupCost = ""
groupSdk = ""
}
def splitCamelCase(String word) {
def result = []
int nextStart = 0;
for (int i = 1; i < word.length(); i++) {
if(word.charAt(i).isUpperCase()) {
result.add(word.substring(nextStart, i));
nextStart = i;
}
}
result.add(word.substring(nextStart));
return result;
}
// start parameters
println "Start parametes: tasks = " + gradle.startParameter.getTaskNames()
gradle.startParameter.getTaskNames().each { task ->
// This line is needed to skip other projects' tasks
// You can safely remove it if you have only one project
if(!task.startsWith(':<your_application_name>:')) return;
def taskParts = splitCamelCase(task.split(":").last());
def groupCostPrefix = taskParts[taskParts.size() - 3];
def groupSdkPrefix = taskParts[taskParts.size() - 2];
def buildTypePrefix = taskParts[taskParts.size() - 1];
if("Debug".startsWith(buildTypePrefix)) {
buildType = 'debug';
}
else if("Release".startsWith(buildTypePrefix)) {
buildType = 'release';
}
else {
return; // do not process tasks that are not ending with proper build type.
}
if("Free".startsWith(groupCostPrefix)) {
groupCost = 'free';
}
else if("Pro".startsWith(groupCostPrefix)) {
groupCost = 'pro';
}
if("Sdk7".startsWith(groupSdkPrefix)) {
groupSdk = 'froyo';
}
else if("Latest".startsWith(groupSdkPrefix)) {
groupSdk = 'latest';
}
}
然后,您只需在“依赖关系”中添加以下代码即可。部分:
if(groupSdk == 'latest' && groupCost == 'free') {
compile 'com.google.android.gms:play-services:4.1.32'
}
答案 1 :(得分:1)
这里也有同样的问题,但Pawel的解决方案不起作用,因为gradle依赖有其他问题,它不仅开始构建选定的flavor / build类型,而且所有这些都需要更动态的解决方案。
还有一个引用来修复我上面提到的重建 - 所有错误,但我还没有尝试过。
并实施了此解决方案(根据问题跟踪器回复#60):
Map<String, Dependency> customDeps = new HashMap<String, Dependency>()
customDeps.put('flavor1GrpXflabor1GrpYDebugCompile', dependencies.project(path: ':lib', configuration: 'debug'))
customDeps.put('flavor1GrpXflavor1GrpYReleaseCompile', dependencies.project(path: ':lib', configuration: 'release'))
customDeps.put('flavor2GrpXflavor1GrpYDebugCompile', dependencies.project(path: ':other_lib', configuration: 'debug'))
customDeps.put('flavor2GrpXflavor1GrpYReleaseCompile', dependencies.project(path: ':other_lib', configuration: 'release'))
....
configurations.all() { config ->
Dependency d = customDeps.get(config.name)
if (d != null) {
config.dependencies.add(d)
}
}
答案 2 :(得分:1)
正如官方Android Developers website所述,如果您要声明产品风味和内部类型的特定组合的依赖项,则需要先声明该配置。
例如,free
风味和debug
类型:
configurations {
freeDebugImplementation {}
}
dependencies {
freeDebugImplementation 'com.google.firebase:firebase-ads:9.8.0'
}
请注意,如果您的应用具有多个口味维度,则必须创建与完整构建变体相对的配置(所有口味,然后是构建类型),如下所示:
flavorDimensions "money", "image"
productFlavors {
free { dimension "money" }
paid { dimension "money" }
picasso { dimension "image" }
glide { dimension "image" }
}
configurations {
freePicassoDebugImplementation {}
freeGlideDebugImplementation {}
}
dependencies {
freePicassoDebugImplementation 'com.google.firebase:firebase-ads:9.8.0'
freeGlideDebugImplementation 'com.google.firebase:firebase-ads:9.8.0'
}