Gradle kotlin jvm target

Multiplatform Gradle DSL reference

Multiplatform projects are in Alpha. Language features and tooling may change in future Kotlin versions.

The Kotlin Multiplatform Gradle plugin is a tool for creating Kotlin Multiplatform projects. Here we provide a reference of its contents; use it as a reminder when writing Gradle build scripts for Kotlin Multiplatform projects. Learn the concepts of Kotlin Multiplatform projects, how to create and configure them.

Id and version

The fully qualified name of the Kotlin Multiplatform Gradle plugin is org.jetbrains.kotlin.multiplatform . If you use the Kotlin Gradle DSL, you can apply the plugin with kotlin(«multiplatform») . The plugin versions match the Kotlin release versions. The most recent version is 1.9.0.

Top-level blocks

kotlin is the top-level block for multiplatform project configuration in the Gradle build script. Inside kotlin , you can write the following blocks:

Declares a particular target of a project. The names of available targets are listed in the Targets section.

All targets of the project.

All predefined targets. Use this for configuring multiple predefined targets at once.

Configures predefined and declares custom source sets of the project.

Targets

Target is a part of the build responsible for compiling, testing, and packaging a piece of software aimed at one of the supported platforms. Kotlin provides target presets for each platform. See how to use a target preset.

Each target can have one or more compilations. In addition to default compilations for test and production purposes, you can create custom compilations.

The targets of a multiplatform project are described in the corresponding blocks inside kotlin , for example, jvm , android , iosArm64 . The complete list of available targets is the following:

Select the execution environment:

  • browser <> for applications running in the browser.
  • nodejs <> for applications running on Node.js.

Learn about currently supported targets for the macOS, Linux, and Windows hosts in Kotlin/Native target support.

Android applications and libraries

Manually apply an Android Gradle plugin: com.android.application or com.android.library .

Читайте также:  Вывод реального времени python

You can only create one Android target per Gradle subproject.

A target that is not supported by the current host is ignored during building and, therefore, not published.

The configuration of a target can include two parts:

Each target can have one or more compilations.

Common target configuration

In any target block, you can use the following declarations:

Attributes used for disambiguating targets for a single platform.

The preset that the target has been created from, if any.

Designates the Kotlin platform of this target. Available values: jvm , androidJvm , js , native , common .

The name of the task that builds the resulting artifacts of this target.

The components used to setup Gradle publications.

JVM targets

In addition to common target configuration, jvm targets have a specific function:

Includes Java sources into the JVM target’s compilations.

Use this function for projects that contain both Java and Kotlin source files. Note that the default source directories for Java sources don’t follow the Java plugin’s defaults. Instead, they are derived from the Kotlin source sets. For example, if the JVM target has the default name jvm , the paths are src/jvmMain/java (for production Java sources) and src/jvmTest/java for test Java sources. Learn more about Java sources in JVM compilations.

JavaScript targets

The js block describes the configuration of JavaScript targets. It can contain one of two blocks depending on the target execution environment:

Configuration of the browser target.

Configuration of the Node.js target.

Browser

browser can contain the following configuration blocks:

Configuration of test execution.

Configuration of project running.

Configuration of project bundling with Webpack.

Node.js

nodejs can contain configurations of test and run tasks:

Configuration of test execution.

Configuration of project running.

Native targets

For native targets, the following specific blocks are available:

Configuration of binaries to produce.

Binaries

There are the following kinds of binaries:

For binary configuration, the following parameters are available:

The compilation from which the binary is built. By default, test binaries are based on the test compilation while other binaries — on the main compilation.

Читайте также:  Java видимость переменных this

Options passed to a system linker during binary building.

Custom base name for the output file. The final file name will be formed by adding system-dependent prefix and postfix to this base name.

The entry point function for executable binaries. By default, it’s main() in the root package.

Access to the output file.

Access to the run task for executable binaries. For targets other than linuxX64 , macosX64 , or mingwX64 the value is null .

For Objective-C frameworks. Includes a static library instead of a dynamic one.

CInterops

cinterops is a collection of descriptions for interop with native libraries. To provide an interop with a library, add an entry to cinterops and define its parameters:

def file describing the native API.

Package prefix for the generated Kotlin API.

Options to pass to the compiler by the cinterop tool.

Directories to look for headers.

kotlin < linuxX64 < // Replace with a target you need. compilations.getByName("main") < val myInterop by cinterops.creating < // Def-file describing the native API. // The default path is src/nativeInterop/cinterop/.def defFile(project.file(«def-file.def»)) // Package to place the Kotlin API generated. packageName(«org.sample») // Options to be passed to compiler by cinterop tool. compilerOpts(«-Ipath/to/headers») // Directories for header search (an analogue of the -I compiler option). includeDirs.allHeaders(«path1», «path2») // A shortcut for includeDirs.allHeaders. includeDirs(«include/directory», «another/directory») > val anotherInterop by cinterops.creating < /* . */ >> > >

kotlin < linuxX64 < // Replace with a target you need. compilations.main < cinterops < myInterop < // Def-file describing the native API. // The default path is src/nativeInterop/cinterop/.def defFile project.file(«def-file.def») // Package to place the Kotlin API generated. packageName ‘org.sample’ // Options to be passed to compiler by cinterop tool. compilerOpts ‘-Ipath/to/headers’ // Directories for header search (an analogue of the -I compiler option). includeDirs.allHeaders(«path1», «path2») // A shortcut for includeDirs.allHeaders. includeDirs(«include/directory», «another/directory») > anotherInterop < /* . */ >> > > >

Android targets

The Kotlin Multiplatform plugin contains two specific functions for android targets. Two functions help you configure build variants:

Specifies build variants to publish. Learn more about publishing Android libraries.

Publishes all build variants.

The android configuration inside kotlin doesn’t replace the build configuration of any Android project. Learn more about writing build scripts for Android projects in Android developer documentation.

Читайте также:  Html запрет контекстного меню

Source sets

The sourceSets block describes source sets of the project. A source set contains Kotlin source files that participate in compilations together, along with their resources, dependencies, and language settings.

A multiplatform project contains predefined source sets for its targets; developers can also create custom source sets for their needs.

Predefined source sets

Predefined source sets are set up automatically upon creation of a multiplatform project. Available predefined source sets are the following:

Code and resources shared between all platforms. Available in all multiplatform projects. Used in all main compilations of a project.

Test code and resources shared between all platforms. Available in all multiplatform projects. Used in all test compilations of a project.

Target-specific sources for a compilation. is the name of a predefined target and is the name of a compilation for this target. Examples: jsTest , jvmMain .

With Kotlin Gradle DSL, the sections of predefined source sets should be marked by getting .

Custom source sets

Custom source sets are created by the project developers manually. To create a custom source set, add a section with its name inside the sourceSets section. If using Kotlin Gradle DSL, mark custom source sets by creating .

Note that a newly created source set isn’t connected to other ones. To use it in the project’s compilations, connect it with other source sets.

Source set parameters

Configurations of source sets are stored inside the corresponding blocks of sourceSets . A source set has the following parameters:

Location of Kotlin source files inside the source set directory.

Location of resources inside the source set directory.

Language settings applied to the source set.

Compilations

A target can have one or more compilations, for example, for production or testing. There are predefined compilations that are added automatically upon target creation. You can additionally create custom compilations.

To refer to all or some particular compilations of a target, use the compilations object collection. From compilations , you can refer to a compilation by its name.

Predefined compilations

Predefined compilations are created automatically for each target of a project except for Android targets. Available predefined compilations are the following:

Compilation for production sources.

Источник

Оцените статью