Android/Gradle: conditionally apply plugin based on build type

With Gradle 4.6, the following works:

if (getGradle().getStartParameter().getTaskRequests().toString().contains("Release")) {
    apply plugin: 'testfairy'
} else if (getGradle().getStartParameter().getTaskRequests().toString().contains("Debug")) {
    apply plugin: 'io.fabric'
}

Based on Stefan Oehme, a Gradle core developer, he said:

Plugins can’t be applied to only “part of your project”. They are either applied or not. What is the use case where this becomes a problem for you?

So, the answer is this is not possible. I have exposed my use cases where this is becomes a problem and I’ll see what hi says about it.

Here is a workaround solution I used. The idea is to introduce an Env variable and only apply the plugin in some specific env.

if (System.getenv("PROJECT_ENV") == "Release") {
    apply plugin: 'your plugin'
}

Leave a Comment