How to set sourceSets with Gradle 4.10 rc2


(ian) #1

I am using the following gradle kotlin DSL code:

    java.sourceSets["main"].java {
        srcDir("src/main/extraSource")
    }

works perfectly in gradle 4.9, but in 4.10 rc1 and 4.10rc2 gives the following error:

  Line 5: java.sourceSets["main"].java {
               ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
                   public val Project.sourceSets: SourceSetContainer defined in org.gradle.kotlin.dsl

Has this changed? If so, is there any doco on the change?


Gradle 4.10-rc-2 is now available for testing
(Mike Kobit) #2

Yes, it did change. It is documented in the RC release notes at https://docs.gradle.org/4.10-rc-2/release-notes.html#changes-to-the-gradle-kotlin-dsl

It is now itself a top level object.

So instead of java.sourceSets["main"] you should just be able to do sourceSets["main"]


(ian) #3

Thank you for the reply.

I had tried following those notes:

The source set container can now be accessed using project.sourceSets , or just sourceSets . Previously it was located at project.java.sourceSets , or just java.sourceSets .

But perseverance that comes from the confidence of you post helps.
Strangely - switch back to gradle wrapper to use the new version… and i get the error
But using just 'sourceSets[“main”] yeilds

  Script compilation error:
  Line XX: sourceSets["main"].java {
           ^ Unresolved reference: sourceSets
1 error

and project.sourceSets[“main”] also yields
Script compilation error:

  Line XX: project.sourceSets["main"].java {
                   ^ Unresolved reference: sourceSets
 1 error

However, then I launch the build directly, rather than click the ‘reload’ when a change is detected…and then the build passes!

Seems it takes Intellij an extra run to ripple through the changes? Still highlights as an error, but works after first run.

First try, the new source is not actually brought into the project. No highlighting as a source folder. Modify to do a dummy change, rebuild and it does work. Source folder shown, app builds.
There must still be some adjustment to get all correct as source syntax highlighting still shows as an error.

Thanks again. Wonder how I get the syntax highlight to play nice,


(Mike Kobit) #4

I believe that that IDEA needs to update to be able to get the new script context from Gradle to be able to appropriately suggest auto-completion. This means the script needs to be compilable.

The sourceSets piece gets generated based on applied plugins in plugins {}. If IDEA refresh or auto-refresh doesn’t work, sometimes I need to go nuclear and run the Invalidate and Restart to completely flush IDEA.

If you have a good way to reproduce it, I would suggest opening an issue.

Side note, this was also asked on Stack Overflow