Unresolved References in settings.gradle.kts moving from 5.6 to 6.2

I use the following to keep version strings in a single file (gradle.properties) and use them from either the build script or settings.gradle.kts

val kotlinVersion: String by settings

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            when(requested.id.id) {
                "org.jetbrains.kotlin.jvm" -> useVersion(kotlinVersion)

However, this doesn’t work with gradle 6. I’m getting

Unresolved reference: kotlinVersion

when trying to run a build.

Is there a way I can still achieve the goal of keeping all version strings in gradle.properties and access them in the settings file?

Thanks.

1 Like

I have the same problem when migrate from 5.6.4 to 6.4.
did you found what is the problem?

No, we are still using 5.6

I had the same problem in Gradle 6.4.1. This is the solution I found:

pluginManagement {
    fun PluginManagementSpec.loadProperties(fileNameWithoutExtension: String, path: String = rootDir.absolutePath): java.util.Properties {
        val properties = java.util.Properties()
        File("$path/$fileNameWithoutExtension.properties").inputStream().use {
            properties.load(it)
        }
        return properties
    }
    val pluginVersions: java.util.Properties = loadProperties("pluginVersions")
    val dockerRunPluginVersion: String by pluginVersions
    resolutionStrategy {
        eachPlugin {
            when (requested.id.id) {
                "com.palantir.docker-run" -> useVersion(dockerRunPluginVersion)
            }
       }
    }
}

Essentially it look like the PluginManagementSpec object does not see any imports, variables or extension functions declared outside itself.

EDIT: Obviously, I have plugin versions stored in pluginVersions.properties rather than gradle.properties, but the underlying principle is the same.

1 Like

Thanks for this, fits perfect.

I kotlinyfied it slightly as I couldn’t resist :slight_smile:

fun PluginManagementSpec.loadProperties(fileName: String, path: String = rootDir.absolutePath) = java.util.Properties().also { properties ->
    File("$path/$fileName").inputStream().use {
        properties.load(it)
    }
}
val versions: java.util.Properties = loadProperties("gradle.properties")

for those still looking, the
val kotlinVersion: String by settings
declaration needs to be moved into the plugin management block.
See here for details

how about loading id and version inside plugins{} block using a constant String declared in a kotlin object.

I am trying that and couldn’t resolve Kotlin objects references.