Book Image

Kotlin Quick Start Guide

By : Marko Devcic
Book Image

Kotlin Quick Start Guide

By: Marko Devcic

Overview of this book

Kotlin is a general purpose, object-oriented language that primarily targets the JVM and Android. Intended as a better alternative to Java, its main goals are high interoperability with Java and increased developer productivity. Kotlin is still a new language and this book will help you to learn the core Kotlin features and get you ready for developing applications with Kotlin. This book covers Kotlin features in detail and explains them with practical code examples.You will learn how to set up the environment and take your frst steps with Kotlin and its syntax. We will cover the basics of the language, including functions, variables, and basic data types. With the basics covered, the next chapters show how functions are first-class citizens in Kotlin and deal with the object-oriented side of Kotlin. You will move on to more advanced features of Kotlin. You will explore Kotlin's Standard Library and learn how to work with the Collections API. The book finishes by putting Kotlin in to practice, showing how to build a desktop app. By the end of this book, you will be confident enough to use Kotlin for your next project.
Table of Contents (15 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Build tools


For anything more than a simple app, you will probably use some kind of build automation tools like Maven, Ant or Gradle. The good news is that Kotlin is supported by all three of them.

In this section, will see how to set up Maven and Gradle tools for development with Kotlin.

If you are starting a new project with Gradle or Maven, your IDE probably can generate the required files for you. Here you will learn how to add Kotlin manually because you might have an existing Java project and want to start using Kotlin with it or in case you don't want to use an IDE.

Gradle

In your Gradle project, you have the build.gradle file located somewhere in the root of your project. Make sure that you have this buildScript section in that file.

buildscript {
   ext.kotlin_version = '1.2.40'

   repositories {
       mavenCentral()
   }
   dependencies {
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }
}

This tells Gradle to use a Kotlin plugin and adds Maven central to the list of repositories. With the plugin added, we still need the Kotlin standard library to be able to compile a Kotlin source code. If you have multiple Gradle modules, make sure that the module's build.gradle file in which you plan to use Kotlin has the following:

apply plugin: "kotlin"

repositories {
   mavenCentral()
}

dependencies {
   compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

compileKotlin {
   kotlinOptions.jvmTarget = "1.8"
}

If you don't have multiple build.gradle files, then put this to the root build.gradle file. The same one in which you added the Kotlin plugin.

You can also set various compiler options under the compile Kotlin section. For example, with the kotlinOptions.jvmTarget = "1.8" option we told the Kotlin compiler to produce Java 1.8 compatible bytecode.

Now, if you sync your Gradle project, you should be able to add Kotlin files to it and compile them with Gradle.

Maven

If Maven is your build tool of choice, then setting up Kotlin will be just as easy.

In the same way as with Gradle, we first need to add a Kotlin plugin to a pom.xml file. You can do this by adding these lines to the plugins section:

<plugin>
   <artifactId>kotlin-maven-plugin</artifactId>
   <groupId>org.jetbrains.kotlin</groupId>
   <version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
          <goals><goal>compile</goal></goals>
      </execution>
    </executions>
</plugin>

We can define a Kotlin version at one place, inside the properties section:

<properties>
   <kotlin.version>1.2.40</kotlin.version>
</properties>

In the same way as with Gradle, the Kotlin standard library needs to be added to dependencies.

<dependencies>
   <dependency>
       <groupId>org.jetbrains.kotlin</groupId>
       <artifactId>kotlin-stdlib</artifactId>
       <version>${kotlin.version}</version>
   </dependency>
</dependencies>

If you are targeting JDK 8, you can replace kotlin-stdlib with kotlin-stdlib-jdk8 so you can have Kotlin extension functions that cover the latest APIs from JDK 8.

This should be enough if you plan on only writing Kotlin. If you are mixing Kotlin and Java inside a Maven project, a Kotlin compiler needs to run before a Java compiler. To instruct Maven to compile Kotlin first, the Kotlin plugin needs to be before the Maven compiler plugin.

Here's what the complete build section looks like:

<build>
   <plugins>
       <plugin>
           <artifactId>kotlin-maven-plugin</artifactId>
           <groupId>org.jetbrains.kotlin</groupId>
           <version>${kotlin.version}</version>
           <executions>
               <execution>
                   <id>compile</id>
                   <goals> <goal>compile</goal> </goals>
                   <configuration>
                       <sourceDirs>
                           <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                           <sourceDir>${project.basedir}/src/main/java</sourceDir>
                       </sourceDirs>
                   </configuration>
               </execution>
           </executions>
       </plugin>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>3.5.1</version>
           <executions>
               <execution>
                   <id>default-compile</id>
                   <phase>none</phase>
               </execution>
               <execution>
                   <id>java-compile</id>
                   <phase>compile</phase>
                   <goals> <goal>compile</goal> </goals>
               </execution>
           </executions>
       </plugin>
   </plugins>
</build>

Finally, there are various Kotlin compiler options that can be set under the configuration section of a Kotlin plugin. Here's an example of how to instruct the compiler to produce a Java 1.8 compatible bytecode.

<plugin>
   <artifactId>kotlin-maven-plugin</artifactId>
   <groupId>org.jetbrains.kotlin</groupId>
   <version>${kotlin.version}</version>
   <configuration>
      <jvmTarget>1.8</jvmTarget>
   </configuration>
</plugin>