0

What is the best way to apply a gradle plugin to only a specific build variant. For example we might have

  • usStage
  • usDebug
  • usProduction

but a plugin has a lot of build overhead for developers (several minutes), and has no need to run on usStage and usProduction. Normally for libraries you would just use something like releaseImplementation, but I am not aware of anything like this existing at the gradle plugin level.

So right now we have somethig like this at the top of our app/build.gradle.kts in Android

plugins {
    id("com.google.gms.google-services")
    id("com.google.firebase.crashlytics")
    id("com.heapanalytics.android")
    id("com.gladed.androidgitversion") version "0.4.14"
    id("jacoco-reports")
}

where id("com.heapanalytics.android") is the culprit of slow build times on debug, where it is not even needed.

Is there a way that within our build variants, to only apply this for usProduction?

I have tried something like this:

plugins {
    id("com.google.gms.google-services")
    id("com.google.firebase.crashlytics")
    id("com.gladed.androidgitversion") version "0.4.14"
    id("jacoco-reports")
}

android {
defaultConfig {
    ...
    
signingConfigs {
    create(release) {
        ...
    }
    getByName(debug) {
        ...
    }
}

buildTypes {
    getByName(debug) {
        ...
    }

    getByName(release) {
        ...

        apply(plugin = "com.heapanalytics.android")

        ...
    }

But it just applies the plugin to every variant still, I believe because that block configures all variants even when you are only building one.

if (project.hasProperty(BuildProperties.Metrics.ENABLE_HEAP)) {
    apply(plugin = "com.heapanalytics.android")
}

Tried something like this too, which may work, but was not really sure it is the best way. Feels hacky.

sonrohancue
  • 321
  • 2
  • 12

1 Answers1

0

You can do this in many ways but for the most recent solution I know and use do:

android {
   productFlavors {
      flavourName{
         apply plugin: 'com...'
      }
   }
}

For more solutions please reference to here.

Top4o
  • 547
  • 6
  • 19