我想做一些类似的东西(伪代码):
if (BuildType == "release"){
apply plugin: 'testfairy'
} else if (BuildType == "debug"){
apply plugin: 'io.fabric'
}
这个想法是根据构建类型,应用(或不应用)插件。怎么办?
在 Gradle 4.6 中,以下工作:
if (getGradle().getStartParameter().getTaskRequests().toString().contains("Release")) {
apply plugin: 'testfairy'
} else if (getGradle().getStartParameter().getTaskRequests().toString().contains("Debug")) {
apply plugin: 'io.fabric'
}
这是我使用的解决方法。这个想法是引入一个 Env 变量,并且仅在某个特定的环境中应用插件。
if (System.getenv("PROJECT_ENV") == "Release") {
apply plugin: 'your plugin'
}
基于Gradle核心开发人员Stefan Oehme,他说:
插件不能只应用于"项目的一部分"。它们要么应用,要么不应用。这成为您问题的用例是什么?
所以,答案是这是不可能的。我已经暴露了我的用例,这成为一个问题,我会看看你好怎么说。
这是我的解决方案,它没有使应用程序崩溃。 当最终调用类并出现找不到类异常时,其他解决方案确实崩溃了。
def tasks = gradle.startParameter.taskNames[0] ?: ""
if (tasks.toLowerCase().contains("prod")) {
println "Firebase-Performance pluign applied..."
apply plugin: 'com.google.firebase.firebase-perf'
}
还记得 maven 个人资料吗?您可以使用从gradle-fury借来的这个片段做类似的事情
在构建文件中
if (project.hasProperty('profile') && project.profile.split(',').contains("ci")) {
//do something
}
然后在以下情况下运行它 gradlew -Pprofile=ci
这里有一个完整的例子 https://github.com/gradle-fury/gradle-fury/blob/develop/build.gradle
免责声明,我致力于格拉德尔-愤怒。 为了科学