Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure IntelliJ auto-completion for gradle script in kotlin

Tags:

I am trying out gradle-script-kotlin with simple hello-world application in IntelliJ. But IntelliJ auto-completion doesn't popup in build.gradle.kts file.

https://github.com/gradle/gradle-script-kotlin/tree/master/samples/hello-world

build.gradle.kts:

apply<ApplicationPlugin>()  configure<ApplicationPluginConvention> {     mainClassName = ".HelloWorld" }  configure<JavaPluginConvention> {     setSourceCompatibility(1.8)     setTargetCompatibility(1.8) }  repositories {     jcenter() }  dependencies {     testCompile("junit:junit:4.12") } 

settings.gradle:

rootProject.buildFileName = 'build.gradle.kts' 

I have IntelliJ kotlin plugin and gradle plugin installed and using gradle 3.0. The sample application works with gradle commands.

How to configure IntelliJ to enable auto-completion on build.gradle.kts file?

I'm using IntelliJ 2016.2.2 with kotlin plugin version: 1.0.3-release-IJ2016.1-120

like image 323
TheKojuEffect Avatar asked Aug 16 '16 04:08

TheKojuEffect


People also ask

How do I enable auto completion in IntelliJ?

By default, IntelliJ IDEA displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Shift+Space or choose Code | Code Completion | Type-Matching from the main menu. If necessary, press Ctrl+Shift+Space once again.

How do I enable Gradle Auto Import in IntelliJ?

Go to File > Settings (or hit Ctrl + Alt + s ), and navigate to Build, Execution, Deployment > Build Tools > Gradle. Select Automatically import this project on change to build script files and hit OK: JetBrains, who created IntelliJ IDEA, say that this setting can slow things down on large projects.

How do I refresh the Gradle cache in IntelliJ?

We can do this by: Selecting one of the suggestions in the message; Pressing on the Refresh Gradle icon in the top right, Using the keyboard shortcut ⇧⌘I (macOS), or Ctrl+Shift+O (Windows/Linux).

How do I sync Gradle project in IntelliJ?

If you have some custom plugins that require you to import your project from the IntelliJ IDEA model, press Ctrl+Shift+A and search for the Project from Existing Sources action. In the dialog that opens, select a directory containing a Gradle project and click OK. IntelliJ IDEA opens and syncs the project in the IDE.


2 Answers

Had the same problem. Gradle script Kotlin requires version 1.1x of the IntelliJ Kotlin plugin.

Follow these steps to install it:
https://github.com/gradle/gradle-script-kotlin/tree/master/samples#install-idea-kotlin-plugin

The regular update channel only updates to version 1.0.3x right now.

Update 18/06:
The github readme has been updated to include:
(Note: this version will not work with the official Gradle 3.0 release, stick to the official EAP 1.1 from JetBrains if you intend to use Gradle 3.0)

If you manually installed version 1.1.0-dev-2222 earlier, uninstall it and restart IntelliJ.

Install version 1.1.x from the EAP Channel

This version works with Kotlin-Script in Gradle 3.0.

In IntelliJ, press:
Tools => Kotlin => Configure Kotlin Updates.
Select
Early Access Preview 1.1
and press
Check for updates now.

Download the latest plugin.
If it gives an error, just restart IntelliJ, it will have installed the plugin.

like image 89
Lauw Avatar answered Sep 29 '22 16:09

Lauw


I tried gradle 3.1 with kotlin-plugin-1.1.0-dev-2222.zip in intellij 2.5. And it works for me.

Here is my intellij version:
IntelliJ IDEA 2016.2.5 Build #IC-162.2228.15, built on October 14, 2016 JRE: 1.8.0_112-release-287-b2 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

And the steps:

1, Download the kotlin-plugin-1.1.0-dev-2222.zip in https://github.com/gradle/gradle-script-kotlin/tree/master/samples and install the plugin in Idea
2, Create a gradle java project in Idea, and choose to use gradle wrapper
3, In the terminal, under the project directory, use "./gradlew wrapper --gradle-version=3.1" to switch to gradle 3.1
4, Create a file "build.gradle.kts" under the root directory of the project
5, Add rootProject.buildFileName = 'build.gradle.kts' in the settings.gradle file
6, Add codes in build.gradle.kts, and if the auto-completion does not work, try "Refresh all projects" in the Gradle Tool Window.
7, If it still not work, restart your Idea

Here is a github repo: https://github.com/kolyjjj/gradle-kotlin-test

like image 25
koly Avatar answered Sep 29 '22 14:09

koly