Skip to content

Instantly share code, notes, and snippets.

@thebino
Last active August 28, 2023 16:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thebino/da74f46cb326b95d88ab3314d4e1debc to your computer and use it in GitHub Desktop.
Save thebino/da74f46cb326b95d88ab3314d4e1debc to your computer and use it in GitHub Desktop.

Gradle Kotlin DSL

This is small overview how to migrate your groovy buildfiles into Kotlin DSL.

Using the Kotlin DSL for gradle build files improves the editors typing experience in many ways. First of all, the groovy syntax doesn't support refactoring nor auto-completion while editing in most IDEs.

Build import Syntax highlighting Semantic editor
IntelliJ IDEA
Android Studio
Eclipse IDE
CLion
Apache NetBeans
Visual Studio Code
Visual Studio

source of the table

Table of contents

Getting started

To start with a migration, it is recommended to apply these 4-steps to reduce the warnings and errors.

  1. single quotes 'string' or double quotes "string"
  2. make ambiguous statements property by using the assignment operator
  3. change failing statements into function invocations
  4. rename the goovy script to *.gradle.kts

Project hierarchy

settings.gradle

include ':App'

settings.gradle.kts

include(":App")

apply from

build.gradle

apply from: "kayleeSettings.gradle"

build.gradle.kts

apply(from = "kayleeSettings.gradle")

apply plugin

build.gradle

apply plugin: "com.android.application"

build.gradle.kts

apply(plugin = "com.android.application")

improved kotlin build.gradle.kts

plugins {
    id("com.android.application")
}

placeholders

build.gradle

manifestPlaceholders = [isFirebaseTrackingDeactivated: false]

build.gradle.kts

manifestPlaceholders = mapOf("isFirebaseTrackingDeactivated" to false)

Tasks

build.gradle

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle.kts

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

Versioning

build.gradle

ext {
    compileSdkVersion = 29
}

rootProject.ext.compileSdkVersion

There are different approaches for version management

1

build.gradle.kts

allprojects {
    ext {
        set("compileSdkVersion", 29)
    }
}

2

build.gradle.kts

buildscript {
    extra.apply{
        set("compileSdkVersion", 26)
    }
}

3

create a file buildSrc/build.gradle.kts

plugins {
    `kotlin-dsl`
}

buildSrc/src/main/kotlin/Constants.kt

object Constants {
   const val kotlinVersion = "1.3.70"
}

build.gradle.kts

targetSdkVersion(Constants.targetSdkVersion)

4

gradel.properties

compileSdkVersion = 29

build.gradle.kts

buildScript {
   val kotlinVersion by project
}

Under the hood

You may ask how gradle will find plugins like java?

build.gradle.kts

plugins {
    java
}

It is using inline functions to resolve the dependency

inline val org.gradle.plugin.use.PluginDependenciesSpec.`java`: org.gradle.plugin.use.PluginDependencySpec
    get() = id("org.gradle.java")

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment