[英]How to optionally apply some plugins using Kotlin DSL and plugins block with gradle and android
I want to apply some plugins optionally to improve build time in development time.我想选择应用一些插件来改善开发时间的构建时间。
I mean I have this:我的意思是我有这个:
app/gradle.build.kts
: app/gradle.build.kts
:
plugins {
id("com.android.application")
id("com.google.firebase.crashlytics")
}
But I dont want to apply firebase crashlytics plugin all the time (and other plugins like perf monitor), but if I try to access the project
inside of the plugins
block I get:但我不想一直应用 firebase crashlytics 插件(以及其他插件,如性能监视器),但如果我尝试访问
plugins
块内的project
,我会得到:
plugins {
id("com.android.application")
if (!project.hasProperty("build_fast")) {
id("com.google.firebase.crashlytics")
}
}
I get:我得到:
'val Build_gradle.project: Project' can't be called in this context by implicit receiver. Use the explicit one if necessary
So my questions are:所以我的问题是:
I can access the System env variables with System.getenv("CI")
but replacing those values from android studio is a little bit hacky currently to me, someone can show a way to update those variables?我可以使用
System.getenv("CI")
访问 System env 变量,但是从 android 工作室中替换这些值目前对我来说有点麻烦,有人可以展示一种更新这些变量的方法吗?
How can I do it using project.hasPropperty("")
?如何使用
project.hasPropperty("")
来做到这一点?
You can't.你不能。 The plugins DSL is limited in what you can and can't do.
插件 DSL 受限于你能做什么和不能做什么。 This is documented in Limitations of the plugins DSL .
这记录在插件 DSL 的限制中。
To conditionally apply a plugin, you must use Project.apply(T)
:要有条件地应用插件,您必须使用
Project.apply(T)
:
plugins {
id("com.android.application")
id("com.google.firebase.crashlytics") apply false
}
The apply false
is necessary because we don't want to apply the plugin, but we want the types or classes that are available from that plugin so we can programmatically apply the plugin in a type safe manner. apply false
是必要的,因为我们不想应用插件,但我们想要该插件可用的类型或类,以便我们可以以编程方式以类型安全的方式应用插件。
import com.google.firebase.crashlytics.buildtools.gradle.CrashlyticsPlugin
plugins {
id("com.android.application")
id("com.google.firebase.crashlytics") apply false
}
apply<CrashlyticsPlugin>()
I can access the System env variables with
System.getenv("CI")
but replacing those values from android studio is a little bit hacky currently to me, someone can show a way to update those variables?我可以使用
System.getenv("CI")
访问 System env 变量,但是从 android 工作室中替换这些值目前对我来说有点麻烦,有人可以展示一种更新这些变量的方法吗?
You can't update the environment ( System.getenv()
), it is a unmodifiable map.您无法更新环境(
System.getenv()
),它是不可修改的 map。
How can I do it using
project.hasPropperty("")
我该如何使用
project.hasPropperty("")
Use that as a conditional which you have done:将其用作您已完成的条件:
import com.google.firebase.crashlytics.buildtools.gradle.CrashlyticsPlugin
plugins {
id("com.android.application")
id("com.google.firebase.crashlytics") apply false
}
if (!hasProperty("build_fast")) {
apply<CrashlyticsPlugin>()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.