When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply
, dependencies
configure
appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi()
to the build's dependencies
block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
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.
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).
From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Project Settings | Modules. Select the necessary module and then open the Sources tab in the right-hand part of the dialog. Click Add Content Root and specify the folder that you want to add as a new content root.
I solved this problem as follows:
update gradle/wrapper/gradle-wrapper.properties
file
change bin
to all
in distributionUrl i.e.distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
todistributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
build.gradle
and add java
and idea
plugin to the plugins listplugins {
id "java-library"
id "idea"
}
./gradlew clean build idea
or simply ./gradlew idea
Preferences
--> build,Execution,Deployment
--> BuildTools
--> Gradle
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Autocomplete functionality as mentioned in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With