Kotlin/Native 入门——在 IntelliJ IDEA 中

This tutorial demonstrates how to use IntelliJ IDEA for creating a Kotlin/Native application.

To get started, install the latest version of IntelliJ IDEA. The tutorial is applicable to both IntelliJ IDEA Community Edition and the Ultimate Edition.

Create a new Kotlin/Native project in IntelliJ IDEA

  1. In IntelliJ IDEA, select File | New | Project.
  2. In the panel on the left, select Kotlin Multiplatform.
  3. Enter a project name, select Native Application as the project template, and click Next. By default, your project will use Gradle with Kotlin DSL as the build system.

    Kotlin/Native doesn’t support Maven and IntelliJ IDEA native builder.

    Kotlin/Native 入门——在 IntelliJ IDEA 中 - 图1

  4. Accept the default configuration on the next screen and click Finish. Your project will open.

    Configure a native application

    By default, the wizard creates the necessary Main.kt file with code that prints “Hello, Kotlin/Native!” to the standard output.

  5. Open the build.gradle.kts file, the build script that contains the project settings. To create Kotlin/Native applications, you need the Kotlin Multiplatform Gradle plugin installed. Ensure that you use the latest version of the plugin:

    1. plugins {
    2. kotlin("multiplatform") version "1.9.10"
    3. }

Kotlin/Native 入门——在 IntelliJ IDEA 中 - 图3

Build and run the application

  1. Click Build Project next to the run configuration at the top of the screen:

    Build the application

  2. In the IntelliJ IDEA terminal or your command-line tool, run the following command:

    1. build/bin/native/debugExecutable/<your_app_name>.kexe

    IntelliJ IDEA prints “Hello, Kotlin/Native!”.

You can configure IntelliJ IDEA to build your project automatically:

  1. Go to Settings/Preferences | 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 the incremental build of the project.

Update the application

Count the letters in your name

  1. Open the file Main.kt in src/nativeMain/kotlin.

    The src directory contains the Kotlin source files and resources. The file Main.kt includes sample code that prints “Hello, Kotlin/Native!” using the println() function.

  2. Add code to read the input. Use the readln() function to read the input value and assign it to the name variable:

    1. fun main() {
    2. // Read the input value.
    3. println("Hello, enter your name:")
    4. val name = readln()
    5. }
  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.
    1. fun main() {
    2. // Read the input value.
    3. println("Hello, enter your name:")
    4. val name = readln()
    5. // Count the letters in the name.
    6. name.replace(" ", "").let {
    7. println("Your name contains ${it.length} letters")
    8. }
    9. }
  4. Save the changes and run the following command in the IntelliJ IDEA terminal or your command-line tool:

    1. build/bin/native/debugExecutable/<your_app_name>.kexe
  5. Enter your name and enjoy the result:

    Application output

Count the unique letters in your name

  1. Open the file Main.kt in src/nativeMain/kotlin.

  2. 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.
    1. fun String.countDistinctCharacters() = lowercase().toList().distinct().count()
  3. Use the countDistinctCharacters() function to count the unique letters in your name:

    1. fun String.countDistinctCharacters() = lowercase().toList().distinct().count()
    2. fun main() {
    3. // Read the input value.
    4. println("Hello, enter your name:")
    5. val name = readln()
    6. // Count the letters in the name.
    7. name.replace(" ", "").let {
    8. println("Your name contains ${it.length} letters")
    9. // Print the number of unique letters.
    10. println("Your name contains ${it.countDistinctCharacters()} unique letters")
    11. }
    12. }
  4. Save the changes and run the following command in the IntelliJ IDEA terminal or your command-line tool:

    1. build/bin/native/debugExecutable/<your_app_name>.kexe
  5. Enter your name and enjoy the result:

    Application output

下一步做什么?

Once you have created your first application, you can complete our long-form tutorial on Kotlin/Native, Create an app using C Interop and libcurl that explains how to create a native HTTP client and interoperate with C libraries.