How to apply a plugin only for certain build variant in Android?

I want to apply a plugin only for non-release builds. I am currently using this block below android {} section.

androidComponents {
    val isRelease = gradle.startParameter.taskRequests.toString().contains("release", ignoreCase = true)
    if (!isRelease) {
        println("Applying plugin")
        apply(plugin = "com.example.plugin")
    }
}

Isn’t there a better way to achieve this without parsing the command line?