18

I would like to make something like (pseudo code):

if (BuildType == "release"){
    apply plugin: 'testfairy'
} else if (BuildType == "debug"){
    apply plugin: 'io.fabric'
}

The idea is that based on the build type, apply (or not) a plugin. How to do it ?

JoseF
  • 1,261
  • 13
  • 30

5 Answers5

26

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'
}
Jason Grife
  • 1,197
  • 11
  • 14
  • This is the answer, we had need of this to only apply the performance monitoring to prod releases. Adding it to dev/debug builds increased the cold build times by almost 1 minute or more. We opted to not impact our devs everytime they built locally. Our solution was slightly different but it worked. – JPM Nov 05 '19 at 17:35
  • worked on android studio with gradle 7.4 – erik.aortiz Aug 30 '22 at 16:05
  • this works on Android studio and on command line if you give variant, but it will not work if your give at command line ./gradlew build to build all variants at once. – diidu Sep 01 '22 at 13:27
  • This is the right answer! I've been looking for this for a long time – Natan Lotério Jun 12 '23 at 07:14
4

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'
}
blade
  • 2,285
  • 1
  • 18
  • 12
  • This is close to the right solution but try using firebase perfmon and see that this does not work. – JPM Nov 05 '19 at 17:36
2

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.

JoseF
  • 1,261
  • 13
  • 30
  • 5
    @jason-grife has the answer, it should be marked as the solution to this question – Fabio Feb 09 '19 at 11:45
  • 3
    This is a weird self-answer, because the question as worded isn't asking if plugins can only be applied to *part* of a project; it's asking if there is a way to *conditionally* apply a plugin to the *entire* project. – Herohtar May 12 '22 at 20:58
  • We are using a plugin which is actually for release builds. Debug builds does not use it. So I want to skip it for debug builds to speed up builds. In my opinion, this is a valid use case. – Murat Jun 07 '23 at 16:47
1

Here was the solution I had that did not crash the app. Other solutions did crash when the class was finally called with a Class not found exception.

def tasks = gradle.startParameter.taskNames[0] ?: ""
if (tasks.toLowerCase().contains("prod")) {
    println "Firebase-Performance pluign applied..."
    apply plugin: 'com.google.firebase.firebase-perf'
}
JPM
  • 9,077
  • 13
  • 78
  • 137
0

Remember maven profiles? You can do something similar using this snippet which was borrowed from gradle-fury

in your build file if (project.hasProperty('profile') && project.profile.split(',').contains("ci")) { //do something }

then run it when gradlew -Pprofile=ci

There's a complete example here https://github.com/gradle-fury/gradle-fury/blob/develop/build.gradle

Disclaimer, i work on gradle-fury. for science

spy
  • 3,199
  • 1
  • 18
  • 26