简体   繁体   中英

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 :

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:

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?

  2. How can I do it using project.hasPropperty("") ?

You can't. The plugins DSL is limited in what you can and can't do. This is documented in Limitations of the plugins DSL .

To conditionally apply a plugin, you must use 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.

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?

You can't update the environment ( System.getenv() ), it is a unmodifiable map.

How can I do it using 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>()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM