How to directly access the plugin instance created by "apply?

I’d like to directly access the instance of a plugin in my build file in order to call a public method exposed by the plugin’s class. How can I do that?

Typically the plugin would add the method/property to the project model. Anyway, the plugin instance can be accessed via the ‘plugins’ container, e.g. ‘def plugin = plugins.apply(“some-plugin”)’, ‘plugins.findPlugin(“some-plugin”)’, or ‘plugins.withId(“some-plugin”) { plugin -> … }’. For details, see PluginContainer in the Javadoc.

Thanks, I’m now doing this in a

plugins.whenPluginAdded { plugin ->

// … }

block that I needed anyway.

Hi Sebastian,

I’m curious as to why you need access to the instance. The reason I’m curious is that this is something we are considering changing.

I was looking for a way in the Android plugin for Gradle to get the Android SDK directory the plugin is using. I’ve found the public getSdkFolder() method in its BasePlugin.groovy, but it doesn’t look as if that method was added to the project model (How can I actually verify this?). So I needed a way to call this method directly from the plugin instance.

> but it doesn’t look as if that method was added to the project model (How can I actually verify this?)

There’s no automated way. You just have to look at the API of their public types.

Why doesn’t Gradle simply add all public methods of a plugin to the project model automatically?

Basically, there haven’t been compelling use cases identified.