我正在尝试从我的 local.properties
中的 build.gradle.kts
文件加载一个属性,如下所示:
val properties = Properties().load(project.rootProject.file("local.properties").inputStream())
val key: String = properties.getProperty("key")
但我收到以下错误:
<块引用>e: /build.gradle.kts:37:30: 未解析的引用:getProperty
为什么会这样?它可以从 java.util.Properties
中找到类 Properties,但不能从函数 getProperty
中找到。这对我来说没有任何意义。我该如何解决?
这是整个构建文件:
完整的 build.gradle.kts 文件:
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")
答案 0 :(得分:1)
load
类的 Properties
方法返回 void
,因此您的 val properties
是 kotlin.Unit
。
为了得到想要的结果,你需要通过以下方式初始化properties
:
val properties = Properties().apply { load(project.rootProject.file("local.properties").inputStream()) }
无论如何,这不是将配置属性传递到 Gradle 构建脚本的推荐方式(请参阅 https://docs.gradle.org/current/userguide/build_environment.html)