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

出现Unresolved reference: ImmutableList错误,需导入什么库?

解决Unresolved reference: ImmutableList错误的方案

ImmutableList并非Android SDK默认包含的类,需要手动添加对应依赖库才能解决这个错误,以下是两种可行方案:

方案1:引入Guava库

Guava是Google提供的工具库,ImmutableList是其中的核心集合类之一。在模块级build.gradle(或build.gradle.kts)的dependencies块中添加依赖:

// Groovy语法
implementation 'com.google.guava:guava:32.1.3-android'
// Kotlin DSL语法
implementation("com.google.guava:guava:32.1.3-android")

同步项目后,手动添加导入语句即可:

import com.google.common.collect.ImmutableList

方案2:使用AndroidX Collection库

如果不想引入体积较大的Guava,可以选择AndroidX提供的轻量级集合库,同样包含ImmutableList实现:

// Groovy语法
implementation 'androidx.collection:collection-ktx:1.3.0'
// Kotlin DSL语法
implementation("androidx.collection:collection-ktx:1.3.0")

导入语句为:

import androidx.collection.ImmutableList

补充说明

Android Studio未提供自动导入选项,是因为项目中还没有添加对应依赖库。添加依赖并同步后,IDE就能识别该类,后续也会正常提示导入操作。

内容的提问来源于stack exchange,提问作者user19763422

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 20:15:33