Seems to be a basic question but I am yet to find ...
# kotlin-dsl
m
Seems to be a basic question but I am yet to find a solution that would feel idiomatic, that being said, If I want my convention plugin to work with or rely on other plugins information this is an obvious solution
Copy code
project.pluginManager.withPlugin
But what if I need several plugins to be applied beforehand, for example both agp and kgp? What's the right approach? Is there an API I am missing? Or is the API itself seems to be missing?
m
You set a flag in each
withPlugin{}
block and then you do an
afterEvaluate {}
that fails if not all your flags are set
This is one of the only valid use cases of
afterEvaluate{}
Something like this but generalized for more plugins
m
That's what I always did but I wish there would be a better way like vararg
withPlugins
v
No, this is not a valid use-case for
afterEvaluate
, just nest the
withPlugin
calls if you need both.
Copy code
pluginManager.withPlugin("<kgp>") {
    pluginManager.withPlugin("<agp>") {
        // do what needs both plugins applied
    }
}
👀 1
âž• 1
Also, this is not really Kotlin DSL related, it is the same, no matter which language or DSL you use.
👀 1
m
this is not a valid use-case for
afterEvaluate
You need
afterEvaluate{}
if you need to fail if the plugins are not applied (which I agree is not part of the original question but often related)
v
If you need to fail if the plugins are not applied, you should instead apply the plugins and not react to them being applied 🙂
m
Not if you want the user to control the version
Or rather, not if you want to display a user friendly message
v
Why not? You can just apply the version that was added by the user to the classpath or that he already applied
m
Yeayea but if that's not in the classpath it's going to fail with a weird message
v
Check whether the plugin class is available and throw a meaningful error if it is not found in the classpath 🙂
And even if you want to react and fail, it is probably better to have some execution time check that fails the build if the expected plugins are missing rather than
afterEvaluate
🙂
m
That works. I'm all for not using
afterEvaluate{}
TBH.
👌 1
e
This is one of the only valid use cases of
afterEvaluate{}
No, not really, you should chain the with plugins together!
👆 1
Oops! sorry, didn’t read the entire thread before typing 😅
😄 1
🤷 1