Kotlin DSL syntax error for build.gradle.kts

I currently using intellij v2017.2.5 community edition. The editor does not recognize kotlin DSL syntax in the build.gradle.kts. E.g editor flagged out errors such as unresolved reference plugin when I use the plugin function, such as shown below

content of my build.gradle.kts

apply{
plugin(“java”) <–unresolved reference plugin
}
task(“testing”){ <–unresolved reference task
doLast{ <–unresolved reference doLast
println(“hello”) <–the editor recognized println api
}

}

How do I configure the editor to recognize the kotlin DSL syntax?

Note that i configure my project to use local gradle distribution, not gradle wrapper.

Note: I am able to execute this build.gradle.kts successfully using command prompt, which show that the kotlin syntax I using is correct.

1 Like

Hi,

I think the syntax of the kts gradle build file is not correct. It is correct for the groovy based script.

Try the following

plugins {
    java
}

tasks {
    "testing" {
        println(“hello”)
    }
}

Test it with

./gradlew testing

Kr

Tony

Hi Antonel,

Great thanks for your reply. I place your sample code, still the intellij editor displays unresolved reference: plugins error (among all other syntax errors) .
Maybe syntax is correct, but the editor is not configured to recognize it. Hence Is there something i need to do to configure for the intellij editor?

*Note that I did not execute using gradlew, cause I prefer using the local distribution of the gradle (which I download from gradle official website), unless executing the gradlew can help intellij editor to recognize the kotlin dsl.

Hi hanzhiding,

I just recently started to play a bit with the gradle kotlin DSL and I created a project poi DSL.
You can clone it locally and see if this works for you.
https://github.com/jtonic/dsl-poi
For me it works w/o any issues with IntelliJ Idea 2017.2.5 (2017.3 EAP) and latest version of kotlin stdlib and idea plugin (1.1.51). Gradle version: 4.2.1.

Can you tell me the version for:

  1. IntelliJ Idea
  2. IntelliJ Idea kotlin plugin
  3. Gradle
  4. kotlin version in gradle.build.kts

Kind regards.

I did another try.
On my system I had install gradle version 3.4.1 (configured with GRADLE_HOME).
When I ran the gradle script with it, it failed.
Moreover when the gradle wrapper was quite old the gradle.build.kts was full of red lines, and running the application lead to an error.

As soon as I updated the gradle on my machine to latest 4.2.1 all is fine in Idea, gradlew and gradle.

The version of gradle is extremely important. Try to use the latest one. Moreover I remembered I read somewhere that gradle v 4.0 have the issue you are currently facing.

I suppose you use the IntelliJ Idea 2016.1.x or recent.

Note: Even it is an warning I personally took it very seriously. Quite ofter idea warns the version of its kotlin plugin and project one (configured in gradle or maven) is not the same.

I noticed the latest kotlin dsl gradle DSL (built in the latest gradle - 4.2.1) comes with kotlin 1.1.50.
In my project on the github the gradle.build.kts based one is 1.1.51. Modify it to 1.1.50 and the warn should vanish.

Kr

Tony

Hi,

Thanks for your reply and assistance!
Currently, I am already using the below

  1. Intellij IDEA 2017.25
  2. Intellij Idea kotlin plugin (1.1.51-release-IJ2017.2-1)
  3. Gradle 4.2.1
  4. kolin version in gradle.build.kts? I not sure what this mean. I did not configure anything for this.
    Note: I configure Intellij to use my local distribution of gradle 4.2.1, and I did not use gradle wrapper at all.

I have reduced the content of my gradle.build.kts to only 3 lines as shown below

plugins{
java
}

but still the plugins and java words are in red, meaning unrecognized. Not sure why

Extra Info: My application source codes is actually java source code, not kotlin source codes.

Hi,

It is strange. I use the same versions as you and for me it works.
I created a new gradle kotlin dsl project with the gradle 4.2.1 install on my system, and this project also works like a charm. the command i ran was

gradle run

You can clone is from https://github.com/jtonic/gradledsl

If this project doesn’t work for you as well I recommend to Invalidate and Restart project. I hope this will help you.

Kr

Tony

Hi, Thanks!

By invalidate cache and delete away the .gradle folder in my user folder and boom! the error gone. Thanks Antonel for your patient great help!!!

1 Like

Hi,

I am glad it works for you in the end.
I am sure Jetbrains Dev Team will improve the support of kotlin based (build.gradle.kts) gradle project.

For me build.gradle.kts is better than the vanilla gradle.build in terms that it is type safe - this leads to a better IDE support.
Of course Gradle Kotlin DSL (GitHub - gradle/kotlin-dsl-samples: Samples builds using the Gradle Kotlin DSL) is a new project, so it expected to have some rough edges. But surely they will be addressed by the kt/gradle teams and community .

Kr

Antonel

Had the same problem and the same versions of:

  • IntellijIdea 2017.25
  • IntellijIdea kotlin plugin (1.1.51-release-IJ2017.2-1)
  • Gradle 4.2.1
  • kotlin version: 1.1.51 (JRE 1.8.0_131-b11)

Solution is rm -r ~/.gradle. Thanks a lot.

I am seeing this error too. Here are my steps for reproduction:

Create new project
Select Gradle (Kotlin DSL)
Uncheck Java
Check Kotlin (Java)
Fill out the artifact/group/version stuff
Use all default options
Click create
Double click build.gradle.kts to view the file
Tons of red text appears in the file

I ran “check for updates” in IntelliJ before doing this. Ubuntu 16.04 LTS

I’m also having this issue. Not even deleting my .gradle folder helped - still everything red

Using the currently stable versions (IntelliJ IDEA 2017.3.4, Gradle 4.5.1 and Kotlin 1.2.21) on Windows 10, I was able to resolve it by following these steps:

  • Exit IDEA.
  • If needed, update the Gradle wrapper. Repeat the following command until Gradle says it is up to date: ./gradlew wrapper
  • Delete folder .gradle
  • Start IDEA.

It still worked after I restarted IDEA.

2 Likes