Android Studio中testFixtures字符串资源无法访问求助
解决方案
引用正确的R类路径
testFixtures源集的资源会生成独立的R类,位于[你的主包名].testfixtures.R下,而非主包的R。比如主包是com.example.myapp,你需要通过com.example.myapp.testfixtures.R.string.string_name来引用testFixtures下的资源,而不是直接用R.string.string_name。确保test源集依赖testFixtures
在模块的build.gradle中,给test源集添加对testFixtures的依赖,这样test代码才能访问到testFixtures的资源:
dependencies { testImplementation testFixtures(project()) }
验证资源目录结构
确认testFixtures的资源放在正确路径下:src/testFixtures/res/values/strings.xml(其他资源类型对应各自的子目录,比如drawable、layout等),目录层级不能出错。清理重建项目
执行Gradle清理命令清除缓存,再重新构建testFixtures和test相关的变体:
./gradlew clean assembleTestFixtures assembleDebugTest
完成后同步Gradle项目,必要时重启Android Studio。
内容的提问来源于stack exchange,提问作者Petrakeas
相关产品推荐
相关产品推荐

