Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IntelliJ to associate Gradle sources with build.gradle?

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?

like image 578
Doron Gold Avatar asked Sep 07 '15 16:09

Doron Gold


People also ask

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.

How do I refresh all Gradle dependencies 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 change the source directory in IntelliJ?

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.


1 Answers

I solved this problem as follows:

  1. As mention in already posted answers, configure gradle
  • 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
    to
    distributionUrl=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
    }
    
  1. Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
    id "java-library"
    id "idea"
}
  1. From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
  2. Import project to IntelliJ idea.
    Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
    You can see options to choose
  3. Restart IntelliJ idea IDE.

So above we have configured both of the options so choose either of them, except the specified location option. That's it.

Before

Before

After

after

Autocomplete functionality as mentioned in this answer.

autocomplete

like image 156
dkb Avatar answered Nov 15 '22 08:11

dkb