szi*_*ani 38 android gradle newrelic android-studio android-productflavors
我有一个多味道,多构建类型的android项目,我想要集成NewRelic插件.但我必须只为其中一个客户应用它,因此只适用于一种产品风味.
NewRelic使用检测,如果我在那里应用插件,插件会生成其他风格的代码,这是我们不允许的.
所以我的问题是:如何使用apply plugin: somethinggradle文件中的命令仅应用于我的一种口味?
小智 36
使用此代码:
if (!getGradle().getStartParameter().getTaskRequests()
.toString().contains("Develop")){
apply plugin: 'com.google.gms.google-services'
}
Run Code Online (Sandbox Code Playgroud)
getGradle().getStartParameter().getTaskRequests().toString()返回类似于[DefaultTaskExecutionRequest{args=[:app:generateDevelopDebugSources],projectPath='null'}]注释中所述的内容Develop必须以大写开头.
Ser*_*nko 11
def fl在你的Flavors(和/或build)中初始化变量
productFlavors {
freeFlavour {
(...)
fl = "free"
}
paidFlavour {
(...)
fl = "paid"
}
}
Run Code Online (Sandbox Code Playgroud)使用if语句 -
if (fl == "free") {
apply plugin: something
}
尝试了不同的解决方案,但没有一个对我有用。这是我想出的,并且据我测试似乎可以正常工作:
build.gradle
productFlavors {
someFlavorWithGoogleGcm {
dimension "type"
applicationId "com.example.withGcm"
ext.useGoogleGcm = true
}
someFlavorWithoutGoogleGcm {
dimension "type"
applicationId "com.example.withoutGcm"
}
}
Run Code Online (Sandbox Code Playgroud)
在配置之外,在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'
}
}
Run Code Online (Sandbox Code Playgroud)
我只是apply plugin: 'com.google.gms.google-services'在应用级别 build.gradle 中的风味中使用了它,它工作得很好。
productFlavors {
..... your other flavors ....
yourFlv {
....
....
apply plugin: 'com.google.gms.google-services'
}
}
Run Code Online (Sandbox Code Playgroud)
不需要额外的步骤。
我找到了一个解决方案,但目前还不是最好的解决方案.所以我不确定,我最初想做的事情是可能的.gradle文件评估以及正确的flavor和build类型的选择处于gradle构建的不同阶段,所以我所做的是:
我从命令行使用构建参数.当该参数为真时,我应用插件,当它甚至不存在时,我也应用它(用于IDE构建).我使用Jenkins,所以我可以在构建作业中编写该参数.
build.gradle文件:
// 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")
}
}
Run Code Online (Sandbox Code Playgroud)
你必须通过这个运行gradle构建:
assembleYourApp -PcompileNewRelic=true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5125 次 |
| 最近记录: |