Multi-platform Projects in Kotlin – Everything You Need to Know

Multi-platform Projects in Kotlin

What are multiplatform projects?

Multiplatform projects are a new experimental feature of Kotlin 1.2. All the language features described here may change in future versions of Kotlin. Kotlin’s multi-platform projects allow you to compile the same code for many target platforms. Currently supported target platforms are JVM and JS, with Native added in the future.

Structure of a multi-platform project

A multi-platform project consists of three types of modules:

The general module contains of a code that is not specific to any particular platform, as well as ads for implementation in platform-dependent APIs. These declarations allow common code to be a dependency for platform-specific implementations.

The platform module contains platform-specific implementations of platform-specific ads from the general module and other platform code. A platform module is always an implementation of a single shared module.

The regular module. Such modules are based on a specific platform and can either be a dependency of platform modules or depend on them.

A shared module can only depend on other shared modules and libraries, including the shared version of the Kotlin standard library (kotlin-stdlib-common). Shared modules contain only Kotlin code and no other languages.

A platform module can depend on any modules and libraries for a given platform (including Java libraries for Kotlin/JVM and JavaScript libraries for Kotlin/JS). Platform modules for Kotlin/JVM can also contain code in Java and other languages for the JVM.

The result of compiling a shared module is a special metadata file containing all the declarations in that module. The result of compiling a platform module is the code for the specified platform (JVM bytecode or JS source code) for the Kotlin source code in both the platform and the implemented shared module. Therefore, each multiplatform library must be distributed as a set of artifacts – .jar with metadata for shared code and several .jar for each platform module.

Preparing a multi-platform project

Kotlin 1.2 supports building multi-platform projects only with Gradle; other build systems are not supported.

To create a new multiplatform project in the IDE, open the project creation window and select the “Kotlin (Multiplatform)” option in the “Kotlin”tab. The IDE will create a project with three modules: General and two platform modules for JVM and JS. To add extra modules, open the module creation window and select one of the “Kotlin (Multiplatform)” options in the “Gradle”tab.

If you need to set up the project manually:

  1. Add the Kotlin plugin for Gradle to the classpath of the build script: classpath ” org. jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”
  2. Apply the kotlin-platform-common plugin to the shared module
  3. Add the kotlin-stdlib-common dependency to the shared module
  4. Apply the kotlin-platform-jvm and kotlin-platform-js plugins for the corresponding platform modules
  5. Add dependencies with the expectedBy scope from platform modules to the general one

Platform-dependent ads

One of the key features of the Kotlin multiplatform code is a way to organize the dependence of common code on the platform code. Other languages use the method of creating a set of interfaces in common code and implementing them in platform code. However, this approach is not perfect if you have a library for the target platform that has the functionality you need, and you want to use the API of this library directly, without additional wrappers. Also, you will be forced to submit to all general announcements in the form of interfaces, which is not always applicable.


.

Recommended Articles

Share
Tweet
Pin
Share
Share