Is it possible to make it work without importing MyEnum class to build.gradle?
Yes, but I recommend to just use the import, that’s just idiomatic.
You can do some hacks though, like polluting the Gradle package namespace which is auto-imported.
Or (works for Groovy DSL consumers, but I don’t think for Kotlin DSL consumers) you can add the class as extra property.
Possibly other hack-arounds you also shouldn’t use.
The value of entension properties are available only in the afterEvaluate block. Is there a better way to do it?
Always.
Even your statement is not correct.
They might be available in afterEvaluate, but also maybe not, for example if the are changed themselves in an afterEvaluate block that is executed after yours.
The main effect of using afterEvaluate is introducing ordering constraints, timing problems, and race conditions.
That’s why it is highly discouraged to use them and why actually the Provider/Property APIs were introduced, to get rid of afterEvaluate.
With those lazy APIs, you usually wire one property to another and then evaluate them only at execution time when no configuration changes should or can happen anymore.
If you actually need the values at configuration time, there are other patterns, like not having a property, but a function in the extension that does the logic, or using some DomainObjectCollection where you can react to items being added or removed.
I am writing a plugin that needs to know the enum value from enumExtension during the configuration phase to configure further. Unfortunately, the value is not available unless I read it from within an afterEvluate block.
I read through gradle’s documentation to see if there are examples of reading an extension property value lazily but within the configuration phase. All I could see is how these properties could be used in tasks. My use case however is different and I could not find an alternative to afterEvaluate
As I said, one possibility is to not have a property, but have a function in your extension.
The consumer can then call that function with your enum as argument and you can do the necessary configuration in that method for example.
But as I said, highly depends on the concrete use-case at hand.