Home Android & Kotlin Books Kotlin Coroutines by Tutorials

2
Setting Up Your Build Environments Written by Filip Babić

To start learning about coroutines and suspending functions, you need a place to work. Throughout this book, you will utilize IntelliJ IDEA or Android Studio, which will serve as workstations for all the projects and challenges of this book.

Android Studio is based off of IntelliJ IDEA, so both tools will look and function similarly. Once you set up a good part of the first environment, the second one should be easier to do.

Choosing the build environments

IntelliJ IDEA is great when you have pure Kotlin or Java projects, but it also supports a variety of plugins to those projects, like the Spring framework. Android Studio, on the other hand, is the prime tool used for building Android applications, and it’s crucial for the last section of this book.

Since both of these tools require a Java Virtual Machine (JVM) environment, you’ll have to set that up first.

Configuring the Java development kit

When writing Kotlin, you’re dependent upon the JVM and its build tools, unless you’re using Kotlin/Native. This means that you have to set up the Java development Kit (JDK).

First, go to the JDK dowloand site here: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.

JDK download from oracle.com
JDK download from oracle.com

Please note that there are newer versions of the JDK available, but Android only supports up to version 1.8, and some of the projects in this book are based in Android. This is why JDK 1.8 (or Java 8) is a safe bet for you to use. Once you download it, you can proceed with the installation, and that should be it regarding the Java dependencies.

Your next step is IntelliJ IDEA.

Installing the IntelliJ IDEA

To work with most of the projects in this book, you’ll use IntelliJ. It’s a powerful tool built by Jetbrains, and it helps with productivity using features such as smart autocomplete, code and project templates, and much more.

To install it, go to the Jetbrains website here: https://www.jetbrains.com/idea/download/.

IntelliJ download
IntelliJ download

Choose the free community edition, as it is sufficient enough for the projects that you’ll work on. Download it and, once the download completes, run the installer. This chapter uses MacOS; if you’re using Windows or Linux, make sure to pick the right version for you. The user interface for the installers might be a bit different, depending on which operating system you are using. When you finish installing it, you can run the program, and it will prompt you for settings like so:

Import your settings if any
Import your settings if any

If you haven’t worked with IntelliJ before, choose the default settings or the Do not import settings option. If you already have it installed, or have used it before, you can import your previous settings.

Next, you might be asked to read through and accept the license agreement and privacy policy, which should like like this:

Read and accept JetBrains Privacy Policy
Read and accept JetBrains Privacy Policy

Once you accept everything, the home screen should appear and give you the option to create new projects or open existing ones.

Create a new Project
Create a new Project

To make sure everything works, try creating a simple Kotlin JVM project and see what happens.

You should see a window that asks you what type of a project you would like. Make sure to select Kotlin and the Kotlin/JVM option.

Select the Kotlin platform
Select the Kotlin platform

When you press Next, you should see a project overview screen. This screen shows you details like the project name, the type of runtime and the version of the Java SDK.

Configure your project
Configure your project

The program should find your Java SDK location, but, if it doesn’t, you can manually add it by:

  • Pressing New.
  • Locating the JDK install directory on your computer.
  • You can also go through the the File ▸ Project Structure menu.
  • If you have newer versions of Java installed, this image might show JDK 10, but you should pick JDK 1.8, since the newer versions have some issues when building projects with the Gradle build system.

Setup your JDK configuration
Setup your JDK configuration

If you have to set it up manually, pick the Java 1.8 SDK that you’ve installed and press Apply or Accept. Once the JDK path is set up, you should be able to build and run Kotlin and Java projects! When you open the project, you should see the default layout of an Intellij IDEA project.

The structure of your project
The structure of your project

On the left, you have the project structure view. You can change between different view types, but mostly you’ll use the project overview, as it shows everything. There you can browse through all the files and libraries that you will use in the project.

On the bottom, there are a few things to note, such as the Terminal and the Event log. The bottom strip is reserved for system and build messages, logs, the terminal and the console. You can see any output-related data there. On the top, below the project name, there’s another strip, but this one generally shows tools for the build system and debugging.

IntelliJ is filled with features, which you’ll learn as you read through the book. You could also look up the official documentation to see everything the IDE offers. Now that you’ve set up IntelliJ, the only thing left is the Android Studio!

Building the Android environment

Later on in the book, you’ll work on an Android project to see how coroutines can be implemented in a multi-threaded environment like Android. To do this, you first need Android Studio. Android Studio is an IDE built by Jetbrains, as well.

It also contains many helpful features, like autocomplete and various templates. However, the main benefit is the end-to-end Android build system, powered by Gradle.

To set Android studio up, first go to the download site here: https://developer.android.com/studio/.

Download Android Studio
Download Android Studio

Download the latest stable version and run the installer. You’ll be prompted for a few things, like the SDK you wish to download and the emulator settings. The default options should be alright, but if you wish you can tweak them.

Once you install it, you should see a window similar like this:

Android Studio Launcher
Android Studio Launcher

Starting a new project

You can start a new project.

  • Select Next on the first step.
  • On the second step, where you choose the target API level, pick API 21.
  • Click Next, again.
  • Finally, select the Empty Activity option.
  • Press Next one final time, followed by a Finish.

This should set you up with an empty Android project. You’ll find out about the settings for each Android project later on in the Android section of the book.

If the build system finishes without any errors, it means you have successfully configured Android Studio, and you’ll be able to work on the Android section of the book. Don’t worry about the time it takes for the project to build; the build system is doing a lot of work, so it may take a few minutes.

Importing a project

One of the things you’ll be doing in this book is importing projects. This means that you’re taking in an already-built project and adding it to your workspace. After adding it, the IDE builds it and connects any modules that should be connected in order for the project to work.

For example, if you’re importing a Gradle project, the build environment will connect the scripts and load all the dependencies that you need. You’ll do this in the following chapters, so let’s walk through an example project import.

Note: This is just an example of what it would look like to import a project. You don’t actually have this project available yet; it’s in the next chapter. You don’t have to follow the steps yourself, but do remember to go back to this in case you forget how to import projects in the future.

To import a project in IntelliJ, you have to open it up, and then click the Import project button:

Import a Project in IntelliJ
Import a Project in IntelliJ

Once you press it, a pop-up should appear, asking you which project you want to import.

Find the project you want — the image shows a project from the next chapter of the book, but you get the idea:

Select the project folder from your local file system
Select the project folder from your local file system

Once you pick the project, import by pressing Open; you’ll get a pop-up asking you what type of project would you like to import it as. Usually, the IDE will understand which type it is, so here it knows it’s a Gradle project:

Select Gradle as Import Model
Select Gradle as Import Model

Then, once you choose the type of module you want to import and press Next, you’ll see some general settings for the Gradle modules. But, in general, you can just press Finish and everything should work:

Accept the default configuration
Accept the default configuration

For this chapter, you don’t have a project; in the next chapter, you’ll have to import a project with Gradle. Once you’ve done that, you’ll see a new project, and you’ll be ready to play around with coroutines!

Key points

  • The Build environments that you’ll use for the book require Java.
  • IntelliJ IDEA is the most popular JVM development environment, and it is developed by Jetbrains.
  • IntelliJ is powered by the IntelliJ platform, enabling features such as autocomplete, templates, pre-baked projects and many more.
  • For Android projects, you’ll use Android Studio, also developed by Jetbrains.
  • Android Studio uses the Gradle build system, fully integrated into every project.
  • Importing project connects all the gears that need to run fluently for you to work.

Where to go from here?

Now that you have environments set up, you can finally work on projects and write some Kotlin and coroutines code. Some of the chapters in the book might come with starter projects, which are already set up, so you don’t have to do any extra work. Some might require you to complete a challenge. Without further ado, time to start writing practical code!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2021 Razeware LLC