Sample Kotlin-based Gradle build scripts
The Gradle projects in this directory demonstrate typical use cases with and features available in the Gradle Kotlin DSL. They include:
ant: demonstrates how to use Ant from Gradle via the Ant Groovy Builder
build-cache: demonstrates how to configure the Gradle build cache
build-scan: demonstrates how to apply and configure the
buildSrc-plugin: demonstrates how to use the
java-gradle-pluginplugins together in
code-quality: demonstrates how to configure Gradle code quality plugins
composite-builds: demonstrates how to use Composite Builds
copy: demonstrates typed task declarations, and configuration of a Gradle
domain-objects: demonstrates how to create and configure a
NamedDomainObjectContainerfrom a Kotlin build script.
extra-properties: demonstrates the use of
extraproperties (equivalent of the
extproperties found in Gradle Script Groovy)
gradle-plugin: demonstrates a Gradle plugin implemented in Kotlin and taking advantage of the
groovy-interop: demonstrates how to interact with Groovy code from Kotlin
hello-android: demonstrates a Kotlin-based Gradle build script for a Kotlin-based Android project
hello-coroutines: demonstrates how to enable experimental support for coroutines in Kotlin
hello-kapt: demonstrates a Kotlin-based Gradle build script for a project that is itself written in Kotlin and uses
kapt(Kotlin Annotation Processing Tool)
hello-kotlin: demonstrates a Kotlin-based Gradle build script for a project that is itself written in Kotlin
hello-world: demonstrates plugin application and configuration, dependency management, JUnit testing
kotlin-friendly-groovy-plugin: demonstrates a Groovy Gradle plugin and its use from Kotlin-based build scripts
maven-plugin: demonstrates how to configure the Gradle
maven-publish: demonstrates how to configure the Gradle
model-rules: demonstrates the use of model rules
modularity: demonstrates the use of
apply(from = "")to modularize build scripts
multi-kotlin-project: demonstrates a multi-project build comprising two Kotlin based projects
multi-kotlin-project-config-injection: demonstrates a multi-project build comprising two Kotlin based projects in which all
KotlinCompiletasks belonging to the subprojects are configured by the root project
multi-kotlin-project-with-buildSrc: demonstrates a multi-project build comprising two Kotlin based projects with custom build logic in
multi-project-with-buildSrc: demonstrates a multi-project build with custom build logic in
buildSrc, including a custom task
precompiled-script-plugin: demonstrates a Gradle plugin implemented as a precompiled script
project-properties: demonstrates project property access via delegated properties
project-with-buildSrc: demonstrates a single-project build with custom build logic in
provider-properties: demonstrates usage of lazily evaluated properties to map extension properties to task properties
source-control: demonstrates how to use external source dependencies
task-dependencies: demonstrates explicit configuration of task dependencies
testkit: demonstrates how to test a Gradle plugin written in Kotlin using TestKit
Set up dev environment
Note: Currently, these instructions only address working with IntelliJ IDEA. In the future, they'll be expanded to include working with Eclipse.
Install IntelliJ IDEA
Version 2017.3.3 or better is required, and can be downloaded from https://www.jetbrains.com/idea.
Install IDEA Kotlin Plugin
Note: the specific version of the Kotlin plugin matters.
All the samples should work against the latest Kotlin plugin, 1.2.20 at the time of this writing, from the Stable channel accessible via the Tools > Kotlin > Configure Kotlin Plugin Updates action.
Set up a sample project
Clone the Gradle Kotlin DSL repository
If you have not already done so, clone the kotlin-dsl repository:
git clone email@example.com:gradle/kotlin-dsl.git # ($REPO_ROOT)
Note: The remainder of these instructions focus on the
hello-world sample project, but will work equally well for any of the other samples.
Import the sample project into IDEA
In IDEA, go to
File->Open... and navigate to
When prompted, choose "Use default Gradle wrapper".
The project should import without errors.
Explore available IDE support
You're now ready to explore what's possible with the Gradle Kotlin DSL in IDEA. Generally speaking, things should "just work", as they would in the context of any other Java or Kotlin code you would write in IDEA.
Start by opening
Note: The very first time you do, IDEA might fail to recognise the classpath of the script, if that happens, simply restart IDEA. This is a known issue that will be fixed soon.
Continue with any or all of the following:
You should notice that normal Kotlin syntax highlighting works throughout the file.
Try clicking on any type or function in the script, and hit
F1 (or possibly
CTRL-J, dependending on which IDEA key mapping you use). Notice how you're presented with a quick documentation pop-up complete with that element's Javadoc / KDoc.
Navigation to source
Again, try clicking on any type or function in the script and hit
CMD-B. Notice that you're taken directly to the source for that element.
Auto-completion / content assist
CTRL-SPACE at various sites in the script, and notice that you're provided with complete content assist regarding what's available at that site.
Most any refactoring action that is possible in a Kotlin file should also work in a Kotlin-based Gradle build script. Explore!
See the latest release notes for further details on current features and limitations.