Android Espresso测试运行失败,AndroidJUnitRunner找不到FileTestStorage类
问题根源
报错指向的androidx.test.platform.io.FileTestStorage类缺失,是因为你使用的1.4.1-alpha01、3.5.0-alpha01版本的AndroidX测试套件属于不稳定预览版,拆分出了独立的test-platform组件没有被默认引入,同时你存在测试依赖作用域配置错误的问题。
解决方案
1. 修正依赖版本(优先选择稳定版避免兼容性问题)
替换build.gradle中dependencies块下的测试相关依赖:
dependencies { // 其他原有依赖保持不变 // 删掉原来放在implementation里的androidx.test.ext:junit,测试依赖只能放在androidTestImplementation下 androidTestImplementation 'androidx.test.ext:junit:1.1.3' // 替换原来的alpha版测试依赖为统一版本的稳定版 androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test:rules:1.4.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
如果确实需要使用alpha版功能,不需要替换版本,只要补加缺失的依赖即可:
androidTestImplementation 'androidx.test:test-platform:1.4.1-alpha01'
2. 清理重建项目
执行Android Studio菜单栏 File → Invalidate Caches / Restart 清理缓存,重启后重新构建项目运行测试即可。
可选优化
你代码中使用的ActivityTestRule已经被官方废弃,建议换回你注释掉的ActivityScenarioRule,后续兼容性和稳定性更好。
内容的提问来源于stack exchange,提问作者MKE
相关产品推荐
相关产品推荐

