简体   繁体   English

如何使用 Kotlin DSL 和插件块使用 gradle 和 android 选择性地应用一些插件

[英]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:所以我的问题是:

  1. 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 工作室中替换这些值目前对我来说有点麻烦,有人可以展示一种更新这些变量的方法吗?

  2. 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.

相关问题 应用插件的不同方式? (摇篮 Kotlin DSL) - Different ways to apply plugins ? (Gradle Kotlin DSL) 使用插件 DSL 添加 Kotlin 插件 - Adding Kotlin plugins using plugins DSL 使用插件 dsl 语法应用 hibernate-gradle-plugin? - Apply hibernate-gradle-plugin using plugins dsl syntax? 在插件块中引用 kotlin gradle 脚本变量 - Referencing kotlin gradle script varaibles in plugins block 如何使用 Kotlin 将导入的插件合并到 Gradle 中的自定义插件 - How to consolidate imported plugins to custom plugin in Gradle using Kotlin 引起:org.gradle.api.internal.plugins.PluginApplicationException:无法应用插件[id &#39;kotlin-android&#39;] - Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id 'kotlin-android'] 如何在 build.gradle.kts 脚本的插件块中参数化 Kotlin 版本? - How to parametrize Kotlin version in the plugins block of a build.gradle.kts script? 为什么我无法使用 Gradle Kotlin DSL (5.0) 访问 build.gradle.kts 的“插件”部分中的项目属性? - Why I cannot access project properties in "plugins" section of build.gradle.kts with Gradle Kotlin DSL (5.0)? 如何使用 Kotlin gradle dsl 在 android 项目中启用 viewBinging? - How to enable viewBinging in android project using Kotlin gradle dsl? Gradle 将 groovy 转换为 kotlin 用于插件 - Gradle translate groovy to kotlin for plugins
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM