➡️Transition from Groovy to Kotlin DSL

Ali Osman ARSLAN
Huawei Developers
Published in
4 min readDec 29, 2023
Gradle — Groovy vs Kotlin DSL

Introduction

Greetings! In this article, we will focus on the vibrant changes in the software world, narrating the journey of transitioning from the Groovy programming language to Kotlin DSL. We used to sway to the rhythm of Groovy in the past, but now, on the stage, the modern melody of Kotlin DSL is emerging. This technological evolution marks a significant milestone in the software development world, and in this article, we will delve into the reasons, advantages, and intricacies of these transformations.

Perhaps the code you currently write with Groovy satisfies you, but when you open the doors of Kotlin DSL, you will discover a new world. This article serves as a guide, taking you from the comforting and enjoyable atmosphere of Groovy to the simple, powerful, and scalable world of Kotlin DSL.

This process is not only a technical transformation but also signifies a change in a programmer’s perspective. Therefore, in this article, we will focus not only on code lines but also on the philosophical differences between these languages.

What is Groovy?

Groovy is a dynamic programming language that operates on the Java Virtual Machine (JVM), commonly used for configuration files in Android applications, particularly in build.gradle files. In Android Studio, the configuration manager named Gradle utilizes Groovy-based build.gradle files to compile and manage projects. Groovy's straightforward and readable syntax simplifies the configuration process for Android projects.

Example of an Android build.gradle file (Groovy-based):

android {
compileSdkVersion 30
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 16
targetSdkVersion 30
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

In the above example, the android block specifies the compilation settings for the application, and a release configuration is defined within the buildTypes section.

What is Kotlin DSL?

Kotlin DSL (Domain Specific Language) refers to languages specifically designed for a particular domain, written in the Kotlin programming language. In the realm of Android application development, it has become more prevalent to write Gradle configuration files using Kotlin DSL. The robust and expressive nature of Kotlin DSL enables more effective management of the configuration process in Android projects.

Example of an Android build.gradle.kts file (Kotlin DSL-based):

android {
compileSdkVersion(30)
defaultConfig {
applicationId = "com.example.myapp"
minSdkVersion(16)
targetSdkVersion(30)
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}

In the above example, the same configuration details are expressed using Kotlin DSL. Pay attention to changes in properties such as compileSdkVersion, applicationId, and minSdkVersion. Kotlin DSL brings the modern and expressive features of Kotlin to build.gradle files.

How to Transition?

After initially setting up your Android project with Groovy-based Gradle, you can follow these steps to transition to Kotlin DSL-based Gradle:

  1. Project Preparation: As a first step, review your Groovy code in the existing project and check for any different dependencies. Ensure that your project is in a stable state before proceeding with the transition.
  2. Creating Kotlin DSL Files: Create a buildSrc folder in the root directory of your project. Inside this folder, create a build.gradle.kts file. Then, you can create a src.main.java folder within it and create a Dependencies.kt file. This is where you will add your dependencies.

3. build.gradle.kts File: To enable compilation, update this file as follows

plugins {
kotlin("dsl")
}

repositories {
google()
mavenCentral()
}

4. Updating Dependencies: As we transition to Kotlin DSL, it’s essential to update our dependencies and configurations to align with DSL syntax. I’ve explained this step with the following code example:

Let’s Look at Some Code Examples!

Groovy Example

android {
compileSdkVersion 30
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 16
targetSdkVersion 30
}
}
dependencies {
//Core
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.7.0-alpha01'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

// Retrofit
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
}

Kotlin DSL Example

android {
compileSdkVersion(30)
defaultConfig {
applicationId = "com.example.myapp"
minSdkVersion(16)
targetSdkVersion(30)
}
}
dependencies {
//Core
implementation(Dependencies.androidCore)
implementation(Dependencies.appCompat)
implementation(Dependencies.material)
implementation(Dependencies.constraintLayout)

// Retrofit
implementation(Dependencies.retrofit)
implementation(Dependencies.okhttpLoggingInterceptor)
implementation(Dependencies.gsonConverter)
}
    // Core
const val androidCore = "androidx.core:core-ktx:${Version.core_ktx}"
const val appCompat = "androidx.appcompat:appcompat:${Version.appCompat}"
const val material = "com.google.android.material:material:${Version.material}"
const val constraintLayout = "androidx.constraintlayout:constraintlayout:${Version.constraintLayout}"

// Retrofit
const val retrofit = "com.squareup.retrofit2:retrofit:${Version.retrofitVersion}"
const val okhttpLoggingInterceptor = "com.squareup.okhttp3:logging-interceptor:${Version.okhttpLoggingInterceptorVersion}"
const val gsonConverter = "com.squareup.retrofit2:converter-gson:${Version.gsonConverterVersion}"
    // Kotlin
const val core_ktx = "1.7.0"

// AppCompat
const val appCompat = "1.4.1"

// Material
const val material = "1.7.0-alpha01"

// ConstraintLayout
const val constraintLayout = "2.1.3"

// Retrofit
const val retrofitVersion = "2.9.0"
const val okhttpLoggingInterceptorVersion = "4.9.1"
const val gsonConverterVersion = "2.9.0"

Summary

In this transition process, we’ve explored the fundamental steps of migrating your Android project’s Gradle scripts from Groovy to Kotlin DSL. This transition leads to a more modern, readable, and sustainable structure for your project. Thanks to Gradle’s support for Kotlin DSL, this transition can provide a more effective and enjoyable experience in Android application development.

References

--

--