Android/Gradle: conditionally apply plugin based on build type
Asked Answered
N

5

19

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 ?

Niobous answered 16/3, 2016 at 15:15 Comment(2)
I have asked the same question on Gradle forum: discuss.gradle.org/t/…Niobous
I'm interested too. Did you find a solution?Hysterical
N
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.

Niobous answered 21/3, 2016 at 10:19 Comment(3)
@jason-grife has the answer, it should be marked as the solution to this questionHouck
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.Elongate
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.Grogshop
S
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'
}
Strabismus answered 9/4, 2018 at 18:25 Comment(4)
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.Allethrin
worked on android studio with gradle 7.4Patriciate
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.Pentyl
This is the right answer! I've been looking for this for a long timeMelena
E
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'
}
Edette answered 3/10, 2016 at 18:10 Comment(1)
This is close to the right solution but try using firebase perfmon and see that this does not work.Allethrin
N
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.

Niobous answered 21/3, 2016 at 10:19 Comment(3)
@jason-grife has the answer, it should be marked as the solution to this questionHouck
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.Elongate
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.Grogshop
A
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'
}
Allethrin answered 5/11, 2019 at 17:39 Comment(0)
N
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

Nightingale answered 11/10, 2016 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.