Espresso截图测试调用writeToTestStorage报无Content Provider错误如何解决
Espresso 截图测试
writeToTestStorage 报错解决方案 - 报错根因:
No content provider registered错误是因为缺少 AndroidX Test Services 依赖,writeToTestStorage依赖测试服务内置的 ContentProvider 实现文件读写,无需开发者自行配置 Provider。 - 具体修复步骤:
- 在模块级
build.gradle中添加统一版本的 AndroidX Test 相关依赖:
dependencies { // AndroidX 测试核心依赖 androidTestImplementation "androidx.test:core:1.5.0" androidTestImplementation "androidx.test:runner:1.5.2" // 必须添加测试服务依赖,提供存储相关实现 androidTestImplementation "androidx.test.services:test-services:2.1.0" }- 确认
build.gradle的defaultConfig区块配置了正确的测试运行器:
android { defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } }- 删除 Manifest 中自行添加的
WRITE_EXTERNAL_STORAGE权限:该 API 通过测试服务读写应用内部专属测试目录,不需要申请外部存储权限,多余的权限配置反而可能触发不同安卓版本的权限适配问题。
- 在模块级
- API 文档获取说明:
writeToTestStorage的完整用法可以直接在 Android Studio 中通过跳转到方法定义查看官方注释,注释包含参数说明、存储路径、注意事项等完整信息。测试完成后生成的截图可以在设备的/sdcard/Android/data/[你的应用包名]/files/test_output/目录下获取。
内容的提问来源于stack exchange,提问作者Denis Podzorov
相关产品推荐
相关产品推荐

