Kotlin DSL migration on an existing groovy Android MultiModule Project

Berkin Tatlısu
2 min readSep 30, 2021

Gradle’s Kotlin DSL provides an alternative syntax to the traditional Groovy DSL with an enhanced editing experience in supported IDEs, with superior content assist, refactoring, documentation, and more. This chapter provides details of the main Kotlin DSL constructs and how to use it to interact with the Gradle API.

If you are interested in migrating an existing Gradle build to the Kotlin DSL, please also check out the dedicated migration section.

For android side, things may be mixed, so lets try my approach which will save you a couple of hours.

  1. Create a module named : “buildSrc” inside the root project.
  2. remove “buildsrc” from settings.gradle
  3. Rename existing build.gradle and settings.gradle files to build.gradle.kts and settings.gradle.kts
  4. adopt the kotlin code style on every *.kts file(groovy will fail, needs to be changed.)

module(build.gradle) before:

after :

commondep() refers the kotlin fun inside our buildSrc module. so all code remains clean.

whole utils.kt inside buildSrc module , replaces buildscript code in seperate modules into 60 lines after this migration.

We may extend gradle Project per module and ad dependencies inside it. this way every module can see and grab their dependencies from here(Utils.kt).

import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
import org.gradle.kotlin.dsl.project

fun Project.commonDep() {
dependencies {
"testImplementation"("org.junit.jupiter:junit-jupiter-api:5.7.0")
"testRuntimeOnly"("org.junit.jupiter:junit-jupiter-engine:5.7.0")
"implementation"("io.reactivex.rxjava2:rxjava:2.2.21")
"implementation"("com.google.dagger:dagger:2.37")
"kapt"("com.google.dagger:dagger-compiler:2.37")
}
}

fun Project.dataModuleDep() {
dependencies {
"implementation"(project(":domain"))
}
}

fun Project.networkModuleDep() {
dependencies {
"implementation"(project(":domain"))
"implementation"("com.squareup.retrofit2:retrofit:2.9.0")
"implementation"("com.squareup.retrofit2:converter-gson:2.9.0")
}
}


fun Project.androidUIDep() {
apply(plugin = "com.android.application")
apply(plugin = "kotlin-android")
// configure<AppExtension>("android") {

//}
dependencies {
"implementation"(project(":data"))
"implementation"(project(":domain"))
"implementation"(project(":network"))

"implementation"("com.google.dagger:dagger:2.37")
"implementation"("androidx.navigation:navigation-fragment-ktx:2.3.0")
"implementation"("androidx.navigation:navigation-ui-ktx:2.3.0")
"implementation"("androidx.fragment:fragment-ktx:1.3.6")
"implementation"("androidx.legacy:legacy-support-v4:1.0.0")
"implementation"("androidx.lifecycle:lifecycle-livedata-ktx:2.2.0")
"implementation"("androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0")
"kapt"("com.google.dagger:dagger-compiler:2.37")
"implementation"("io.reactivex.rxjava2:rxjava:2.2.21")
"implementation"("io.reactivex.rxjava2:rxandroid:2.1.1")
// "implementation"("org.openjfx:javafx:11")
// "implementation"("org.openjfx:javafx-controls:11")

"implementation"("org.jetbrains.kotlin:kotlin-stdlib:1.5.10")

"implementation"("androidx.appcompat:appcompat:1.2.0")
"implementation"("androidx.constraintlayout:constraintlayout:1.1.3")
"testImplementation"("junit:junit:4.+")
"androidTestImplementation"("androidx.test.ext:junit:1.1.1")
"androidTestImplementation"("androidx.test.espresso:espresso-core:3.1.0")
}
}

--

--