Migration from Groovy to Kotlin DSL Gradle (Part 1)

Nicos Nicolaou
4 min readDec 3, 2023

--

For this article we will see the migration from the Groovy to the new Kotlin DSL Gradle file. I decided to write this article because when I try to migrate my projects Gradle files, I spent a lot of time to find all the correct codes from my personal projects, from different sources, from official Android developer sites, tutorials, forums etc. So, we will see the migration step by step. The article will be in two parts.

Note: In this article I will show some basics and common codes for Gradle files, that developers usually have in their Gradle files.

Migrations

First of all rename the Gradle file from build.gradle to build.gradle.kts. We can start the migration.

Groovy

plugins {
id 'com.android.application'
id 'kotlin-android'
}

Kotlin

plugins {
id("com.android.application")
id("kotlin-android")
}

Groovy

android {
signingConfigs {
release {
storeFile file(".../app/keystoreFileName.keystore or keystoreFileName.jks")
storePassword "storePassword"
keyAlias "keyAlias"
keyPassword "keyPassword"
}
}
//...
}

Kotlin

android {
signingConfigs {
create("config") {
keyAlias = "keyAlias"
keyPassword = "keyPassword"
storeFile = file(".../app/keystoreFileName.keystore or keystoreFileName.jks""")
storePassword = "storePassword"
}
}
//...
}

Groovy

android {
//...
buildToolsVersion '34.0.0'
compileSdkVersion 34
namespace 'com.myproject.myproject'
//...
}

Kotlin

android {
//...
buildToolsVersion = "34.0.0"
compileSdk = 34
namespace = "com.myproject.myproject"
//...
}

Groovy

defaultConfig {
applicationId "com.myproject.myproject"
minSdkVersion 27
targetSdkVersion 34
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true

buildFeatures {
dataBinding true
viewBinding true
compose true
}

javaCompileOptions {
annotationProcessorOptions {
arguments += [
"room.incremental":"true"]
}
}

vectorDrawables {
useSupportLibrary true
}

buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -fexceptions"
}
}
}

Kotlin

defaultConfig {
applicationId = "com.myproject.myproject"
minSdk = 27
targetSdk = 34
versionCode = 1
versionName = "1.0.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled = true

buildFeatures {
dataBinding = true
viewBinding = true
compose = true
}

javaCompileOptions {
annotationProcessorOptions {
arguments += mapOf(
"room.incremental" to "true"
)
}
}

vectorDrawables {
useSupportLibrary = true
}

buildTypes {
release {
isShrinkResources = true
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
debug {
isShrinkResources = true
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}

ndk {
abiFilters.add("armeabi-v7a")
abiFilters.add("arm64-v8a")
abiFilters.add("x86")
abiFilters.add("x86_64")
}
externalNativeBuild {
cmake {
cppFlags("-std=c++11 -fexceptions")
}
}
}

Groovy

externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.22.1"
}
}
ndkVersion '26.1.10909125'

Kotlin

externalNativeBuild {
cmake {
path = file("src/main/cpp/CMakeLists.txt")
version = "3.22.1"
}
}
ndkVersion = "26.1.10909125"

Groovy

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}

Kotlin

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}

Groovy

composeOptions {
kotlinCompilerExtensionVersion = '1.5.4'
}

packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}' //for compose
}
}

Kotlin

composeOptions {
kotlinCompilerExtensionVersion = "1.5.4"
}

packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}" //for compose
}
}

Groovy

flavorDimensions "version"
productFlavors {
dev{
applicationId "com.myproject.myproject.dev"
minSdkVersion 27
targetSdkVersion 37
versionCode 1
versionName "1.0.0"
flavorDimensions "version"
resValue "string", "app_name", "App Name dev"
buildConfigField "String", "url", "\"https://testServer.com\""
}
pro{
applicationId "com.myproject.myproject"
minSdkVersion 27
targetSdkVersion 34
versionCode 1
versionName "1.0.0"
flavorDimensions "version"
resValue "string", "app_name", "App Name"
buildConfigField "String", "url", "\"https://liveServer.com\""
signingConfig signingConfigs.release
}
}

Kotlin

flavorDimensions += "version"
productFlavors {
create("dev") {
applicationId = "com.myproject.myproject.dev"
minSdk = 27
targetSdk = 34
versionCode = 1
versionName = "1.0.0"
dimension = "version"
resValue("string", "app_name", "App Name dev")
buildConfigField("String", "url", "\"https://testServer.com\"")
}
create("pro") {
applicationId = "com.myproject.myproject"
minSdk = 27
targetSdk = 34
versionCode = 1
versionName = "1.0.0"
dimension = "version"
resValue("string", "app_name", "App Name")
buildConfigField("String", "url", "\"https://liveServer.com\"")
signingConfig = signingConfigs.getByName("config")
}
}

Groovy

ext {
activityVersion = '1.8.1'
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.activity:activity-ktx:$activityVersion"
}

Kotlin

val activityVersion by extra("1.8.1")

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("androidx.activity:activity-ktx:$activityVersion")
}

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.

--

--

Nicos Nicolaou

Senior Software Engineer, Android and Flutter Developer, Google Play Developer, Building libraries, Writing articles. 👇🔗 https://github.com/NicosNicolaou16