了解项目

Discover main parts of your multiplatform project:

多平台插件

When you create a multiplatform project, the Project Wizard automatically applies the kotlin-multiplatform Gradle plugin in the file build.gradle(.kts).

You can also apply it manually.

The kotlin-multiplatform plugin works with Gradle 6.0 or later.

  1. plugins {
  2. id 'org.jetbrains.kotlin.multiplatform' version '1.4.20'
  3. }
  1. plugins {
  2. kotlin("multiplatform") version "1.4.20"
  3. }

The kotlin-multiplatform plugin configures the project for creating an application or library to work on multiple platforms and prepares it for building on these platforms.

In the file build.gradle(.kts), it creates the kotlin extension at the top level, which includes configuration for targets, source sets, and dependencies.

目标

A multiplatform project is aimed at multiple platforms that are represented by different targets. A target is part of the build that is responsible for building, testing, and packaging the application for a specific platform, such as macOS, iOS, or Android. See the list of supported platforms.

When you create a multiplatform project, targets are added to the kotlin block in the file build.gradle (build.gradle.kts).

  1. kotlin {
  2. jvm()
  3. js {
  4. browser {}
  5. }
  6. }

Learn how to set up targets manually.

源集

The project includes the directory src with Kotlin source sets, which are collections of Kotlin code files, along with their resources, dependencies, and language settings. A source set can be used in Kotlin compilations for one or more target platforms.

Each source set directory includes Kotlin code files (the kotlin directory) and resources. The Project Wizard creates default source sets for the main and test compilations of the common code and all added targets.

Source sets

Source set names are case sensitive.

Source sets are added to the sourceSets block of the top-level kotlin block.

  1. kotlin {
  2. sourceSets {
  3. commonMain { /* ... */}
  4. commonTest { /* ... */}
  5. jvmMain { /* ... */}
  6. jvmTest { /* ... */ }
  7. jsMain { /* ... */}
  8. jsTest { /* ... */}
  9. }
  10. }
  1. kotlin {
  2. sourceSets {
  3. val commonMain by getting { /* ... */ }
  4. val commonTest by getting { /* ... */ }
  5. val jvmMain by getting { /* ... */ }
  6. val jvmTest by getting { /* ... */ }
  7. val jsMain by getting { /* ... */ }
  8. val jsTest by getting { /* ... */ }
  9. }
  10. }

Source sets form a hierarchy, which is used for sharing the common code. In a source set shared among several targets, you can use the platform-specific language features and dependencies that are available for all these targets.

For example, all Kotlin/Native features are available in the desktopMain source set, which targets the Linux (linuxX64), Windows (mingwX64), and macOS (macosX64) platforms.

Hierarchical structure

Learn how to build the hierarchy of source sets.

编译项

Each target can have one or more compilations, for example, for production and test purposes.

For each target, default compilations include:

  • main and test compilations for JVM, JS, and Native targets.
  • A compilation per Android build variant, for Android targets.

Compilations

Each compilation has a default source set, which contains sources and dependencies specific to that compilation.

Learn how to configure compilations.