
Migrating to Gradle Kotlin DSL
A Step by Step Guide
Tom Hanley
Senior Software Engineer @
Organiser of the Dublin Kotlin User Group
Huge fan of Kotlin





What is Gradle?
General purpose build management system
Gradle Creation
- They chose Groovy over Java because:
- It could provide an extensible DSL
- Highly declarative builds
- It could provide an extensible DSL
- Didn't realise the limitations around tooling
- Due to Groovys dynamic types and metaprogramming, tooling is hard
- Bad user experience
- Hard to configure and refactor
Gradle Kotlin DSL
-
From Hans Dockter, the creator of Gradle
-
"Kotlin is better than groovy"
-
"I wish Kotlin existed when I created Gradle"
-
"You lucky b*******s"
-
Gradle Kotlin DSL
- Why switch to the Gradle Kotlin DSL?
- Better developer experience
- Type safe build logic
- Great IDE Support
- Shorter feedback loop
- Way more compile time checks
- Find usages, refactoring support
- Groovys dynamism has performance hit
- Null Safety
- Documentation + discovery
- Better developer experience
Gradle Kotlin DSL
- Shipped as part of Gradle 5.0
- Kotlin + groovy can live side by side
- Due to groovy dynamism
- Kotlin should drive groovy, not the opposite
Migration Demo
Migration Prep
- Prepare all gradle scripts:
- Use replace ' with "
- Disambiguate function calls and property assignments
- Use plugins block
- Make sure everything still compiles
Shared code
-
Create the buildSrc folder
-
Create the src/main/kotlin directory
-
Create the build.gradle.kts file
-
Plugins block with `kotlin-dsl` applied
-
Repositories block with jcenter()
-
-
Create Dependencies.kt file
-
Make sure everything compiles
-
The Real Migration
- Rename scripts to .gradle.kts
- settings.gradle
- build.gradle
- app/build.gradle
- Fix issues
- Where the bulk of the time is spent
The Issues
-
build.gradle
-
compileSdkVersion
-
isEnabled
-
Release
-
getByName
-
-
isMinifyEnabled
-
kotlinOptions
-
Migration 3
-
remove libraries namespace
-
issue with list of dependencies
-
Create extension functions
-
-
Test logging fix
-
Refactor- extract function
-
-
-
Delete dependencies.gradle file
Key Takeaways
- The migration can be tricky
- No auto convert from groovy to kotlin
- Differences in Kotlin and Gradle APIs.
- You don't get much IDE feedback when scripts aren't compiling
- Its worth it in the end, it'll make your life easier
Coming up
- Arrow night
- Kotlin Christmas Katas
- KotlinConf '19, Copenhagen


are hiring!

Thank you!
Questions?
Twitter: @conduct_of_code
Linkedin: Tomás Hanley
Medium: @hanleyt
Migrating to Gradle Kotlin DSL
By Tom Hanley
Migrating to Gradle Kotlin DSL
- 478