Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Programming Kotlin

You're reading from   Programming Kotlin Get to grips quickly with the best Java alternative

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781787126367
Length 420 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Stefan Bocutiu Stefan Bocutiu
Author Profile Icon Stefan Bocutiu
Stefan Bocutiu
Stephen Samuel Stephen Samuel
Author Profile Icon Stephen Samuel
Stephen Samuel
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with Kotlin FREE CHAPTER 2. Kotlin Basics 3. Object-Oriented Programming in Kotlin 4. Functions in Kotlin 5. Higher Order Functions and Functional Programming 6. Properties 7. Null Safety, Reflection, and Annotations 8. Generics 9. Data Classes 10. Collections 11. Testing in Kotlin 12. Microservices with Kotlin 13. Concurrency

Kotlin with Gradle

If you are familiar with the build tool landscape, you might be in one of three camps: Maven, Gradle, or SBT (more likely if you are a Scala dev). I am not going to go into the details, but we will present the basics of Gradle, the modern open source polyglot build automation system, and leave it up to the curious to find out more from http://gradle.org. Before we continue, please make sure you have it installed and available in your classpath in order to be accessible from the terminal. If you have SDKMAN, you can install it using this command:

$ sdk install gradle 3.0

The build system comes with some baked-in templates, although limited, and in its latest 3.0 version Kotlin is not yet included. Hopefully, this shortfall will be dealt with sooner rather than later. However, it takes very little to configure support for it. First, let's see how you can interrogate for the available templates:

$ gradle help --task :init

You should see the following being printed out on the terminal:

Options
--type  Set type of build to create.
Available values are:
basic
groovy-library
java-library
pom
scala-library

Let's go and use the Java template and create our project structure by executing this bash command:

$ gradle init --type java-library

This template will generate a bunch of files and folders; if you have been using Maven, you will see that this structure is similar:

Kotlin with Gradle

Project Folders layout

As it stands, the Gradle project is not ready for Kotlin. First, go ahead and delete Library.java and LibraryTest.java, and create a new folder named kotlin, a sibling of the java one. Then, using a text editor, open the build.gradle file. We need to add the plugin enabling the Gradle system to compile Kotlin code for us, so at the top of your file you have to add the following snippet:

    buildscript {
      ext.kotlin_version = '1.1-M04'

      repositories {
        maven { url "https://dl.bintray.com/kotlin/kotlin-dev" }
        mavenCentral()
      }
      dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
      }
    }

The preceding instructions tell Gradle to use the plugin for Kotlin, and set the dependency maven repository. Since Kotlin 1.1 is only at milestone 4, there is a specific repository to pick it from. See last entry in repositories. We are not done yet; we still need to enable the plugin. The template generated will already have an applied plugin: java. Replace it with the following:

    apply plugin: 'kotlin' 
    apply plugin: 'application' 
    mainClassName = 'com.programming.kotlin.chapter01.ProgramKt' 

Now Kotlin plugin support is enabled; you may have noticed that we have also added the application plugin, and set the class containing the program entry point. The reason for this is to allow the program to run directly, as we will see shortly.

We are not quite done. We still need to link to the Kotlin standard library. Replace the repositories and dependencies sections with the following:

    repositories {
      maven { url "https://dl.bintray.com/kotlin/kotlin-dev" }
      mavenCentral()
    } 
    dependencies { 
      compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
      testCompile 'io.kotlintest:kotlintest:1.3.3' 
    } 

Now let's create the file named HelloWorld.Kt. This time, we will set a namespace and thus avoid having our class as part of the default one. If you are not yet familiar with the term, don't worry; it will be covered in the next chapter.

From the terminal, run the following:

$ mkdir -p src/main/kotlin/com/programming/kotlin/chapter01
$ echo "" >> src/main/kotlin/com/programming/kotlin/chapter01/Program.kt
$ cat <<EOF >> src/main/kotlin/com/programming/kotlin/chapter01/Program.kt
package com.programming.kotlin.chapter01
fun main(args: Array<String>) {
  println("Hello World!")
}

We are now in a position to build and run the application:

$ gradle build
$ gradle run

Now we want to be able to run our program using java -jar [artefact]. Before we can do that, we need to adapt the build.gradle. First, we need to create a manifest file and set the main class; the JVM will look for the main function to start executing it:

    jar { 
      manifest { 
        attributes( 
          'Main-Class': 'com.programming.kotlin.chapter01.ProgramKt' 
        ) 
      } 
      from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } 
    } 

Furthermore, we also embed into the JAR the dependency for kotlin-stdlib, as well as kotlin-runtime. If we leave out these dependencies, we will need to add them to the classpath  when we run the application. Now you are ready to build and run the code.

You have been reading a chapter from
Programming Kotlin
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781787126367
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image