我有一个多种口味,多种构建类型的Android项目,我想集成NewRelic插件。但是我必须仅将其应用于一个客户,因此仅适用于一种产品口味。
NewRelic使用仪器,并且如果我在其他口味中应用了该插件,该插件将在其他口味中生成代码,而我们不允许这样做。
所以我的问题是:如何在gradle文件中使用apply plugin: something
命令仅适用于我的其中一种口味?
我有一个多种口味,多种构建类型的Android项目,我想集成NewRelic插件。但是我必须仅将其应用于一个客户,因此仅适用于一种产品口味。
NewRelic使用仪器,并且如果我在其他口味中应用了该插件,该插件将在其他口味中生成代码,而我们不允许这样做。
所以我的问题是:如何在gradle文件中使用apply plugin: something
命令仅适用于我的其中一种口味?
使用这段代码:
if (!getGradle().getStartParameter().getTaskRequests()
.toString().contains("Develop")){
apply plugin: 'com.google.gms.google-services'
}
getGradle().getStartParameter().getTaskRequests().toString()
的返回值类似于 [DefaultTaskExecutionRequest{args=[:app:generateDevelopDebugSources],projectPath='null'}]
,因此根据注释所述,Develop
必须以大写字母开头。
尝试了不同的解决方案,但没有一个适用于我。这是我想出来的,并且在我测试的范围内似乎可以工作:
build.gradle
productFlavors {
someFlavorWithGoogleGcm {
dimension "type"
applicationId "com.example.withGcm"
ext.useGoogleGcm = true
}
someFlavorWithoutGoogleGcm {
dimension "type"
applicationId "com.example.withoutGcm"
}
}
在配置之外,build.gradle
文件内:
android.productFlavors.each { flavor ->
if (getGradle().getStartParameter().getTaskRequests().toString().toLowerCase().contains(flavor.name) && flavor.ext.useGoogleGcm) {
println("Building flavor with Google GCM [${flavor.name}] - applying plugin")
apply plugin: 'com.google.gms.google-services'
}
}
if (getGradle().getStartParameter().getTaskNames().stream().filter { it.toLowerCase().contains(flavor.name) }.count() > 0 && flavor.ext.useGoogleGcm)
。 - Gabriel GavaCannot get property 'shouldMinify' on extra properties extension as it does not exist
。因此,我需要将ext.shouldMinify
属性添加到所有的flavors中(并设置相应的值)。 - Sasha Davydenko'com.google.gms.google-services'
插件在实际构建生成 R.string.default_web_client_id
之前就已经运行了。 - Suau我只是在应用级别的build.gradle文件中的flavor中使用了apply plugin: 'com.google.gms.google-services'
,它完全正常工作。
productFlavors {
..... your other flavors ....
yourFlv {
....
....
apply plugin: 'com.google.gms.google-services'
}
}
无需额外步骤。
def fl
在您的Flavours(和/或构建)中初始化变量
productFlavors {
freeFlavour {
(...)
fl = "free"
}
paidFlavour {
(...)
fl = "paid"
}
}
使用 if 语句 -
if (fl == "free") {
apply plugin: something
}
// First we have to attach a task to the project, which will be running first
gradle.projectsEvaluated {
preBuild.dependsOn(applyNewRelicByProperty)
}
// Then check on the parameter, which comes from the command line
task applyNewRelicByProperty {
if(!project.hasProperty('compileNewRelic')) {
// NewRelic on: 'compileNewRelic' property not set, so running from IDE.
apply plugin: 'newrelic'
} else if(project.hasProperty('compileNewRelic') && project.getProperties().get('compileNewRelic').equals('true')) {
// NewRelic on: 'compileNewRelic' property is set and it is 'true'.
apply plugin: 'newrelic'
} else {
// NewRelic off
println("No NewRelic")
}
}
你需要通过以下方式运行gradle构建:
assembleYourApp -PcompileNewRelic=true
build.gradle
解析器在评估项目之前查找apply
语句,因此使if
检查无效。 - Sebastiano升级到Gradle 4.2后,Tarps的方法不再适用,所以我最终将其与Erik的答案结合起来。
在您的build.gradle
中为每个风味设置ext.useGoogleGcm
:
productFlavors {
a {
...
ext.useGoogleGcm = true
}
b {
...
ext.useGoogleGcm = false
}
}
然后在文件底部:
apply plugin: 'com.google.gms.google-services'
afterEvaluate {
android.productFlavors.each { flavor ->
tasks.matching {
it.name.contains('GoogleServices') && it.name.contains(flavor.name.capitalize())
}*.enabled = flavor.ext.useGoogleGcm
}
}
assembleRelease
任务输出中,您应该看到名称中带有“GoogleServices”的任务已运行,对于值为true的任务执行,对于值为false的任务跳过执行。如果您的构建出现关于toCapitalize
的问题,您可以对it.name
和flavor.name
都使用toLowerCase
。apply plugin: 'com.google.gms.google-services'
afterEvaluate {
tasks.matching { it.name.contains("GoogleServices") && !it.name.contains(yourFlavorName) }*.enabled = false
}
其中,yourFlavorName
是一个大写字符串,包含必须应用插件的您的口味的名称;所有其他风味都不使用该插件。
请注意,插件仍然适用于其他风味;此解决方案仅禁用它们的*GoogleServices*
任务。这假定 Google Services Gradle 插件仅通过添加包含子字符串"GoogleServices"
的任务来应用更改,这在未来可能不起作用。
要检查这是否有效,您可以检查依赖项,例如:
./gradlew app:dependencyInsight --configuration yourFlavorNameDebugCompileClasspath --dependency google | grep -i services
这应该显示一些yourFlavorName
的依赖项,但不是其他口味的依赖项:
./gradlew app:dependencyInsight --configuration otherFlavorNameDebugCompileClasspath --dependency google | grep -i services
./gradlew assembleDe
时,getTaskRequests
中的值也是assembleDe
- 而我预期它已经扩展为assembleDebug
。 - Fabio./gradlew asF
而不是./gradlew assembleFlavor
,就会失败,因为这个答案提出的解决方案中,任务请求字符串包含"asF"
而不是assembleFlavor
。当尝试匹配"Flavor"
时,这是一个问题! - Erik