Chapters

Hide chapters

Kotlin Coroutines by Tutorials

Third Edition · Android 12 · Kotlin 1.6 · Android Studio Bumblebee

Section I: Introduction to Coroutines

Section 1: 9 chapters
Show chapters Hide chapters

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.

You’ll also install the Java Development Kit (JDK) with the IntelliJ IDEA’s internal wizard, so let’s first install IntelliJ.

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 Windows; if you’re using MacOS 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 might prompt you to import settings from a previous installation.

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

Whatever you choose, know that you can always manually import settings by going to File ▸Manage IDE Settings ▸ Import Settings, like so:

Import your settings if any
Import your settings if any

Next, you might be asked to read through and accept the user agreement 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.

Configuring the Java Development Kit

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

The easiest way to do so is to start a new project with IntelliJ and use the New Project Wizard. If you’ve installed the IDE and accepted all the licenses, you should see the following starting screen. Note that your list of projects will probably be empty!

Now click on New Project to create a simple Kotlin JVM project and you should see the following:

Create a new Project
Create a new Project

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

Select the Kotlin platform
Select the Kotlin platform

Note the Project SDK tab at the top. This features your development kit that you’ll use for the code, typically the JDK.

If you already have JDK installed, you should be good to go. If not, click on the dropdown menu and select Download JDK. You should see some of the following options:

Download JDK
Download JDK

Based on your project and the language features you want to use, this version will vary. For this book, you can choose JDK 16 or 17. Oracle OpenJDK is a solid option so download that JDK and you should be ready to go.

When you press Next, you should see a project overview screen. This screen shows you details like the project name and its location.

Configure your project
Configure your project

Finally, press Finish and you’ll start your IntelliJ project! 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 Android Studio!

Installing 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 to this:

Android Studio Launcher
Android Studio Launcher

Starting a New Project

Now you can start a new project. First select New Project, you should see the following menu that lets you choose the project template.

Android Studio Project Template
Android Studio Project Template

Then, select Empty Compose Activity for the project template. You could go with an Empty Activity too, if you’re using XML and the View toolkit. Click Next to move to the next step where you can fill in the project details like the project name, package name and the location.

Android Studio Project Details
Android Studio Project Details

Like with IntelliJ, each project has specific information you need to put in before creating it. In this case you have the following:

  • Name
  • Package name
  • Save location
  • Language
  • Minimum SDK

The name and save location are self explanatory. The package name serves as a unique identifier for your project, in case you push your app to the Play Store, as it will help you form a unique URL to find the app.

The language of the project represents if you want to primarily use Kotlin or Java.

And finally, the Minimum SDK represents what is the lowest version of Android your app can run on. For the version, choose API 21 to cover the largest audience with your app.

Finally, press Finish and wait for the project to build.

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 IntelliJ and then click the Open 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 open.

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 it by pressing OK. Usually, the IDE will understand which type of project it is, so it should know it’s a Gradle project and prepare everything for you. Sometimes, the Kotlin plugin version you have installed might not be the same as what the project uses, so you might get a prompt to update the plugin.

Additionally, your system might ask you if you trust the project or if you want to open it in Safe Mode.

Trust projectg
Trust projectg

But since we’re so trustworthy you can just click Trust Project and it will be fine.

For this chapter, you don’t have a project. Starting from the next chapter, you’ll open projects for each chapter and you’ll be ready to play around with coroutines!

Key Points

  • The Build environments that you’ll use for the book require the Java Development Kit.
  • IntelliJ IDEA is the most popular JVM development environment, and 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, in cooperation with Google.
  • Android Studio uses the Gradle build system, fully integrated into every project.
  • Importing a 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.
© 2024 Kodeco Inc.