News

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

Multi-platform Projects in Kotlin

What are multiplatform projects?

Kotlin 1.2 introduces a new experimental capability called multiplatform projects. All of the languages features detailed here may change in future Kotlin versions. The multiplatform projects in Kotlin allow you to compile the same code for many target platforms. JVM and JS have currently supported target platforms, with Native support coming in the future.

Structure of a multi-platform project

A multi-platform project is made up of three kinds of modules:

The general module contains code that is not platform-specific and advertisements for use in platform-specific APIs. These declarations make it possible for standard code to be used as a dependency for platform-specific implementations.

Platform-specific implementations of platform-specific advertising from the general module and other platform code are found in the platform module. A platform module is always a subset of a single standard module.

The standard module. Such modules are based on a particular platform and can either be platform modules’ dependencies or depend on them.

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

A platform module can rely on any platform module or library (including Java libraries for Kotlin/JVM and JavaScript libraries for Kotlin/JS). Platform modules for Kotlin/JVM can also include Java and other JVM-specific functionality.

The consequence of building a shared module is a specific metadata file containing all of the module’s declarations. The code for the given platform (JVM bytecode or JS source code) for the Kotlin source code in both the venue and the implemented shared module results from compiling a platform module. As a result, each multiplatform library must be released as a collection of artifacts, including a.jar with metadata for shared code and several.jar for each platform module.

Preparing a multi-platform project

Kotlin 1.2 only supports Gradle for developing multiplatform projects; other build systems are not supported.

To start a new multiplatform project in the IDE, enter the project creation window and choose “Kotlin (Multiplatform)” from the “Kotlin” tab. The IDE will generate a project with three modules: General, JVM, and JS platform modules. To add additional modules, enter the module creation window and choose one of the “Kotlin (Multiplatform)” options on 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 critical characteristics of the Kotlin multiplatform code is a method for organizing standard code’s reliance on platform code. Other languages employ writing a set of interfaces in standard code and implementing them in platform code. This method, however, is not ideal if you have a library for the target platform that contains the functionality you require and wish to utilize the API of that library directly, without the usage of additional wrappers. You will also be required to respond to any general announcements in the form of interfaces, which is not always necessary.


.

You may also like...