Migration from Groovy to Kotlin DSL Gradle (Part 2)

Nicos Nicolaou
3 min readDec 3, 2023

--

This article is the part 2 for migration from the Groovy to the new Kotlin DSL Gradle file. We will see the two remaining Gradle files.

First of all rename the Gradle file from build.gradle to build.gradle.kts and settings.gradle to settings.gradle.kts, we can start the migration.

Build Gradle

Groovy

buildscript {
ext {
kotlin_version = '1.9.20'
}
dependencies {
def navigationVersion = "2.7.4"
def hiltVersion = "2.48.1"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hiltVersion"
}
repositories {
mavenCentral()
}
}

plugins {
id 'com.android.application' version '8.1.4' apply false
id 'com.android.library' version '8.1.4' apply false
id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
}

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

Kotlin

buildscript {
dependencies {
val navigationVersion by extra("2.7.4")
val hiltVersion by extra("2.48.1")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion")
classpath("com.google.dagger:hilt-android-gradle-plugin:$hiltVersion")
}
repositories {
mavenCentral()
}
}

plugins {
id("com.android.application") version "8.1.4" apply false
id("com.android.library") version "8.1.4" apply false
id("org.jetbrains.kotlin.android") version "1.9.20" apply false
id("com.google.devtools.ksp") version "1.9.20-1.0.14" apply false
}

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

Settings Gradle

Groovy

pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name='App Name'
include ':app'

Kotlin

pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "App Name"
include(":app")

Bonus setup for Android library

Groovy

afterEvaluate {
publishing {
publications {
release(MavenPublication) {
groupId = 'com.groupid.groupid'
artifactId = 'myLibrary'
version = '1.0.0'
from components.release
}
}
}
}

Kotlin

afterEvaluate {
publishing {
publications {
register<MavenPublication>("release") {
groupId = "com.groupid.groupid"
artifactId = "myLibrary"
version = "1.0.0"
from(components["release"])
}
}
}
}

I hope you find the article useful. I hope that I helped you to find the basic code for the Gradle in one place, and don’t spend time for searching them. Let me know your opinion under the comments, feel free to report any issue. I will appreciate it if you let a clap, so, if I have your support to continue writing.

You can check the Part 1 and Part 3, the new initialization way with Version Catalogs.

References:

--

--

Nicos Nicolaou

BSc Computer Science, MSc Mobile Systems, Android and Flutter Developer, Google Play Developer, Programming, Guitar. 👇🔗 https://github.com/NicosNicolaou16