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

Android Studio编译报错:找不到androidx.core.view.MenuHost类如何解决

问题原因

androidx.core.view.MenuHost 是 AndroidX Core 1.7.0 及以上版本才新增的类,你当前在 build.gradle 中强制固定了 Core 版本为 1.6.0,但引入的 androidx.appcompat:appcompat:1.4.0、androidx.activity:activity:1.4.0 等组件都要求 Core 版本≥1.7.0,版本不匹配就会触发类找不到的报错。

解决方案

按以下步骤修改配置即可:

  • 升级 Core 依赖版本
    找到 app 模块 build.gradle 中 configurations.all 的版本强制配置,将 Core 版本从 1.6.0 升级到 1.7.0 及以上:
    configurations.all {
        resolutionStrategy {
            force 'androidx.core:core:1.8.0'
            force 'androidx.core:core-ktx:1.8.0'
        }
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
    }
    
    同时找到 dependencies 中声明的 core 依赖,同步升级版本:
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.core:core:1.8.0'
    
  • 清理冲突和重复依赖
    • 删除已废弃的支持库约束布局依赖:implementation 'com.android.support.constraint:constraint-layout:2.0.4',你已经声明了对应AndroidX版本的依赖,该旧版本会引发冲突。
    • 保留正式版material依赖,删除alpha测试版声明:删掉 implementation 'com.google.android.material:material:1.4.0-alpha02',仅保留 implementation 'com.google.android.material:material:1.4.0' 即可。
    • 删除所有重复声明的依赖:你重复引入了appcompat、test相关依赖,保留其中一份即可。
    • 替换multidex依赖:删掉旧支持库版本的 implementation 'com.android.support:multidex:1.0.3',替换为AndroidX版本:
      implementation 'androidx.multidex:multidex:2.0.1'
      
  • 清理缓存重新构建
    修改完配置同步Gradle后,点击顶部菜单 Build -> Clean Project 清理项目,再点击 File -> Invalidate Caches / Restart 重启Android Studio,之后重新构建项目即可解决报错。

可选优化建议:如果后续还有兼容性报错,可以把compileSdkVersion和targetSdkVersion升级到31及以上,和高版本的AndroidX组件适配性更好。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 16:15:08