Using Kotlin DSL For Gradle

Using Kotlin DSL For Gradle

If you’re an Android developer then you’ve undoubtedly worked in the build.gradle file.  And you’ve undoubtedly wanted to throw things at your computer when it didn’t work as intended.  It’s a file (2 actually) essential to your app’s well being, but it’s written in Groovy which pretty much no one knows well.

This has created frustration for countless users when they can’t compile things correctly.  Thankfully there’s a better way, and as with most things Android the answer is Kotlin!

Kotlin Instead of Groovy:

Gradle recently released version 5.0, and along with this comes Gradle Kotlin DSL v1.0.  This is what lets us write our Gradle build scripts in Kotlin.  But before doing so we should ask if this is even a good idea.  Why would we want to write our build scripts in Kotlin?  Well for starters Groovy is a dynamically typed language (type is associated with run-time variables).  This means that it’s tough for Android Studio to warn you about much until after the script is already running.  Kotlin on the other hand is statically typed, so lots of bugs can be checked by the compiler before the script is ever run.

Along with this check comes autocomplete.  Android Studio will be able to suggest common method names and variables to you the way it already does in Kotlin or Java files you type in.  Couple this with the comfort that you already (hopefully) have with Kotlin, and it’s an instant improvement.

Prepping for the Leap:

So we’ve decided it may be worth looking into. What’s the first step? Step 1 setting yourself up to make the transition less painful.  Certain things are allowed in Groovy that aren’t in Kotlin, so if we make those conversions in the plain text, then converting the file will be easier.  One of these “things” is quotation marks. Groovy uses single quotations a lot, but those aren’t allowed in Kotlin.  So step one is replacing any single quotation marks you have with doubles.

Next you should make sure that your “apply plugin” uses are replaced with the plugin DSL block.  This is one plugin block that should encompass all of the plugins your project is using.  When your done here’s an example of what your plugins should look like:

There are unfortunately some limitations with this new plugin though, so if you encounter errors it is possible to still use the legacy plugin.

The final thing you’ll need to do is assign variables and call methods.  What I mean by this is that since Groovy doesn’t require you to explicitly write the = when declaring a variable or the () when running a method, these changes need to be made for Kotlin.  Here are a few examples:

Taking The Leap:

Ok, now we’ve made our prep changes, so hopefully things go smoothly with the actual changes.  We have three files we need to rename to include .kts. Our two build.gradle files now become build.gradle.kts, and our settings.gradle file should become settings.gradle.kts.  Rename these files and try to compile the project. Fingers crossed nothing erroneous pops up, and if that’s the case then congratulations you officially have a project running on Kotlin DSL!

These are just the basics, and I’m sure if your project is complex at all there will be a few more jumps in the process to Kotlin.  If that’s the case Gradle has documentation on their website that helps walk through how to go about some of these common changes.  I’d highly recommend checking them out if you feel lost.  And if you don’t need them, then your app should run just fine now, and any time you make changes to Gradle you can do so in Kotlin.

 

 

 

And So The Pixel 4 Leaks Begin
Android Dev Summit Is Coming!

Super Fans always leave a comment :-)

Leave a Comment

Loading Facebook Comments ...