spierce7
spierce7

Reputation: 15746

Only apply plugin for kotlin script gradle in some situations, but then can't resolve plugin configuration

My team is converting their Gradle scripts from Groovy to Kotlin, and there is some behavior that we're struggling to replicate with Kotlin script gradle.

Sometimes we only apply and configure a plugin when building on CI. There is no sense in applying a plugin you don't use.

plugins {
  if (Build.isCi) {
    id("com.google.firebase.appdistribution")
  }
}

But, then when we are building locally we have an error in our build script because our plugin configuration can't be resolved:

if (Build.isCi) {
  firebaseAppDistribution { // <-- Can't resolve `firebaseAppDistribution`
  }
}

Upvotes: 1

Views: 247

Answers (1)

Cisco
Cisco

Reputation: 22952

The plugins DSL is strict as to what you can do. That is especially true for Kotlin since it is statically typed and compiled in the background.

I believe you will need to do something like:

plugins {
    id("com.google.firebase.appdistribution") apply false
}

This should ensure the the Firebase plugin's related extensions/classes are on the build script classpath.

Then somewhere later on in your build script, actually apply and configure the plugin:

plugins {
    id("com.google.firebase.appdistribution") apply false
}

if (Build.isCi) {
    apply(plugin = "com.google.firebase.appdistribution")
    configure<FirebaseAppDistributionExtension> {
        // ...
    }
}

I don't know the actual type/class of firebaseAppDistribution, so I've used FirebaseAppDistributionExtension as an example. You will need to find out the concrete type of the extension and use that as the type.

References:

Upvotes: 1

Related Questions