skip to content

What are the most common errors engineers hit when converting build.gradle to build.gradle.kts, and how do you fix each?

level: middleimportance: should knowfreq 40%

answer

  1. ' -> " ; : -> = ; def -> val
  2. Property = -> .set(...)
  3. typed values (JavaVersion) not raw numbers
  4. $buildDir -> layout.buildDirectory
  5. task foo {} -> tasks.register

basics

~10 s

Typical errors: single quotes (use double), missing parentheses, def instead of val/var, assigning a lazy Property with = instead of .set(...), and using "$buildDir" patterns that are deprecated. Fix each to Kotlin syntax.

solid answer

~40 s

The recurring conversion errors are mechanical and the compiler catches most of them: - **Strings:** `'x'` (Groovy single quote) → `"x"`. Single quotes in Kotlin are `Char` literals. - **Parentheses:** Groovy command syntax `from 'src'` → `from("src")`. - **Variables:** `def x = 1` → `val x = 1` (or `var`). - **Lazy properties:** assigning a `Property<T>` with `=` fails — use `archiveFileName.set("a.jar")`. - **Named args:** Groovy `apply plugin: 'java'` → `apply(plugin = "java")` (`:` becomes `=`). - **Booleans/types:** Groovy is lenient about types; Kotlin needs the right type, e.g. `JavaVersion.VERSION_17` not `17`. - **buildDir:** `"$buildDir/out"` → `layout.buildDirectory.dir("out")` (the `buildDir` property is deprecated). - **Eager tasks:** `task foo {}` → `tasks.register("foo") {}`. Working in the IDE plus running `./gradlew help` after each file makes these fast to find and fix.

code

kotlin · 6 lines
kotlin
// Before (Groovy) vs after (Kotlin):
// def v = '17'                  ->  val v = "17"
// sourceCompatibility = 1.8     ->  java { sourceCompatibility = JavaVersion.VERSION_1_8 }
// ext.appName = 'svc'           ->  extra["appName"] = "svc"
// jar { archiveFileName = 'a' } ->  tasks.jar { archiveFileName.set("a") }
// "$buildDir/out"               ->  layout.buildDirectory.dir("out")

go deeper

for a junior

Recognize the quote/parenthesis/def errors and their fixes.

for a middle

Add the Property.set, typed-value, ext, and buildDir-deprecation pitfalls.

for a senior

Explain why these surface at compile time and how IDE + help make them a fast loop.

for a principal

Capture the fixes as lint/convention-plugin guidance so future modules avoid the same traps.

## These are mostly compile-time, which is good Because the Kotlin DSL is statically compiled, the majority of conversion mistakes fail **before** any task runs, with a clear error message. Here's the catalog and the fix for each. ### 1. Single vs double quotes Groovy: `'java'`. Kotlin: `"java"`. A single-quoted literal in Kotlin is a `Char` and won't even parse for multi-char text. ### 2. Missing parentheses / named-arg colon Groovy command-style: `from 'src/main'`, `apply plugin: 'java'`. Kotlin: `from("src/main")`, `apply(plugin = "java")`. Note the named-argument separator changes from `:` to `=`. ### 3. def -> val/var Groovy `def name = 'app'` becomes `val name = "app"`. Use `var` only if reassigned. ### 4. Lazy Property assignment Modern Gradle types like `Property<T>`, `RegularFileProperty`, `DirectoryProperty` are assigned with `.set(...)`, not `=`: ```kotlin tasks.jar { archiveFileName.set("app.jar") } ``` Groovy hid this behind a convenience setter; Kotlin exposes the real API. ### 5. Strong typing of values Groovy: `sourceCompatibility = 1.8`. Kotlin wants a typed value: `JavaVersion.VERSION_1_8` (or a string `"1.8"` depending on the property). Passing a raw number where an enum/JavaVersion is expected won't compile. ### 6. buildDir deprecation `"$buildDir/reports"` interpolation is discouraged; the property is deprecated in favor of the lazy `layout.buildDirectory`: ```kotlin layout.buildDirectory.dir("reports") ``` ### 7. ext / extra properties Groovy `ext.foo = 'bar'` becomes `extra["foo"] = "bar"` (via the `ExtraPropertiesExtension`), and reads are `val foo: String by extra`. ### 8. Eager task creation `task foo(type: Copy) {}` → `tasks.register<Copy>("foo") {}` (lazy, recommended). ## How to find and fix fast Keep IntelliJ open (it red-underlines each of these), and after editing a file run `./gradlew help`. The combination of IDE inspections + a cheap script-compiling task turns the whole list into a quick fix-as-you-go loop.

  • Why does "$buildDir/out" trigger a deprecation warning in modern Gradle?
    The eager project.buildDir property is deprecated in favor of the lazy layout.buildDirectory (a DirectoryProperty), which integrates with the configuration cache and lazy APIs. Use layout.buildDirectory.dir("out").
  • How do you migrate Groovy ext properties to Kotlin?
    Write extra["key"] = value to set, and read with a delegate: val key: String by extra. They map to the ExtraPropertiesExtension.

saying these in an interview costs you the question

  • Assigning a lazy Property with = and not understanding why it fails.
  • Leaving raw numeric values where Kotlin expects an enum/JavaVersion.
  • Continuing to use $buildDir interpolation in new Kotlin scripts.

context