Kotlin/Native 入门——使用 Gradle

Gradle 是一个在 Java、Android 与其他生态系统项目中非常常用的构建系统。 它是 Kotlin/Native 与 Multiplatform 的默认构建系统。

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.

If you would like to use an IDE, check out the Using IntelliJ IDEA tutorial.

Kotlin/Native 入门——使用 Gradle - 图1

Create project files

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

【Kotlin】

  1. // build.gradle.kts
  2. plugins {
  3. kotlin("multiplatform") version "1.9.10"
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. kotlin {
  9. macosX64("native") { // on macOS
  10. // linuxX64("native") // on Linux
  11. // mingwX64("native") // on Windows
  12. binaries {
  13. executable()
  14. }
  15. }
  16. }
  17. tasks.withType<Wrapper> {
  18. gradleVersion = "7.6"
  19. distributionType = Wrapper.DistributionType.BIN
  20. }

【Groovy】

  1. // build.gradle
  2. plugins {
  3. id 'org.jetbrains.kotlin.multiplatform' version '1.9.10'
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. kotlin {
  9. macosX64('native') { // on macOS
  10. // linuxX64('native') // on Linux
  11. // mingwX64('native') // on Windows
  12. binaries {
  13. executable()
  14. }
  15. }
  16. }
  17. wrapper {
  18. gradleVersion = '7.6'
  19. distributionType = 'BIN'
  20. }

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 is native in this case. The target name is used to generate the source paths and task names in the project.

  1. Create an empty settings.gradle or settings.gradle.kts file in the project directory.

  2. Create a directory src/nativeMain/kotlin and place inside the hello.kt file with the following content:

    1. fun main() {
    2. println("Hello, Kotlin/Native!")
    3. }

    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.

Now you are ready to build your project and run the application.

Build and run the application

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

    1. gradle nativeBinaries

    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.

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

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

    Terminal prints “Hello, Kotlin/Native!”.

在 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 will automatically detect it as Kotlin/Native project.

If you face any problem with the project, IntelliJ IDEA will show the error message in the Build tab.

Kotlin/Native 入门——使用 Gradle - 图2

下一步做什么?

Learn how to write Gradle build scripts for real-life Kotlin/Native projects.