#IntelliJ IDEA

An Integrated Development Environment (IDE) is custom software designed to support all aspects of software development. It typically includes a code editor, debugging, testing and profiling tools, and anything else that might be needed to support your workflow.

While you certainly can use command-line tools to build applications, we strongly recommend a supported IDE. This provides a large number of advanced features that will be useful in this course e.g. debugging, profiling support, auto-completion in code, integration with other systems and so on.

In this course, we’re going to use IntelliJ IDEA, an IDE produced by JetBrains (the company that invented Kotlin). It supports a number of useful features:

  • Producing new code: tools for navigating existing classes, filling in code snippets for us.
  • Reading existing code: browsing classes, diagramming the system to allow us to build a mental model of an existing system.
  • Refactoring: the ability to produce a series of possibly sweeping changes without breaking existing code. e.g. renaming a method, and everywhere it's invoked; extracting an interface from an existing set of classes.
  • Debugging: visualizations and other tools designed to help diagnose.
  • Profiling: tools to help us understand performance and runtime behaviour.

#Installation

IntelliJ IDEA can be downloaded from https://www.jetbrains.com/intellij

There are two versions of IntelliJ IDEA: the Community Edition, which is open source, and the Ultimate Edition, which is more capable. JetBrains offers free educational licenses to students, which includes a license for IntelliJ IDEA Ultimate if you wish. Make sure to install the version appropriate for your system architecture (e.g. x86 for an Intel processor, or ARM for Apple M1/M2 processors).

IntelliJ will attempt to use the Java JDK that you installed previously. If intelliJ is unable to locate a suitable JDK, it may complain that it cannot locate a Project JDK (i.e. Java JDK). To fix this, click on Setup SDK and enter details for the JDK that you installed (i.e. where it is installed).

#Configuration

It's important to differentiate IDE settings from Project settings:

IDE settings

  • Changes you make in your editor/IDE preferences, typically in IntelliJ > Settings dialog. Examples: font size, code style, key bindings, etc.
  • Saved in your .idea. directory in your project folder.
  • Normally should NOT be saved to Git, since these represent your personal preferences.

Project settings

  • Settings that affect how your project builds and executes. Examples: JDK version, Gradle settings.
  • Saved in your build.gradle.kts file or other project files.
  • These absolutely SHOULD be saved to Git, since they affect how your project is built.

#My Settings

I always recommend setting up a few things in IntelliJ IDEA to make it easier to use.

The first thing I tend to do is to specify how much memory the IDE can use. I have 16 GB of RAM, so I can dedicate 4 GB to IntelliJ IDEA. This drastically increases performance.

  • Help > Change Memory Settings > 4096 MB

Next, I setup the environment to maximize my editing window (and I use hotkeys to toggle windows and navigate).

  • View > Appearance > Compact Mode
  • View > Appearance > Toolbar ON
  • View > Appearance > Tool Window Bars OFF
  • View > Appearance > Navigation Bar > In Status Bar

Finally, I install a few plugins that I find useful. You can install plugins from the JetBrains Plugin Repository, under Settings > Plugins.

Required for this course:

  • Android // android only
  • Android Design Tools // android only
  • Compose Multiplatform IDE support // desktop development only
  • Jetpack Compose // android only
  • Gradle // for Gradle support
  • Gradle Extensions // adds dependency tasks

Recommended

  • Mermaid // for rendering diagrams in markdown
  • Rainbow Brackets // for highlighting matching brackets
  • Database Tools and SQL // for database support, if needed
  • Docker // for Docker service containerization
  • IdeaVim // for Vim keybindings in IntelliJ (!)

#Common Tasks

#Creating a Project

IntelliJ fully supports Gradle, and we can create a new project directly in the IDE.

From the Splash screen, select Create New Project.

New Project Wizard
New Project Wizard

You will need to supply project parameters.

  • Kotlin as your programming language.,
  • Gradle for your build system (using Kotlin as a DSL)
  • JDK should point to your installation.

If successful, IntelliJ IDEA will open into the main window to an empty project.

Empty Project Window
Empty Project Window

After the project has loaded, use View > Tools> Gradle to show the Gradle tasks window, and use build -> build and application -> run to Build and Run respectively.

IntelliJ has a number of windows that it will display by default. You can toggle project windows.

  • Project: a list of all files (Cmd+1).
  • Structure: methods and properties of the current open class/source file (Cmd+7).
  • Source: the current source files (no hotkey).
  • Git: Git status and log (Cmd+9) - not shown.
  • Gradle: tasks that are available to run (no hotkey) - not shown.

#Writing Code

FeatureWhat it doesActivate
Live templatesUse live templates to insert common constructs into your code, such as loops, conditions, various declarations, or print statements.Tab
Implement methods of an interface or abstract classInserts stubs for required methods, with the correct method signature and return types.Ctrl+I
Override methods of a superclassCreates an override method that contains a call to the method of the superclass.Ctrl+O
Code completionHelps you complete the names of classes, methods, fields, and keywords within the visibility scope.Ctrl+Space

#Running Sample Code

We maintain a public Git repository of the source code shown in lectures. To get a copy, git clone the repository URL. This command, for instance, would create a copy of the course materials in a directory named cs346.

$ git clone https://git.uwaterloo.ca/cs346/public/ cs346

The slides folder contains sample code to accompany some of the slide decks. You can build and execute these projects directly in IntelliJ:

  • File -> Open and navigate to the top-level directory containing the build.gradle.kts file. Do NOT open a specific file, just the directory. Click Ok.
  • After the project has loaded, use View -> Tools -> Gradle to show the Gradle tasks window, and use build -> build and application -> run to Build and Run respectively.

IntelliJ with Gradle window open
IntelliJ with Gradle window open

#Reading Code

IntelliJ can generate UML diagrams from existing code. These diagrams will reflect the structure of actual classes and methods in your application.

The documentation contains a section on source code navigation that is worth reading carefully.

  • To navigate backwards, press Cmd+[. To navigate forward, press Cmd+].
  • To navigate to the last edited location, press Shift+Cmd+Backspace.
  • To move caret between matching code block braces, press Ctrl+M.
  • To move the caret to the next word or the previous word, press Alt+Right or Alt+Left.

There are also built-in dialogues that help you navigate through existing code.

FeatureWhat it doesHotkey
Show recent locationsShow the files and sections that have been viewed recently in a dialog, where you can quickly move between them.Shift+Cmd+E
Show type hierarchyType hierarchies show parent and child classes of a class.Ctrl+H
Show method hierarchyMethod hierarchies show subclasses where the method overrides the selected one as well as superclasses or interfaces where the selected method gets overridden.Shift+Cmd+H

#Further Help

There are many excellent online resources for using IntelliJ. I'd suggest starting with the JetBrains IntelliJ Documentation and online help.