Android/Gradle: conditionally apply plugin based on build type

Viewed 8040

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 ?

5 Answers

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'
}

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'
}

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

Related