What is Gradle Kotlin DSL ?
In this article, we will talk about topics such as what is Gradle, why Kotlin DSL, and the transition from Groovy to Kotlin DSL.
What is the Gradle ?
Android Studio allows you to define flexible, custom build configurations while using Gradle an advanced build toolset to automate and manage the compilation process. Each build configuration can define its own code and resource set while reusing common parts across all versions of your application.
The Android build system compiles the application resources and source code and packages them into APKs or Android Application Packages that you can test, sign, and distribute.
Android Studio uses Gradle, an advanced build toolset, to automate and manage the compilation process while allowing you to define flexible, custom build configurations. Each build configuration can define its own code and resource set while reusing common parts across all versions of your application. The Android Gradle plugin works in conjunction with the build toolset to provide processes and configurable settings specific to building and testing Android applications.
Why Kotlin DSL ?
Kotlin Script (KTS) is preferred over Groovy for writing Gradle scripts because Kotlin is more readable and offers better compile-time checks and IDE support. The Android Gradle plugin is available in version 4.0 and higher. The final version of Gradle is version 5. Additionally, you can refer to Gradle’s migration guide and Kotlin DSL Primer for more information.
Additionally, Kotlin DSL provides some advantages such as:
- Automatic completion
- Ease of use of extensions
- Extensibility
The file extension names are based on the language in which the compilation file is written.
- Gradle build files written in Groovy use the “.gradle” file extension
- Gradle build files written in Kotlin use the “.gradle.kts” file extension
The migration from Groovy to Kotlin DSL is quite simple
- We will replace the parts (implementation, apply plugin, etc.) written with single quotes in Gradle with double quotes
- We will rename .gradle extensions to .gradle.kts
- We will make sure that the Gradle version we are using is compatible
Migrate from Groovy to Kotlin DSL
We will quickly migrate from Groovy to Kotlin DSL by applying simple steps.
settings.gradle
- Firstly, we are renaming our ‘settings.gradle’ file to ‘settings.gradle.kts’
- We are replacing single quotes with double quotes
Old
include ':app'
New
include(":app")
Then let’s sync the project and make sure there are no failures.
root — build.gradle
- Firstly, we rename our ‘build.gradle’ file to ‘build.gradle.kts’
- We will replace single quotes with double quotes
- The classpath now has string parameters, and we update it using parentheses
Old Dependencies
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'
}
New Dependencies
dependencies {
classpath("com.android.tools.build:gradle:7.0.4")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
}
Old Clean Task
task clean(type: Delete) {
delete rootProject.buildDir
}
New Clean Task
task("clean") {
delete(project.buildDir)
}
Old Repositories
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
New Repositories
repositories {
google()
mavenCentral()
maven { setUrl("https://jitpack.io") }
}
After all the changes we made, we sync the project and wait for it to be successful.
app — build.gradle
When using Groovy, we applied each plugin separately, but in Kotlin, the plugins{}
block applies all plugins in a single block. As in the other steps;
- We are renaming our ‘build.gradle’ file to ‘build.gradle.kts’
- We will replace single quotes with double quotes
Old Plugin
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
New Plugin
plugins {
id("com.android.application")
id("kotlin-android")
id("org.jetbrains.kotlin.kapt")
id("dagger.hilt.android.plugin")
}
To apply plugins in Kotlin, you use the id() and kotlin() functions.
- id() is used for all plugins
- The
kotlin()
function is used for Kotlin-specific functions. You can also omit the 'kotlin-' prefix. The dash (-) will be replaced with a dot (.)
kotlin("android")
kotlin("android.extensions")
Old Android Block
android {
compileSdkVersion 33
defaultConfig {
applicationId "com.tfaki.kotlindslexample"
minSdkVersion 24
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
New Android Block
android {
compileSdkVersion = 33
defaultConfig {
applicationId = "com.tfaki.kotlindslexample"
minSdkVersion = 24
targetSdkVersion = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}
We quickly move on to the final step, which is the dependencies section.
Old Dependencies
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version'
implementation 'androidx.core:core-ktx:$ktx_version'
implementation 'androidx.appcompat:appcompat:$appcompat_version'
testImplementation 'junit:junit:$junit_version'
androidTestImplementation 'androidx.test.ext:junit:$ext_version'
androidTestImplementation 'androidx.test.espresso:espresso-core:$espresso_version'
}
New Dependencies
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
implementation("androidx.core:core-ktx:$ktx_version")
implementation("androidx.appcompat:appcompat:$appcompat_version")
testImplementation("junit:junit:$junit_version")
androidTestImplementation("androidx.test.ext:junit:$ext_version")
androidTestImplementation("androidx.test.espresso:espresso-core:$espresso_version")
}
After making the changes, we sync the project and complete the transition to Kotlin DSL. You can click on the link to review the project I used Kotlin DSL for.
I hope it was a useful writing. See you in the next article.