W3cubDocs

/Kotlin

Hello Kotlin/Native using Gradle

Last Updated 15 January 2020
A look at how to compile Kotlin/Native applications using Gradle

Creating a Kotlin/Native Gradle project

Gradle is a build system that is very commonly used in the Java, Android, and other ecosystems. It is the default choice for Kotlin/Native and Multiplatform when it comes to build systems.

While most IDE's including IntelliJ IDEA can generate the corresponding Gradle file, we're going to take a look at how to create this manually, to have a better understanding of how things work under the covers. If you'd like to use the IDE, check out Using IntelliJ IDEA.

Gradle supports two languages for build scripts:

  • Groovy scripts in build.gradle files
  • Kotlin scripts in build.gradle.kts files

The Groovy language is the first supported scripting language for Gradle, it leverages the power of dynamic typing and runtime features of the language. It is also possible to use Kotlin in Gradle scripts. Being a statically-typed language, it plays better with IDEs when it comes to compilation and error detection.

Either can be used and samples will show the syntax for both languages.

First step is to create a project folder. Inside it we create build.gradle build.gradle.kts Gradle build file with the following contents:

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.21'
}

repositories {
    mavenCentral()
}

kotlin {
  macosX64("native") {
    binaries {
      executable()
    }
  }
}

wrapper {
  gradleVersion = "5.3.1"
  distributionType = "ALL"
}
plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.21'
}

repositories {
    mavenCentral()
}

kotlin {
  linuxX64("native") {
    binaries {
      executable()
    }
  }
}

wrapper {
  gradleVersion = "5.3.1"
  distributionType = "ALL"
}
plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.21'
}

repositories {
    mavenCentral()
}

kotlin {
  mingwX64("native") {
    binaries {
      executable()
    }
  }
}

wrapper {
  gradleVersion = "5.3.1"
  distributionType = "ALL"
}
plugins {
    kotlin("multiplatform") version "1.3.21"
}

repositories {
    mavenCentral()
}

kotlin {
  macosX64("native") {
    binaries {
      executable()
    }
  }
}

tasks.withType<Wrapper> {
  gradleVersion = "5.3.1"
  distributionType = Wrapper.DistributionType.ALL
}
plugins {
    kotlin("multiplatform") version "1.3.21"
}

repositories {
    mavenCentral()
}

kotlin {
  linuxX64("native") {
    binaries {
      executable()
    }
  }
}

tasks.withType<Wrapper> {
  gradleVersion = "5.3.1"
  distributionType = Wrapper.DistributionType.ALL
}
plugins {
    kotlin("multiplatform") version "1.3.21"
}

repositories {
    mavenCentral()
}

kotlin {
  mingwX64("native") {
    binaries {
      executable()
    }
  }
}

tasks.withType<Wrapper> {
  gradleVersion = "5.3.1"
  distributionType = Wrapper.DistributionType.ALL
}

The prepared project sources can be directly downloaded from GitHub. GitHub. GitHub. GitHub. GitHub. GitHub.

Next create an empty settings.gradle.kts settings.gradle file in the project folder.

Depending on the target platform, different functions, e.g. macosX64, mingwX64, linuxX64, iosX64, are used to create the Kotlin target. The function name is the platform which we are compiling our code for. These functions optionally take the target name as a parameter, which is "native" in our case. The specified target name is used to generate the source paths and task names in the project.

By convention, all sources are located in the src/<target name>[Main|Test]/kotlin folders, where main is for the source code and test is for tests. <target name> corresponds to the target platform (in this case native), as specified in the build file.

Create a folder src/nativeMain/kotlin and inside it place the file hello.kt with the following contents:

fun main() {
  println("Hello Kotlin/Native!")
}

Building the project

From the root project folder, execute the build by running

gradle nativeBinaries

This should create a folder build/native/bin with two subfolders debugExecutable and releaseExecutable with the corresponding binary. By default, the binary is named the same as the project folder.

Opening the project in an IDE

Any IDE that supports Gradle should allow for opening the project in the IDE. In the case of IntelliJ IDEA, just open the project folder, and it will automatically detect it as Kotlin/Native project.

What's next?

Learn about multiplatform projects.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/tutorials/native/using-gradle.html