Get started with Kotlin/Native in 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.
Before you start
Download and install the latest version of IntelliJ IDEA with the latest Kotlin plugin.
Clone the project template by selecting File | New | Project from Version Control in IntelliJ IDEA.
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:plugins { kotlin("multiplatform") version "2.0.21" }Follow the suggestion to reload Gradle files:
Build and run the application
Open the Main.kt
file in the src/nativeMain/kotlin/
directory, then press the green icon in the gutter to run the code:
IntelliJ IDEA runs the code using the Gradle task. You will see the result in the Run tab:
After the first run, you will see the corresponding run configuration on the top bar in the IDE:
You can configure IntelliJ IDEA to build your project automatically:
Go to Settings/Preferences | Build, Execution, Deployment | Compiler.
On the Compiler page, select Build project automatically.
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
Count the letters in your name
Open the file
Main.kt
insrc/nativeMain/kotlin
.The
src
directory contains Kotlin source files. TheMain.kt
file includes code that prints "Hello, Kotlin/Native!" using theprintln()
function.Add code to read the input. Use the
readln()
function to read the input value and assign it to thename
variable:fun main() { // Read the input value. println("Hello, enter your name:") val name = readln() }To run this app using Gradle, specify
System.in
as the input to use in thebuild.gradle.kts
file and load the Gradle changes:kotlin { //... nativeTarget.apply { binaries { executable { entryPoint = "main" runTask?.standardInput = System.`in` } } } //... }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") } }Run the application.
Enter your name and enjoy the result:
Count the unique letters in your name
Open the file
Main.kt
insrc/nativeMain/kotlin
.Declare the new extension function
countDistinctCharacters()
forString
: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()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") } }Run the application.
Enter your name and enjoy the result:
What's next?
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.