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

删除更新项目库后androidx.annotation.NonNull无法解析问题求助

解决androidx.annotation.NonNull无法解析的问题

问题情况

  • 删除并更新项目部分库后,所有导入androidx.annotation.NonNull;的类均报「Cannot resolve symbol 'NonNull'」错误
  • 已尝试添加implementation "androidx.annotation:annotation:1.5.0"依赖,但问题未解决
  • 按下Alt+Enter时,IDE自动将导入语句替换为import io.reactivex.annotations.NonNull;
  • 当前模块Gradle依赖配置如下:
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.annotation:annotation:1.5.0"
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.github.pwittchen:reactivenetwork-rx2:3.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
implementation 'com.j256.ormlite:ormlite-android:6.1'
implementation 'com.google.code.gson:gson:2.10'

解决步骤

  1. 重新同步Gradle依赖
    点击Android Studio右上角的「Sync Project with Gradle Files」按钮,等待同步完成。若同步过程中出现下载失败等报错,先解决网络或仓库配置问题。

  2. 清理并重建项目
    依次执行菜单栏的Build > Clean Project和Build > Rebuild Project,彻底清除旧编译缓存,重新生成编译文件。

  3. 清空IDE缓存并重启
    执行File > Invalidate Caches...,勾选「Invalidate and Restart」选项,让IDE清空缓存后重启,多数符号解析类问题可通过此操作解决。

  4. 强制指定androidx注解依赖优先级
    由于项目同时引入了RxJava的注解库,IDE可能优先识别Rx版本的注解。在模块的build.gradle中添加以下配置,强制使用指定版本的androidx注解:

    configurations.all {
        resolutionStrategy.force "androidx.annotation:annotation:1.5.0"
    }
    

    添加完成后再次同步Gradle。

  5. 检查依赖树与冲突
    执行终端命令./gradlew dependencies(Windows系统使用gradlew.bat dependencies),查看项目依赖树,确认是否有其他库覆盖了androidx.annotation的版本。若存在冲突,可通过上述resolutionStrategy强制指定版本解决。

  6. 全局修正导入语句
    使用IDE的全局替换功能(Edit > Find > Replace in Path),查找import io.reactivex.annotations.NonNull;,替换为import androidx.annotation.NonNull;,勾选「Match entire line」避免误替换。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:55:20