Gradle with Intellij: Using build.gradle

When first opening a kotlin project that has a gradle configuration,

If you haven’t installed gradle, check the following tutorial to install it:

Open Intellij:

  • Open:
    • If Intellij is already running: From “File” menu, click “Open”
    • If Intellij is not already running: Choose “Import Project”
  • Select the build.gradle from the project you want to open
  • Click “Open as Project” file from the pop-up menu
  • In the following “Import Project from Gradle” dialog:
  • Select “Use local gradle distribution”
  • For “Gradle JVM”: Pick up your local JDK
  • If encounter a message says: “File already exists”, select “Yes”
  • In encounter “There is an existing project in …”, select “Delete Existing Project and Import”
  • Click “OK”

Now you should successfully import the gradle project.

When you encounter the error “Java home is different” when syncing gradle project.

If you encounter this error: “The specified Gradle distribution ‘blahblah’ does not exist. ”

It means the current version that is used for this repo is not aviablable any more.

  • If you select “Use gradle wrapper task”
  • Check the wrapper task in build.gradle, make sure it has an available version number
  • delete the gradle folder in the root project
  • re-sync gradle

When you see the following message:

  1.  Configure gradle in project – DO NOT DO THIS!!!
  2. Import gradle project – ONLY DO THIS ONE ONLY

The trap is that the first suggestion modifies the gradle.build file, and in fact our trials, this will normally break the build.gradle file.  If you have selected this, the only solution is to then undo those modifications made by IntelliJ and try again.

The import gradle may require setting the gradle home directory.  Set this to the location where you installed gradle. On a Mac, this is typically /usr/local/opt/gradle/libexec.

intelij-bld
Gradle window (view/tool windows/gradle or gradle from right sidebar)

Note if the import gradle option action does not appear, check if the gradle window is already available, in which case import gradle has already taken place.  If not, try closing and reopening the project after checking that the build.gradle file is in the project root.

Once the import gradle is complete (it takes a while),  open the gradle window (view/tool windows/gradle or gradle in the right sidebar) and expand tasks/build and activate the ‘build‘ option under tasks/build.

It might be useful to then select the gradle build in the run debug toolbar (as shown below):

intelij-run

by clicking the dropdown, and selecting an option with the green gradle icon (as shown to the left of Unnamed above) before using the build build/build project or the run or debug symbols from the bar shown above.  Do not use either of these options before running.

Advertisement

1 thought on “Gradle with Intellij: Using build.gradle”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s