Issue
I want to keep a Sting and Raw file in my androidTest module and read it when needed in Espresso test.
I keep res folder under androidTest and able to sync and generate R file for the same. But when I tried to access string resource using
getTargetContext.getString(R.string.product_name)
or using mAcitivityRule.getActivity.getString(R.string.product_name)
I am getting some random value which is even not used anywhere in resource file.
String output: "res/drawable-v21/abc_action_bar_item_background_material.xml"
Is there any way I can keep and use String resource in my project test module and it will not add into my production build.
Solution
You should use InstrumentationRegistry.getContext()
instead of InstrumentationRegistry.getTargetContext()
and then use appropriate package for generated R file (by default it will append .test
to your package):
Resources resources = InstrumentationRegistry.getContext().getResources();
String name = resources.getString(com.your_package.test.R.string.product_name);
Pay attention to test
part in resource name.
Answered By - Alexander Mironov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.