A Quick Look to Kotlin DSL (KTS) from the Android Development Perspective

dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
}
  1. Language is Kotlin and we all know Kotlin better than Groovy,
  2. It has a content assist and refactoring abilities thanks IDEs which supports Kotlin DSL
  3. Partially type safety.
  4. Easy to migrate from Groovy to Kotlin DSL.
  1. It has poor performance then Groovy scripts :) Yes, unfortunately, this is real and actually, this is not a surprise. Because almost all documents mentioned this; gradle, android, and there is open ticket for this also.
  2. If you create buildSrc along with KTS files, editing anything inside the buildSrc module cause Gradle to clear build caches. It means there will be a long build duration for you :) At that point, the architecture of buildSrc comes as important, you have to avoid writing frequently changed parts of building codes inside buildSrc.
import org.gradle.kotlin.dsl.`kotlin-dsl`

plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
}
  1. String quotes: You have to change all string quotes from ‘ to “. As you know in Kotlin Syntax, ‘ can be used only char type and Unicode.
  2. Assignments: If you assign and property without =, you have to write = operator in KTS.
  3. Functions: You should call functions with (), if you don’t use parentheses you have to write them now.
# Groovy Syntaxplugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-parcelize'
id 'dagger.hilt.android.plugin'
}
# KTS Syntaxplugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-kapt")
id("kotlin-parcelize")
id("dagger.hilt.android.plugin")
}
# Groovy Syntaxandroid {
compileSdk 31
buildToolsVersion '30.0.3'
...
# KTS Syntaxandroid {
compileSdk = 31
buildToolsVersion = "30.0.3"
...
  1. Product Flavors
# Groovy SyntaxflavorDimensions 'default'
productFlavors {
dev {
dimension 'default'
buildConfigField("String", "BASE_URL", "DEV_URL")
}
live {
dimension 'default'
buildConfigField("String", "BASE_URL", "LIVE_URL")
}
}
# KTS SyntaxflavorDimensions.add("default")
productFlavors {
create("dev") {
dimension = "default"

buildConfigField("String", "BASE_URL", "DEV_URL")
}

create("live") {
dimension = "default"

buildConfigField("String", "BASE_URL", "LIVE_URL")
}
}
# Groovy SyntaxProperties appProperties = new Properties()
appProperties.load(project.rootProject.file("app.properties").newDataInputStream())
# KTS Syntaxval appProperties = Properties().apply {
load(FileInputStream(File(rootProject.rootDir, "app.properties")))
}
# Groovy SyntaxsigningConfigs {
debug {
storeFile file("../keystore/android_template_debug.jks")
storePassword "androidtemplate"
keyAlias "template"
keyPassword "androidtemplate"
}
release {
storeFile file("../keystore/android_template_release.jks")
storePassword localProperties.get("release.key.storePassword")
keyAlias localProperties.get("release.key.alias")
keyPassword localProperties.get("release.key.password")
}
}
# KTS SyntaxsigningConfigs {
getByName("debug") {
storeFile = file("../keystore/android_template_debug.jks")
storePassword = "androidtemplate"
keyAlias = "template"
keyPassword = "androidtemplate"
}
create("release") {
storeFile = file("../keystore/android_template_release.jks")
storePassword = localProperties["release.key.storePassword"] as String
keyAlias = localProperties["release.key.alias"] as String
keyPassword = localProperties["release.key.password"] as String
}
}
# Groovy Syntaxtask clean(type: Delete) {
delete rootProject.buildDir
}
# KTS Syntax/**
* Default cleaning task definition
*/
tasks
.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
# Groovy SyntaxappDependencies.gson,
project(":common:core"),
kapt(kaptDependencies.hiltAndroidCompiler)
liveApi(appDependencies.chucker)
devApi(debugDependencies.chucker)
# KTS Syntaximplementation(Dependencies.gson)
implementation(project(":common:core"))
kapt(KaptDependencies.hiltAndroidCompiler)
"liveApi"(Dependencies.chucker)
"devApi"(Dependencies.chucker)
# Groovy Syntaxdef getDateTime() {
def date = new Date()
def formattedDate = date.format('dd-MM HH:mm', TimeZone.getTimeZone("GMT+3"))
return formattedDate
}
# KTS Syntaxfun getDateTime() = DateTimeFormatter.ofPattern("dd-MM HH:mm").format(LocalDateTime.now())
# Groovy SyntaxmanifestPlaceholders = [
appIcon : "@mipmap/ic_launcher",
appIconRound: "@mipmap/ic_launcher_round"
]
# KTS SyntaxmanifestPlaceholders["appIcon"] = "@mipmap/ic_launcher"
manifestPlaceholders["appIconRound"] = "@mipmap/ic_launcher_round"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store