Vlad
01/08/2025, 2:25 PMplugins {
...
val witSomePlugin: Boolean by settings
if (witSomePlugin) {
alias(libs.plugins.somePlugin)
}
}
I just need to conditionally add a plugin into one specific subproject depends on the value in settings/properties.
Main issue that I need to add to specific subproject only (android application project) and I can't add it in deeper subproject which would be added to the androidApp project.
I see in some posts that val witSomePlugin: Boolean by settings
now can be used inside subproject's plugins{} block, but it doesn't work for me.
It is very important to not add that when not needed, because it rewrites complied classes.Vampire
01/08/2025, 2:49 PMby settings
in a project build script.
In a settings script maybe, but even then : Boolean
will not work as form the properties file you get a String
, not a Boolean
.
You could for example write a convention plugin to conditionally apply the plugin and apply that convention plugin.
Or you could use the legacy way to apply a plugin using apply(...)
inside the if
outside the plugins
block.
But either way it is not the best idea.
You can then also not use type-safe accessors to configure that plugin as they would not always be present.Vlad
01/08/2025, 2:55 PMBut either way it is not the best idea.
Then what would be the best/acceptable idea?
And is the problem just by accessing that conditional data from the properties?
If we just replace by settings
with system env System.getenv("withSomePlugin")
we will be good to go, just losing the comfort of having it in the propertiesVampire
01/08/2025, 3:27 PMtapchicoma
01/08/2025, 3:30 PMplugins {
id "plugin" apply false
}
if (withSomeCondition) plugins.apply("plugin-id")
?tapchicoma
01/08/2025, 3:30 PMVlad
01/08/2025, 3:33 PMVampire
01/08/2025, 4:00 PMwhat aboutYep, that was what I meant with legacy application in the condition. But nevertheless not really good practice in most cases. π
Gradle is so complicated.Not really unless you need to do strange and unusual things. π For those it can become a bit complicated yes, but that's the price for having the great flexibility and freedom you have, to do whatever you want and need to.
Vlad
01/08/2025, 4:19 PMVampire
01/08/2025, 4:24 PMask that project to apply the plugin for me(If you meant that the other project should cross-configure your project, that would be even worse and is never the right thing to do. Any cross-project configuration or even only getting information of the model of another project is a veeery bad idea and highly discouraged.
Vlad
01/08/2025, 4:28 PMAny cross-project configuration or even only getting information of the model of another project is a veeery bad idea and highly discouraged.
Yea, I thought so and didn't try that path.
But I am kinda forced anyway. I need the library plugin in one project but the library itself in another subproject and the logic of conditionally connecting that another project in shared project πCLOVIS
01/10/2025, 6:36 PMBut I am kinda forced anyway. I need the library plugin in one project but the library itself in another subproject and the logic of conditionally connecting that another project in shared project πI don't know exactly what you're doing here, but there is definitely another way of doing this. This is just a direction with a whole lot of hurting.