尝试访问其他文件时切换到 Kotlin DSL 未解析的参考

OLe*_*ert 8 android kotlin gradle-kotlin-dsl

我在尝试对我的 gradle 文件使用 Kotlin DSL 时遇到错误。

build.gradle(app)我有一个函数来检索存储在文件中的 api 密钥中keys.properties,Groovy 中的函数如下:

// Retrieve key api
def getApiKey() {
    def keysFile = file("keys.properties")
    def keysProperties = new Properties()
    keysProperties.load(new FileInputStream(keysFile))
    def apiKey = keysProperties['API_KEY']
    return apiKey
}
Run Code Online (Sandbox Code Playgroud)

当切换到 Kotlin DSL 时,我天真地将函数更改如下:

// Retrieve key for TMDB api
fun getApiKey() {
    val keysFile = file("keys.properties")
    val keysProperties = Properties()
    keysProperties.load(FileInputStream(keysFile))
    val apiKey = keysProperties["API_KEY"]
    return apiKey
}
Run Code Online (Sandbox Code Playgroud)

然后构建返回以下错误:

.../app/build.gradle.kts:13:26: Unresolved reference: Properties
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?

编辑

按照 #bam bam 的建议,添加导入import java.util.Properties解决了问题..但是出现了其他问题,请参阅这个问题

小智 11

你导入类了吗?添加import java.util.Properties到您的 build.gradle.kts 之上