Migrating Android App to Gradle Kotlin DSL 1.0

Kotlin DSL RC 1.0 is here š
Kotlin DSL is Kotlin language support for Gradle build scripts.
So it means that now you will be able to write your gradle files in Kotlin instead of Groovy. There are plenty of reasons why Gradle is doing such a huge move but in general we can say that they are trying to bring all IDE super powers to build scripts like:
- Auto-completion
- Content assist
- Quick documentation
- Navigation to source code
- Refactoring and much more
We have the Release Candidate 1.0 and you can use it since Gradle 4.10. The final release will be on Gradle 5. They said there will be no more breaking changes after that but being in RC this give us a lot of confidence that the API is getting more and more stable so thatās why I decided to upgrade my Android App to start using this.
And for this migration Iāll use the app that I use for almost every Kotlin article, the Keddit App :)
Keddit is a small Reddit client made completely using Kotlin and some great libraries like Dagger2, Retrofit, RxJava and more. Please feel free to learn more in this link:
Letās start migrating this app to use Gradle Kotlin Scripts!
If you just want to see the code, here you have the link to the commit.
Upgrade to Gradle 4.10
First of all, you need to upgrade to Gradle 4.10 (or above). You can easily do that by updating your gradle-wrapper.properties file:
#Fri Jun 08 17:01:54 EDT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
Android Studio Version
Make sure you are using the latest version of Android Studio. I was using 3.0.x and that brought me some issues. I updated to 3.1.4 (by the time of writing this) and everything was smooth.
This is an error that I faced when using the 3.0 version which was fixed by just doing the upgrade:

Internal error:
org.gradle.tooling.BuildException: Could not execute build using Gradle distribution "https:/....4-10-all.zip
Now is time to start converting all of our gradle files to Kotlin Script files.
KTS extension
Kts is the extension of the Gradle Kotlin Scripts. We are going to be converting our regular build.gradle file to build.gradle.kts file.
Root > settings.gradle.kts
Go to your settings.gradle file and rename it (Shift + F6) to be settings.gradle.kts. After that you just need to replace itās content for something like this:
// before
include ':app'// after
include(":app")
This is all the changes that I needed for this file. If I try to run gradle I see this error message:
Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org.gradle.configureondemand=false in your gradle.properties file or use a Gradle version less than 4.6.
Open File
I tried solving this issue by adding this property to the gradle.properties file but it didnāt work. In order to solve this problem just go to Android Studio preferences and uncheck: Configure on demand
Android Studio > Preferences > Build, Execution.. > Compiler

This will solve the previous error and you are going to be able to continue updating the gradle files.
Take into account that every time you add or change dependencies you will be prompted to apply those dependencies (like the image below), just make sure to do that or enable auto-reload so the IDE starts getting your dependencies or the applied plugins.

Now we are ready to start migrating the other files as the error is now in the the build.gradle file:
Plugin with id ācom.android.applicationā not found.
Open File
But first we need to update our root build.gradle as is the one that provides all of our dependencies for our plugins, among other things.
Root > build.gradle.kts
I renamed build.gradle to build.gradle.kts. For this one I had to comment all the code so it started to work the autocomplete. After that I started to replace the Groovy syntax to Kotlin one:
// before
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"// after
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
App > build.gradle.kts
Here the same thing, rename it and start replacing the code.
I started with the plugins:
apply plugin: 'com.android.application'
replaced to
plugins {
id("com.android.application")
...
}
Something really useful was to use Multicursor from Android Studio: Shift+Alt+Click (click each line you want to update)

I was able to update almost all the dependencies at once š
And Iām not going to copy and paste here every change but I give you the commit that I did so you can easily compare and see all the changes needed to migrate from Groovy files to Kotlin DSL files.

Commit
And thatās it! now you can start using Kotlin DSL in your own projects. Feel free to ask any question and please donāt forget to clap š
Thanks!!
You can find me on:
Twitter: https://twitter.com/juanchosaravia
LinkedIn: https://www.linkedin.com/in/juansaravia