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 IDEs, including IntelliJ IDEA, can generate necessary Gradle files, this tutorial covers how to create them manually to provide a better understanding of how things work under the hood.
To get started, install the latest version of Gradle.
Create project files
Create a project directory. Inside it, create
build.gradle(.kts)
Gradle build file with the following content:// build.gradle.kts plugins { kotlin("multiplatform") version "2.0.21" } repositories { mavenCentral() } kotlin { macosX64("native") { // on macOS // linuxX64("native") // on Linux // mingwX64("native") // on Windows binaries { executable() } } } tasks.withType<Wrapper> { gradleVersion = "8.5" distributionType = Wrapper.DistributionType.BIN }// build.gradle plugins { id 'org.jetbrains.kotlin.multiplatform' version '2.0.21' } repositories { mavenCentral() } kotlin { macosX64('native') { // on macOS // linuxX64('native') // on Linux // mingwX64('native') // on Windows binaries { executable() } } } wrapper { gradleVersion = '8.5' distributionType = 'BIN' }You can use different target presets, such as
macosX64
,mingwX64
,linuxX64
,iosX64
, to define the corresponding target platform. The preset name describes a platform for which you are compiling your code. These target presets optionally take the target name as a parameter, which isnative
in this case. The target name is used to generate the source paths and task names in the project.Create an empty
settings.gradle
orsettings.gradle.kts
file in the project directory.Create a directory
src/nativeMain/kotlin
and place inside thehello.kt
file with the following content:fun main() { println("Hello, Kotlin/Native!") }By convention, all sources are located in the
src/<target name>[Main|Test]/kotlin
directories, wheremain
is for the source code andtest
is for tests.<target name>
corresponds to the target platform (in this casenative
), as specified in the build file.
Now you are ready to build your project and run the application.
Build and run the application
From the root project directory, run the build command:
gradle nativeBinariesThis command creates the
build/bin/native
directory with two directories inside:debugExecutable
andreleaseExecutable
. They contain corresponding binary files.By default, the name of the binary file is the same as the project directory.
To run the project, execute the following command:
build/bin/native/debugExecutable/<project_name>.kexeTerminal prints "Hello, Kotlin/Native!".
Open the project in an IDE
Now you can open your project in any IDE that supports Gradle. If you use IntelliJ IDEA:
Select File | Open....
Select the project directory and click Open.
IntelliJ IDEA will automatically detect it as Kotlin/Native project.
What's next?
Learn how to write Gradle build scripts for real-life Kotlin/Native projects.