<commit_before>// Copyright 2018 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[kotlin_dsl]]
= Gradle Kotlin DSL


[TIP]
====
If you are looking into migrating an existing build to the Gradle Kotlin DSL, please check out the link:https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/[migration guide].
====


[[sec:kotlin-dsl_plugin]]
== The `kotlin-dsl` plugin

When applied to a project, the `kotlin-dsl` Gradle plugin configures the project's Kotlin source sets to enable Gradle Kotlin DSL features.

It is useful for all projects implementing build logic: <<organizing_gradle_projects.adoc#sec:build_sources, buildSrc>>, <<composite_builds.adoc, included builds>> for build logic or link:{guidesUrl}/designing-gradle-plugins/[Gradle plugin] builds.

The `kotlin-dsl` plugin can be applied as follows:

[source,kotlin]
.build.gradle.kts
----
plugins {
    `kotlin-dsl`
}
----

The `kotlin-dsl` plugin

* applies the link:https://kotlinlang.org/docs/reference/using-gradle.html#targeting-the-jvm[org.jetbrains.kotlin.jvm] plugin,
* configures an embedded repository that contains all Kotlin libraries embedded with the Gradle Kotlin DSL, pins them to the embedded Kotlin version,
* adds the `gradleKotlinDsl()` dependency to the `compileOnly` and `testImplementation` configurations,
* and, configures the Kotlin compiler with the same set of Kotlin compiler settings that is used for Gradle Kotlin DSL scripts.

[CAUTION]
====
The `kotlin-dsl` plugin enables experimental Kotlin compiler features.
See the <<sec:kotlin_compiler_arguments>> section below for more information.
====

[[sec:kotlin_compiler_arguments]]
== Kotlin compiler arguments

These are the Kotlin compiler arguments used for compiling Gradle Kotlin DSL scripts and Kotlin sources and scripts in a project that has the `kotlin-dsl` plugin applied:

`-jvm-target=1.8`::
Sets the target version of the generated JVM bytecode to `1.8`.

`-Xjsr305=strict`::
Sets up Java interoperability to strictly follow JSR-305 annotations for more null safety.
See link:https://kotlinlang.org/docs/reference/java-interop.html#compiler-configuration[Calling Java code from Kotlin] in the Kotlin documentation for more information.

`-XX:NewInference`::
Enable the experimental Kotlin compiler inference engine

`-XX:SamConversionForKotlinFunctions`::
Enable SAM conversion for Kotlin functions<commit_msg>Refine kotlin_dsl.adoc<commit_after>// Copyright 2018 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[kotlin_dsl]]
= Gradle Kotlin DSL


[TIP]
====
If you are looking into migrating an existing build to the Gradle Kotlin DSL, please check out the link:https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/[migration guide].
====


[[sec:kotlin-dsl_plugin]]
== The `kotlin-dsl` plugin

When applied to a project, the `kotlin-dsl` Gradle plugin configures the project's Kotlin source sets to enable Gradle Kotlin DSL features.

It is useful for all projects implementing build logic: <<organizing_gradle_projects.adoc#sec:build_sources, buildSrc>>, <<composite_builds.adoc, included builds>> for build logic or link:{guidesUrl}/designing-gradle-plugins/[Gradle plugin] builds.

The `kotlin-dsl` plugin can be applied as follows:

[source,kotlin]
.buildSrc/build.gradle.kts
----
plugins {
    `kotlin-dsl`
}

repositories {
    // The org.jetbrains.kotlin.jvm plugin requires a repository
    // where to download the Kotlin compiler dependencies from.
    jcenter()
}
----

The `kotlin-dsl` plugin

* applies the link:https://kotlinlang.org/docs/reference/using-gradle.html#targeting-the-jvm[org.jetbrains.kotlin.jvm] plugin,
* configures an embedded repository that contains all Kotlin libraries embedded with the Gradle Kotlin DSL, pins them to the embedded Kotlin version,
* adds the `kotlin-stdlib-jdk8`, `kotlin-reflect` and `gradleKotlinDsl()` dependencies to the `compileOnly` and `testImplementation` configurations,
* and, configures the Kotlin compiler with the same set of Kotlin compiler settings that is used for Gradle Kotlin DSL scripts.

[CAUTION]
====
The `kotlin-dsl` plugin enables experimental Kotlin compiler features.
See the <<sec:kotlin_compiler_arguments>> section below for more information.
====

[[sec:kotlin_compiler_arguments]]
== Kotlin compiler arguments

These are the Kotlin compiler arguments used for compiling Gradle Kotlin DSL scripts and Kotlin sources and scripts in a project that has the `kotlin-dsl` plugin applied:

`-jvm-target=1.8`::
Sets the target version of the generated JVM bytecode to `1.8`.

`-Xjsr305=strict`::
Sets up Kotlin's Java interoperability to strictly follow JSR-305 annotations for increased null safety.
See link:https://kotlinlang.org/docs/reference/java-interop.html#compiler-configuration[Calling Java code from Kotlin] in the Kotlin documentation for more information.

`-XX:NewInference`::
Enables the experimental Kotlin compiler inference engine (required for SAM conversion for Kotlin functions).

`-XX:SamConversionForKotlinFunctions`::
Enables SAM (Single Abstract Method) conversion for Kotlin functions in order to allow Kotlin build logic to expose and consume `org.gradle.api.Action<T>` based APIs. Such APIs can then be used uniformly from both the Kotlin and Groovy DSLs.
+
As an example, given the following hypothetical Kotlin function with a Java SAM parameter type:
+
[source,kotlin]
----
fun kotlinFunctionWithJavaSam(action: org.gradle.api.Action<Any>) = TODO()
----
+
SAM conversion for Kotlin functions enables the following usage of the function:
+
[source,kotlin]
----
kotlinFunctionWithJavaSam {
    // ...
}
----
+
Without SAM conversion for Kotlin functions one would have to explicitly convert the passed lambda:
+
[source,kotlin]
----
kotlinFunctionWithJavaSam(Action {
    // ...
})
----
{'fraction_non_alphanumeric': 0.09417758369723435, 'fraction_numerical': 0.005385735080058224, 'mean_word_length': 4.67382328654005, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5521406', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1906, 'n_words': 771}