Is there any difference between: ```plugins.withTy...
# community-support
s
Is there any difference between:
Copy code
plugins.withType<JavaBasePlugin> { ... }
and
Copy code
plugins.withId("java-base") { ... }
and
Copy code
pluginManager.withPlugin("java-base") { ... }
?
n
I don't think so. Except that the
withType
call returns a collection, so that might potentially affect multiple applied plugins (either immediately, or deferred, which is true for all cases).
👍 1
v
withType
could not really affect multiple applied plugins, as every plugin can only applied once and every attempt after that is a NOP.
But if you look at the JavaDoc of
getPlugins()
(which you use by
plugins.
) you see, that even if not deprecated you should not use it at all, but
pluginManager
instead.
The problem with
plugins.withType
concretely is mainly when matching 3rd party plugins because depending on class loader in effect where it is called, you might get different class instances and so not match the plugin you intend.
With built-in plugins this should not be possible, but nevertheless, just use the recommended
pluginManager.withPlugin
and you are on the safe side. 🙂
s
Thanks!
v
Btw. never use any
...withType<...> { ... }
but always
...withType<...>().configureEach { ... }
. Currently it is mainly making a difference with task containers to not break task-configuration avoidance, but in case another container gets treated lazily in the future, you are already on the safe side.
s
Yeah, I knew already that one is preferred to the other but mixed them up.
t
Wrt
withType
and "multiple applied plugins", you could pass a base class or an interface that could be extended/implemented by more than one plugin (e.g. all plugins from the Android Gradle Plugin extend from
com.android.build.gradle.BasePlugin
). Kind of an edge case anyway.
☝️ 1
v
Ah, yeah, that's right of course 🙈