'I cannot reference a Gradle custom extension using a closure

I'm writing a custom gradle plugin that uses an extension. Here's what the relevant part look like:

class Aar2Jar implements org.gradle.api.Plugin<Project> {

void apply (Project project) {
    println('applying Android Jar')
    def ext = project.extensions.create('jarSpec', JarSpecExtension)
    if (ext.name.get() == "") {
        ext.name.set(project.name)
    }

and Here's what the extension looks like:

package test.android.gradle

import org.gradle.api.provider.Property

abstract class JarSpecExtension {
    abstract Property<String> getName()
    abstract Property<String> getVersion()
    abstract Property<String> getInclude()
    abstract Property<String> getExclude()

    JarSpecExtension() {
        name.convention("")
        version.convention("1.0.0")
        include.convention("*")
        exclude.convention("")
    }
}

And here's what my build.gradle script that uses this custom plugin looks like:

plugins {
    id 'com.android.library'
    id 'test.android.jar'
}

jarSpec {
    name = 'myJar'
}

The problem is that doesn't work and I get the following error:

* What went wrong:
An exception occurred applying plugin request [id: 'test.android.jar']
> Failed to apply plugin 'test.android.jar'.
   > No such property: extensions for class: test.android.gradle.Aar2Jar

However, if I change the build.gradle file to this:

plugins {
    id 'com.android.library'
    id 'test.android.jar'
}

jarSpec.name = 'myJar'

It works perfect. The question is why can I not refer to the extension using the closure style. I'm using gradle version 7.2.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source