Apply plugin in init.d works fine but closures can only evaluated in subprojects

Hello All,
we apply a gradle plugin in the ~/.gradle/init.d/ folder


allprojects {
    project.buildscript {
        dependencies {
            classpath 'my.plugin:my-plugin:0.0.1'
    afterEvaluate {
       println 'applying my-plugin plugin from ~/.gradle/init.d/myplugin.gradle'
       project.apply plugin: 'my-plugin'

Later in the project wich ‘consumes’ the plugin:

// this is an extension created by my plugin: 
myExtension {
 Could not find method myExtension() for arguments [build_653e6o0dmkyz5sbx0h7mlcngf$_run_closure1@4502e702] on root project 'gradle-dummy-plugin-consumer' of type org.gradle.api.Project.

but this closure works in a subproject

We are using gradle version 4.10.3.
We are looking for a soloution without changing the plugin consumer code. Is this possible?

Kind regard Tom

You can find the whole code here:

afterEvaluate is causing my-plugin to be applied after a project is configured/evaluated, so that’s why myExtension cannot be found during the root project’s configuration/evaluation.

The subproject is “working” because after the root project is configured the plugin is applied to it. When the subproject is being configured it does not yet have the plugin/extension applied, but references that cannot be found on the subproject will be searched for up the project hierarchy. So the subproject is actually configuring the root project’s myExtension.

Remove the afterEvaluate from your init script.

Hallo Chris,
I’ve already tried this but I got this error:

* Where:
Initialization script '/home/tom/.gradle/init.d/myplugin.gradle' line: 9

* What went wrong:
Plugin with id 'my-plugin' not found.

Whoops, sorry, did some research and refreshed my memory. It is not currently possible to apply external plugins by id from an init script. You can apply them by class. In order to reference the class the jars must be made available to the init script.

initscript {
    repositories {
        //repo to find my-plugin
    dependencies {
        classpath 'my.plugin:my-plugin:0.0.1'
allprojects {
    apply plugin:

Hi Chris,
thank you for your answer. Actually this works. But MyPlugin has dependencies to other Plugins (which are also applied by id) and lateron these dependencies are not solved and the allprojects.apply: … in the initscript also fails.