Extending an existing plugin DSL?

I’m really liking the Gradle DSL approach, but now I wonder, is it possible to install extensions into existing DSL. Say you want to crate a plugin that supplements the Android plugin:

android {
        brandA {
            applicationId = "com.branda"
            myCustomProperty = "foo"

…where myCustomProperty is not recognized by the Android plugin. I suspect this is not possible without the existing plugin is designed for extension (trap the methodMissing(…) call). Or perhaps it’s entirely possible though Groovy meta-programming but just not recommended (most plugins seem to define their own root DSL, even if they are designed to work in unison with another plugin).

I did it with meta-programming to add properties to the Gradle core eclipse plugin.
Then in my custom plug-in, I’m using these custom properties to perform some custom logic.

I’m afraid you don’t have any other option, if the plug-in doesn’t implement propertyMissing / methodMissing methods.

From my point of view, if it’s better for you to add stuffs in an existing plug-in (e.g. cleaner, more concise build script ), I don’t see anything wrong about it.

1 Like

I believe that ProductFlavor is enhanced with an ExtraPropertiesExtension, so I think you could use ext.myCustomProperty = "foo" and reference it via brandA.myCustomProperty later.

Depending on what you want to do, that might work.

1 Like

Could you show us a code snippet of the way?

For this specific use case, Sterling’s answer is the way to go.

If you really want to use meta-programming,
MyClass.metaClass.foo = ... adds a property foo to all instances of MyClass.

1 Like