Search code examples
gradle-kotlin-dsl

Unresolved reference in build.gradle.kts


Given build.gradle.kts

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    application
    id("org.jetbrains.kotlin.jvm") version "1.9.21"
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}


ext {
    val projectShortName = "My Project short name"
}

application {
    applicationName = project.ext.get("$projectShortName").toString()
}

But when use

./gradlew run

rise error:

* What went wrong:
Script compilation error:

      applicationName = project.ext.get("$projectShortName").toString()
                                                   ^ Unresolved reference: projectShortName

1 error

Solution

  • The extra property name here is projectShortName. It needs to be accessed like a String, instead of as a variable. So the $ sign before projectShortName in the project.ext.get() call needs to be removed. Also the declaration of the property needs to be done differently. There are multiple ways to do it. One of the ways is below:

    extra.apply {
        set("projectShortName", "My Project short name")
    }
    
    application {
        applicationName = project.ext.get("projectShortName").toString()
    }