You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle中使用libs.versions.toml设置composeOptions遇.get()报错求助

问题

尝试通过libs.versions.toml中的版本配置composeOptions,代码如下:

android {
 ...
    composeOptions {
        kotlinCompilerExtensionVersion = libs.versions.kotlinCompilerExtensionVersion.get()
    }
}

调用.get()时出现错误:

Unresolved reference. None of the following candidates is applicable
because of receiver type mismatch: public inline operator fun <K, V>
Map<out TypeVariable(K), TypeVariable(V)>.get(key: TypeVariable(K)):
TypeVariable(V)? defined in kotlin.collections public operator fun
MatchGroupCollection.get(name: String): MatchGroup? defined in
kotlin.text

TOML版本管理在应用插件和引入依赖库时可正常工作,但调用.get()获取版本时失效,使用的Gradle版本为7.5.1。

解决方法

Gradle 7.5.1中,libs.versions下的版本变量不需要调用.get()方法,直接通过属性访问即可。修改代码如下:

android {
 ...
    composeOptions {
        kotlinCompilerExtensionVersion = libs.versions.kotlinCompilerExtensionVersion
    }
}

Gradle的版本目录(Version Catalog)机制里,7.0+版本的版本变量本身是可直接赋值的Provider<String>类型,会自动解析为对应版本字符串,无需手动调用.get()。早期版本可能需要该方法,但7.5.1已无需额外调用,直接引用变量就能完成赋值。

内容的提问来源于stack exchange,提问作者M.Devan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:51:55