Migrating Gradle to Kotlin DSL in Android Studio 3.3

Cheat sheet for migrating Gradle files from Groovy to Kotlin

Jan Stoltman
Jan 22 · 3 min read

Note: In this article I assume that you know what Kotlin has to do with Gradle and are just looking for a quick cheat sheet or a copy-paste solution. For a complete documentation take a look here.

Step one: Gradle Wrapper Version

It is recommended to use Wrapper from version 5.0 and upwards, unfortunately in Android projects it is at the same time strongly recommended to avoid the use of Gradle 5.0 without Android Gradle Plugin 3.4 (which is not yet released). In this case, just make sure that you are using newest supported Wrapper version (4.10.1 at the time of writing).

//gradle.properties
distributionBase
=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

Step two: Change all to

Using the Find and replace (ctrl + R in android studio) tool change all single quoted strings (‘) to double quoted strings (“) in all your Gradle files. For freshly started apps you have to take care of three files:

  • settings.gradle
  • build.gradle (Project)
  • build.gradle (Module)

Step three: Change implicit assignments and function calls into explicit ones

Now, all Groovy assignments and function calls have to be changed into Kotlin-friendly fashion. Replace all of them as shown below:

// Groovy assignment
applicationId "com.yggdralisk.githubbrowser"
// Kotlin assignment
applicationId = "com.yggdralisk.githubbrowser"
// Groovy function call
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha3"
// Kotlin function call
implementation("androidx.constraintlayout:constraintlayout:2.0.0-alpha3")

If you don’t know what, how or even if something should be changed in this step, take a look at the example files linked at the bottom of this article.

Step four: Migrate settings.gradle

Note: Before doing this you might want to turn-off the auto-sync for Gradle files as it might cause you to go crazy. Turn it on again after you’ve completed migration for the whole project.

Using Rename file (shift + F6) tool change the name of settings.gradle file to settings.gradle.kts. For fresh projects it should look like this:

//settings.gradle.kts
include(":app")

Step five: Migrate build.gradle (Project)

Repeat the step as above but for the project’s build.gradle file. Change its name to build.gradle.kts.

//build.gradle.kts (Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
val kotlinVersion = "1.3.11"
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.3.0")
classpath(kotlin("gradle-plugin", kotlinVersion))

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

tasks {
val clean by registering(Delete::class) {
delete(buildDir)
}
}

Step six: Migrate settings.gradle (Module)

And once again, do the same for module’s build.gradle -> build.gradle.kts.

import org.jetbrains.kotlin.config.KotlinCompilerVersion

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

android {
compileSdkVersion(28)
defaultConfig {
applicationId = "com.yggdralisk.githubbrowser"
minSdkVersion(21)
targetSdkVersion(28)
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
}

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
implementation("com.android.support:appcompat-v7:28.0.0")
testImplementation("junit:junit:4.12")
androidTestImplementation("com.android.support.test:runner:1.0.2")
androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
}

repositories {
mavenCentral()
maven("http://repository.jetbrains.com/all")
}

Links for example files:

Examples of working Gradle files can be found at my github repository. Feel free to either fork or copy them at any time.


If you are facing any problems with migration, remember to always follow the errors shown in the build/sync logs, not the ones shown by Android Studio as it is still far from perfect when it comes to that.

Jan Stoltman

Written by

I’m a student, a developer and (apparently) a writer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade