Search code examples
kotlinproperties-filegradle-kotlin-dsl

Unresolved Reference getProperty


I'm trying to load a property from my local.properties file in my build.gradle.kts like this:

val properties = Properties().load(project.rootProject.file("local.properties").inputStream())
val key: String = properties.getProperty("key")

But I get the following error:

e: /build.gradle.kts:37:30: Unresolved reference: getProperty

Why is that happening? It can find the class Properties from java.util.Properties, but not the function getProperty. That doesn't make any sense to me. How can I resolve this?

This is the whole build file:

Full build.gradle.kts file:

import java.util.Properties

plugins {
    kotlin("js") version "1.5.20"
}

group = "de.example"
version = "0.0.1-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(npm("obsidian", "0.12.5", false))
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.5.1")
}

kotlin {
    js(IR) {
        binaries.executable()
        browser {
            useCommonJs()
            webpackTask {
                output.libraryTarget = "commonjs"
                output.library = null
                outputFileName = "main.js"
            }
            commonWebpackConfig {
                cssSupport.enabled = true
            }
        }
    }
}

val properties = Properties().load(project.rootProject.file("local.properties").inputStream())
val key: String = properties.getProperty("key")

Solution

  • load method of Properties class returns void, so your val properties is kotlin.Unit.

    To get desired result, you need to initialize properties in the following way:

    val properties = Properties().apply { load(project.rootProject.file("local.properties").inputStream()) }
    

    Anyway, that's not a recommended way to pass configuration properties into Gradle build script (see https://docs.gradle.org/current/userguide/build_environment.html)