artk2002
artk2002

Reputation: 151

Gradle Cannot have abstract method

I'm developing a custom binary Gradle plugin, following the pattern shown here.

My extension, VersionInfoExtension is abstract with abstract methods, just like the BinaryRepositoryExtension in the documentation. My task is also abstract, with abstract methods for the parameters, like the LatestArtifactVersion in the documentation. When I test, I get the following error:

An exception occurred applying plugin request [id: 'com.example.version-info']
> Failed to apply plugin 'com.example.version-info'.
   > Could not create an instance of type com.example.gradle.version.VersionInfoExtension.
      > Could not generate a decorated class for type VersionInfoExtension.
         > Cannot have abstract method VersionInfoExtension.jars().

What am I missing? Are there any good examples of making this work?

Upvotes: 4

Views: 4033

Answers (3)

Nikola Despotoski
Nikola Despotoski

Reputation: 50548

In addition to @Richard Sitze answer, in my case it was a custom setter function that was used in tests only (annotated with @TestOnly) was preventing generating the class.

I had declared serializable object as @Input

@get:Input 
abstract val myPropertry : Property<MyObject>

@Test
fun setMyProperty(obj : MyObject){
   //...
}

Somehow the name of the function interfered with generating the property setter along generation of the setter.

Upvotes: 1

Jesper Skov
Jesper Skov

Reputation: 11

The problem here seems to be the name of the abstract method.

All configurable methods must be bean-methods - this holds for both Extensions and Tasks.

So you should have used (assuming a java class):

 abstract Property<String> getJars()

instead of

 abstract Property<String> jars()

Upvotes: 1

Richard Sitze
Richard Sitze

Reputation: 8463

I'm using gradle 7.X and Kotlin.

The "Cannot have abstract method myPropertyName" error is reported when the method name is prefixed by "is":

abstract val isRegistered: Property<Boolean>

That was annoying to track down. The type doesn't seem to matter.

The solution was to remove "is" from the name.

Upvotes: 11

Related Questions