Stylianos Gakis
11/10/2024, 11:53 PMplugins {
id("first.plugin")
id("second.plugin")
}
The "second.plugin" is applying the android gradle plugin in my case.
However inside "first.plugin" I have this sort of code:
class FirstGradlePlugin : Plugin<Project> {
override fun apply(project: Project) {
fun Lint.configure(lintXmlFile: File, lintBaselineFile: File) {...impl here}
when {
pluginManager.hasPlugin(libs.plugins.androidApplication.get().pluginId) -> {
configure<ApplicationExtension> { lint { configure(lintXmlPath, lintBaselineFile) } }
}
pluginManager.hasPlugin(libs.plugins.androidLibrary.get().pluginId) -> {
configure<LibraryExtension> { lint { configure(lintXmlPath, lintBaselineFile) } }
}
else -> {
pluginManager.apply(libs.plugins.lintGradlePlugin.get().pluginId)
configure<Lint> { configure(lintXmlPath, lintBaselineFile) }
}
}
}
}
However I realize that since I declare them in this order, the else is always hit, so I always setup the lint plugin, so then when the android gradle plugin tries to be applies it fails since it realizes that the task "lintFix" was already taken. By the accidentally added libs.plugins.lintGradlePlugin in this case.
Is there a way for me to influence the order of this without having to be careful of which order I am declaring my plugins in each of my modules? Ideally I'd like it to work the same regardless of which order I use.
Or do I perhaps do something completely wrong by doing this hasPlugin approach and I should just do something else all-together?Stylianos Gakis
11/11/2024, 12:02 AMwhen in a
afterEvaluate {
when {
...
}
}
does actually work, but I feel like I am just working around the problem rather than fixing itVampire
11/11/2024, 12:07 AMafterEvaluate wherever you can, and having order requirements between plugins is bad practice.Thomas Broyer
11/11/2024, 12:07 AMhasPlugin conditions with withPlugin()
pluginManager.withPlugin("com.android.application") {
configure<ApplicationExtension> { … }
}
pluginManager.withPlugin("com.android.library") {
configure<LibraryExtension> { … }
}
The "problem" here will be the else. Either you use an afterEvaluate in which you check hasPlugin (or whether the withPlugin were called, setting some variable from them); or you let the user of the first.plugin manually apply the com.android.lint plugin, and you configure it with another withPlugin .Vampire
11/11/2024, 12:08 AMpluginManager.withPlugin to react to other plugins being appliedVampire
11/11/2024, 12:08 AMStylianos Gakis
11/11/2024, 12:13 AMhasPlugin with withPlugin sounds like a simple step, I shall do that.
And yeah, it's very awkward since the lint plugin is something you can apply to normal non-android, just jvm modules. But then you have the lint plugin kinda already applied by default when you are in an android application or library.
And then you get the same type in the end which you can configure all in one way.
So this creates this awkward scenario where I want to only apply that standalone jvm lint plugin when I am in neither an android library nor an android application module, but I can't always just apply it, since it fails when I am in an android module, since it comes pre-applied there.
This is my understanding of this problem at least, it's not unlikely that I am doing something else wrong of course.
I will try and get it working with an afterEvaluate just around the else then for now and go from there unless I get some other idea which just works regardless of if I am in a jvm or non jvm module. Thanks a lot for the input!ephemient
11/11/2024, 12:15 AMStylianos Gakis
11/11/2024, 12:22 AMplugins {
id("everything.plugin")
id("plugin.that.adds.android.library")
}
Non-android
plugins {
id("everything.plugin")
id("plugin.that.adds.jvm")
}
---
With your suggestion this would have to turn into
Android:
plugins {
id("everything.plugin")
id("plugin.that.does.withPlugin.and.configures.lint")
id("plugin.that.adds.android.library")
}
Non-android
plugins {
id("everything.plugin")
id("plugin.that.does.appliesLintItself.and.configures.lint")
id("plugin.that.adds.jvm")
}
Is that what you mean?ephemient
11/11/2024, 12:23 AMStylianos Gakis
11/11/2024, 12:29 AMvar didConfigureLint = false
pluginManager.withPlugin(libs.plugins.androidApplication.get().pluginId) {
configure<ApplicationExtension> { lint { configure(lintXmlPath, lintBaselineFile) } }
didConfigureLint = true
}
pluginManager.withPlugin(libs.plugins.androidLibrary.get().pluginId) {
configure<LibraryExtension> { lint { configure(lintXmlPath, lintBaselineFile) } }
didConfigureLint = true
}
afterEvaluate {
if (!didConfigureLint) {
pluginManager.apply(libs.plugins.lintGradlePlugin.get().pluginId)
configure<Lint> { configure(lintXmlPath, lintBaselineFile) }
}
dependencies {
add("lintChecks", project(":hedvig-lint"))
}
}
And this worked. Thanks for the tip about withPlugin ^^Stylianos Gakis
11/11/2024, 12:29 AMephemient
11/11/2024, 1:01 AMclass AndroidLintPlugin : Plugin<Project> { // plugin.that.does.withPlugin.and.configures.lint
override fun apply(target: Project) {
with(target) {
pluginManager.withPlugin(AndroidBasePlugin::class) {
apply(CommonLintPlugin::class)
class JvmLintPlugin : Plugin<Project> { // plugin.that.does.appliesLintItself.and.configures.lint
override fun apply(target: Project) {
with(target) {
apply(LintPlugin::class)
apply(CommonLintPlugin::class)
class CommonLintPlugin : Plugin<Project> { // doesn't need id (unless you want one)
override fun apply(target: Project) {
with(target) {
val lint: Lint = extensions.getByName("lint")Martin
11/11/2024, 9:50 AMMartin
11/11/2024, 9:51 AMafterEvaluate {} IMOMartin
11/11/2024, 9:52 AMStylianos Gakis
11/11/2024, 10:21 AMMartin
11/11/2024, 10:30 AMblocked from filing to anything under “Android Studio”.That's the good thing to do 👍 The google team does the triage from there.