Kotlin Help

Get started with Kotlin/Native

In this tutorial, you'll learn how to create a Kotlin/Native application. Choose the tool that works best for you and create your app using:

  • The IDE. Here, you can clone the project template from a version control system and use it in IntelliJ IDEA.

  • The Gradle build system. To better understand how things work under the hood, create build files for your project manually.

  • The command line tool. You can use the Kotlin/Native compiler, which is shipped as a part of the standard Kotlin distribution, and create the app directly in the command line tool.

    Console compilation may seem easy and straightforward, but it doesn't scale well for larger projects with hundreds of files and libraries. For such projects, we recommend using an IDE or a build system.

With Kotlin/Native, you can compile for different targets, including Linux, macOS, and Windows. While cross-platform compilation is possible, which means using one platform to compile for a different one, in this tutorial, you'll be targeting the same platform you're compiling on.

In IDE

In this section, you'll learn how to use IntelliJ IDEA to create a Kotlin/Native application. You can use both the Community Edition and the Ultimate Edition.

Create the project

  1. Download and install the latest version of IntelliJ IDEA.

  2. Clone the project template by selecting File | New | Project from Version Control in IntelliJ IDEA and using this URL:

    https://github.com/Kotlin/kmp-native-wizard
  3. Open the gradle/libs.versions.toml file, which is the version catalog for project dependencies. To create Kotlin/Native applications, you need the Kotlin Multiplatform Gradle plugin, which has the same version as Kotlin. Ensure that you use the latest Kotlin version:

    [versions] kotlin = "2.1.0"
  4. Follow the suggestion to reload Gradle files:

    Load Gradle changes button

For more information about these settings, see the Multiplatform Gradle DSL reference.

Build and run the application

Open the Main.kt file in the src/nativeMain/kotlin/ directory:

  • The src directory contains Kotlin source files.

  • The Main.kt file includes code that prints "Hello, Kotlin/Native!" using the println() function.

Press the green icon in the gutter to run the code:

Run the application

IntelliJ IDEA runs the code using the Gradle task and outputs the result in the Run tab:

Application output

After the first run, the IDE creates the corresponding run configuration at the top:

Gradle run configuration

You can configure IntelliJ IDEA to build your project automatically:

  1. Go to Settings | Build, Execution, Deployment | Compiler.

  2. On the Compiler page, select Build project automatically.

  3. Apply the changes.

Now, when you make changes in the class files or save the file (Ctrl + S/Cmd + S), IntelliJ IDEA automatically performs an incremental build of the project.

Update the application

Let's add a feature to your application so it can count the number of letters in your name:

  1. In the Main.kt file, add code to read the input. Use the readln() function to read the input value and assign it to the name variable:

    fun main() { // Read the input value. println("Hello, enter your name:") val name = readln() }
  2. To run this app using Gradle, specify System.in as the input to use in the build.gradle.kts file and load the Gradle changes:

    kotlin { //... nativeTarget.apply { binaries { executable { entryPoint = "main" runTask?.standardInput = System.`in` } } } //... }
  3. Eliminate the whitespaces and count the letters:

    • Use the replace() function to remove the empty spaces in the name.

    • Use the scope function let to run the function within the object context.

    • Use a string template to insert your name length into the string by adding a dollar sign $ and enclosing it in curly braces – ${it.length}. it is the default name of a lambda parameter.

    fun main() { // Read the input value. println("Hello, enter your name:") val name = readln() // Count the letters in the name. name.replace(" ", "").let { println("Your name contains ${it.length} letters") } }
  4. Run the application.

  5. Enter your name and enjoy the result:

    Application output

Now let's count only the unique letters in your name:

  1. In the Main.kt file, declare the new extension function .countDistinctCharacters() for String:

    • Convert the name to lowercase using the .lowercase() function.

    • Convert the input string to a list of characters using the toList() function.

    • Select only the distinct characters in your name using the distinct() function.

    • Count the distinct characters using the count() function.

    fun String.countDistinctCharacters() = lowercase().toList().distinct().count()
  2. Use the .countDistinctCharacters() function to count the unique letters in your name:

    fun String.countDistinctCharacters() = lowercase().toList().distinct().count() fun main() { // Read the input value. println("Hello, enter your name:") val name = readln() // Count the letters in the name. name.replace(" ", "").let { println("Your name contains ${it.length} letters") // Print the number of unique letters. println("Your name contains ${it.countDistinctCharacters()} unique letters") } }
  3. Run the application.

  4. Enter your name and see the result:

    Application output

Using Gradle

In this section, you'll learn how to manually create a Kotlin/Native application using Gradle. It's the default build system for Kotlin/Native and Kotlin Multiplatform projects, which is also commonly used in Java, Android, and other ecosystems.

Create project files

  1. To get started, install a compatible version of Gradle. See the compatibility table to check the Kotlin Gradle plugin (KGP) compatibility with available Gradle versions.

  2. Create an empty project directory. Inside it, create a build.gradle(.kts) file with the following content:

    // build.gradle.kts plugins { kotlin("multiplatform") version "2.1.0" } repositories { mavenCentral() } kotlin { macosArm64("native") { // on macOS // linuxArm64("native") // on Linux // mingwX64("native") // on Windows binaries { executable() } } } tasks.withType<Wrapper> { gradleVersion = "8.10" distributionType = Wrapper.DistributionType.BIN }
    // build.gradle plugins { id 'org.jetbrains.kotlin.multiplatform' version '2.1.0' } repositories { mavenCentral() } kotlin { macosArm64('native') { // on macOS // linuxArm64('native') // on Linux // mingwX64('native') // on Windows binaries { executable() } } } wrapper { gradleVersion = '8.10' distributionType = 'BIN' }

    You can use different target names, such as macosArm64, iosArm64 linuxArm64, and mingwX64 to define the targets for which you are compiling your code. These target names can optionally take the platform name as a parameter, which in this case is native. The platform name is used to generate the source paths and task names in the project.

  3. Create an empty settings.gradle(.kts) file in the project directory.

  4. Create a src/nativeMain/kotlin directory and place a hello.kt file inside 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, 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.

Build and run the project

  1. From the root project directory, run the build command:

    ./gradlew nativeBinaries

    This command creates the build/bin/native directory with two directories inside: debugExecutable and releaseExecutable. They contain the corresponding binary files.

    By default, the name of the binary file is the same as the project directory.

  2. To run the project, execute the following command:

    build/bin/native/debugExecutable/<project_name>.kexe

The terminal prints "Hello, Kotlin/Native!".

Open the project in IDE

Now, you can open your project in any IDE that supports Gradle. If you use IntelliJ IDEA:

  1. Select File | Open.

  2. Select the project directory and click Open. IntelliJ IDEA automatically detects if it's a Kotlin/Native project.

If you encounter a problem with the project, IntelliJ IDEA displays the error message in the Build tab.

Using the command-line compiler

In this section, you'll learn how to create a Kotlin/Native application using the Kotlin compiler in the command line tool.

Download and install the compiler

To install the compiler:

  1. Go to the Kotlin's GitHub releases page.

  2. Look for a file with kotlin-native in the name and download one that is suitable for your operating system, for example kotlin-native-prebuilt-linux-x86_64-2.0.21.tar.gz.

  3. Unpack the archive to a directory of your choice.

  4. Open your shell profile and add the path to the compiler's /bin directory to the PATH environment variable:

    export PATH="/<path to the compiler>/kotlin-native/bin:$PATH"

Create the program

Choose a working directory and create a file named hello.kt. Update it with the following code:

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

Compile the code from the console

To compile the application, execute the following command with the downloaded compiler:

kotlinc-native hello.kt -o hello

The value of the -o option specifies the name of the output file, so this call generates the hello.kexe binary file on macOS and Linux (and hello.exe on Windows).

For the full list of available options, see Kotlin compiler options.

Run the program

To run the program, in your command line tool, navigate to the directory containing the binary file and run the following command:

./hello.kexe
./hello.exe

The application prints "Hello, Kotlin/Native" to the standard output.

What's next?

Last modified: 16 December 2024