Get started with Kotlin/Native using Gradle
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
Kotlin scripts in
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.
Create project files
First, create a project directory. Inside it, create
build.gradle.kts Gradle build file with the following contents:
Next, create an empty
settings.gradle.kts file in the project folder.
Depending on the target platform, different functions, such as
iosX64, are used for creating the Kotlin target. The function name is the platform for which you are compiling your code. 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:
Build the project
From the root project folder, execute the build by running
This should create a folder
build/bin/native with two subfolders
releaseExecutable with the corresponding binary. By default, the binary's name is the same as the project folder.
Open 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.