我想在我的
androidTest模块中保留一个Sting和Raw文件,并在Espresso测试中需要时读取它.
我在androidTest下保留res文件夹,并且能够同步并生成R文件.但是当我尝试使用时访问字符串资源
getTargetContext.getString(R.string.product_name)或使用mAcitivityRule.getActivity.getString(R.string.product_name)我得到一些随机值,甚至在资源文件中的任何地方都没有使用.
String output: "res/drawable-v21/abc_action_bar_item_background_material.xml"
有什么办法可以保留并在我的项目测试模块中使用String资源,它不会添加到我的生成版本中.
解决方法
您应该使用InstrumentationRegistry.getContext()而不是InstrumentationRegistry.getTargetContext(),然后使用适当的包生成R文件(默认情况下,它会将.test附加到您的包中):
Resources resources = InstrumentationRegistry.getContext().getResources(); String name = resources.getString(com.your_package.test.R.string.product_name);
注意资源名称中的测试部分.