K/N project setup
In order to start writing a GTK application in Kotlin/Native, you need to know first how to setup a generic Kotlin/Native project. If you are familiar with the topic, you can jump immediately to the Hello World! section.
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.
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 "1.8.10" } kotlin { linuxX64("native") { binaries { executable() } } sourceSets { val nativeMain by getting { dependencies { // Dependencies go here } } } } tasks.withType<Wrapper> { description = "Regenerates the Gradle Wrapper files" distributionType = Wrapper.DistributionType.ALL gradleVersion = "7.6" }
-
Create a
settings.gradle.kts
file in the project directory with the following content: - Create a directory
src/nativeMain/kotlin
and place inside anhello.kt
file with the following content: By convention, all sources are located in thesrc/<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, but first we need to get Gradle.
Downloading the Gradle Wrapper¶
The Gradle Wrapper is a script that allows us to download and run a specific version of Gradle. It is particularly useful for ensuring that our project uses a consistent version of Gradle, regardless of the environment in which it is built.
By using the Gradle Wrapper, we avoid the need to install Gradle globally on our system, which can cause compatibility issues between different versions of Gradle and different operating systems. The Gradle Wrapper ensures that everyone working on the project is using the same version of Gradle, and that the build process is consistent across different environments.
Overall, using the Gradle Wrapper is a best practice for managing dependencies and ensuring that our build process is reliable and consistent.
To download the Gradle Wrapper, we can install the Gradle version provided by our distribution. For example, on Ubuntu we can use the following command:
Once Gradle is installed, we can execute the following command, from withing the directory of our project, to download the Gradle Wrapper:
Once the Gradle Wrapper is downloaded, we can uninstall Gradle using the following commands:
This will remove the Gradle binary from our system, but we can still use the Gradle Wrapper to build our project.
Build and run the application¶
- From the root project directory, run the build command:
This command creates the build/bin/native
directory with two directories inside: debugExecutable
and releaseExecutable
. 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:
The terminal should print “Hello K/N!”.
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.
Note
If you face any problem with the project, IntelliJ IDEA will show the error message in the Build tab.