Migrating Build Configuration From Groovy to Kotlin for an Android Project

Written by azamatnurkhojayev | Published 2023/08/07
Tech Story Tags: android | kotli | kotlin-dsl | tutorial | kotlin-tutorial | software-engineering | development | groovy-to-kotlin

TLDRAndroid Gradle plugin 4.0 added support for using Kotlin in your Gradle build configuration as a replacement for Groovy, the programming language traditionally used in Gradle configuration files. Kotlin is preferred over the Groovy for writing Gradle scripts because Kotlin is more readable and offers better compile-time checking and IDE support.via the TL;DR App

Domain-specific languages, or DSLs, are languages specialized for a specific part of an app. It is used to extract a part of the code to make it reusable and more understandable. As opposed to a function or a method, DSLs also change the way you use and write code.

To make a DSL means to change the syntax of that specific part of the code. In Kotlin, this is achieved by using lambda and extension functions, and expressions, to remove a lot of boilerplate code and hide the internal implementation from the user.

For example, I will take a newly created project and I will use the example to translate it from Groovy to Kotlin step by step.

  1. The first thing we will do is change the file extension from .gradle to .gradle.kts

  1. Changing plugins
plugins {  
    id("com.android.application")  
    id("org.jetbrains.kotlin.android")  
}

  1. Let's go to the android block and change the fields namespace ΠΈ compileSdk:
namespace = "kz.azamat.androidlessoncoroutines"  
compileSdk = 33

  1. Changing defaultConfig block:
defaultConfig {  
    applicationId = "kz.azamat.androidlessoncoroutines"  
    minSdk = 23  
    targetSdk = 33  
    versionCode = 1  
    versionName = "1.0"  
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"  
}

  1. Changing for buildTypes block:
buildTypes {  
    getByName("debug") {  
        isDebuggable = true  
    }  
    getByName("release") {  
        isMinifyEnabled = false  
        proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")  
    }  
}

  1. compileOptions will be changed to java and make a change for sourceCompatibility, targetCompatibility and for kotlinOptions:
java {  
    sourceCompatibility = JavaVersion.VERSION_1_8  
    targetCompatibility = JavaVersion.VERSION_1_8  
}  

kotlinOptions {  
    jvmTarget = "1.8"  
}

  1. Finally change the dependency section:

dependencies {  
    implementation("androidx.core:core-ktx:1.7.0")  
    implementation("androidx.appcompat:appcompat:1.6.1")  
    implementation("com.google.android.material:material:1.9.0")  
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")  
    testImplementation("junit:junit:4.13.2")  
    androidTestImplementation("androidx.test.ext:junit:1.1.5")  
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")  
}

Eventually, our code looks like this:


Written by azamatnurkhojayev | I'm an Android developer. Teaches courses at the programming school, and writes articles about development.
Published by HackerNoon on 2023/08/07