The New Way of Writing Build Gradle with Kotlin DSL

Convert from Groovy to Kotlin script

Achmad Fachrudin
ProAndroidDev

--

Build Gradle with Kotlin DSL

It has been a long time since we wrote a Gradle with a script called Groovy, but finally, there is something new 😮.

Kotlin DSL comes with autocomplete, navigation to resource, checking error at compile time and make code readable. How to use Kotlin script on Gradle? Just follow the steps.

FYI, when writing this I use com.android.tools.build:gradle:3.5.0 and on gradle-wrapper 5.4.1

Step 1 — Create buildSrc

buildSrc folder

Let’s start with create buildSrc on your root folder include two files, first build.gradle.kts

import org.gradle.kotlin.dsl.`kotlin-dsl`plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}

then Dependencies.kt on src/main/java

Dependencies.kt

and try Sync Project with Gradle Files

Step 2 — Convert settings.gradle

Rename settings.gradle to settings.gradle.kts and now you can use kotlin scripts

Before

include ':app', ':data', ':framework'

After

include(":app", ":framework", ":data")

Step 3 — Convert build.gradle Project

Rename again build.gradle to build.gradle.kts and change dependencies block before

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

After

dependencies {
classpath("com.android.tools.build:gradle:${Versions.gradle}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}")
}

for task clean block before

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

After

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

repositories block before

repositories {
google()
jcenter()
maven{ url 'https://jitpack.io' }
}

After

repositories {
google()
jcenter()
maven { url = uri("https://jitpack.io") }
}

Step 4 — Convert build.gradle App

Now you will start to get confused for scripts that are quite complex 😨 rename build.gradle to build.gradle.kts

On plugin block before

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

After

plugins {
id
("com.android.application")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}

If you use kotlin() use must change dash(-) with dot(.) or you can still use dash like id(“kotlin-android-extensions”)

defaultConfig block

defaultConfig

the basic thing when you change Gradle using Kotlin script is the use of single-quotes to double-quotes. For minSdkVersion and targetSdkVersion is function and you can put parameter from Dependencies.kt, but for versionCode and versionName is variable.

dataBinding, bundle, lintOptions, and packagingOptions block

signingConfigs block

buildTypes and testOptions block

dependencies block

Yey! Sync project and your project is converted in Kotlin build scripts. As you can see, we can use implementation with the value from Dependencies.kt this makes our Gradle cleaner.

It could be that your project is more complicated, but it’s only a matter of time until you get used to it. 😎

--

--