15

I want to put data in xml file of androidTest/res/values/string.xml folder. I have created the file say test.xml with below contents.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="test">hello</string>
</resources>

When i am trying to access the field test via R.string.test. It is not accessible and says "cannot resolve symbol test". Can someone please advice me here.

Bingo
  • 269
  • 3
  • 7
  • you dont need define strings in test folder , define them in main projects resource file – saber safavi Apr 30 '16 at 14:02
  • 2
    @sabersafavi , actually we have credit card etc information that we want to store and use them in espresso tests. As its critical information, we neither want it to be stored in the main project file nor want to delete it every time when we build a release build apk. – Bingo May 01 '16 at 13:31
  • 2
    Possible duplicate of [Configuring res srcDirs for androidTest sourceSet](http://stackoverflow.com/questions/26663539/configuring-res-srcdirs-for-androidtest-sourceset) – MyDogTom May 02 '16 at 06:42
  • @MyDogTom, i already tried the solution provided in the link and it does not work. I can not locate the resources. – Bingo May 02 '16 at 07:17
  • @Bingo I tested the provided solution and it works. You need to create a file `strings.xml` under `src/androidTest/res/values` and import `com.yourpackage.test.R` into your test. – MyDogTom May 02 '16 at 08:14
  • 2
    The trick here is the right import, because by default AS imports `com.yourpackage.test.R` instead of `com.yourpackage.test.R`. So you need to fix it manually. – MyDogTom May 02 '16 at 08:20
  • @MyDogTom, i have tried it. I can only see one default parameter that can be imported. com.android.todo.test.R.string.app_name i can not access any newly added parameter – Bingo May 02 '16 at 09:19
  • @Bingo please provide more information about your setup, your test etc. Ideally a simple project which will illustrate this strange behavior. – MyDogTom May 02 '16 at 18:48
  • 1
    @MyDogTom Thanks, I got it working now. The missing part was including it in the gradle file sourceSets { debug { res.srcDirs = ['src/main/res', 'src/test/res', 'src/androidTest/res', 'src/debug/res'] } } – Bingo May 07 '16 at 19:14
  • 1
    @Bingo Can you help me to get it run, I have similar configuration in my application but when I tried to access string resource using getTargetContext.getString(R.string.product_name) or using mAcitivityRule.getActivity.getString(R.string.product_name) 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" – Krishnakant Jul 25 '16 at 07:46
  • 1
    @user3400729 `getTargetContext()` will read the values from the app's APK; I think the point with the `androidTest` folder is that the instrumentation tests are a different APK so you need to use `getContext()` for reading resources from that. – TWiStErRob Aug 29 '16 at 21:48

3 Answers3

24

This is how you do it:

import com.my.example.test.R;

Resources resources = InstrumentationRegistry.getContext().getResources();
String terms = resources.getString(R.string.test);
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
10

androidx.test.platform.app.InstrumentationRegistry is deprecated use androidx.test.core.app.ApplicationProvider

val targetContext = ApplicationProvider.getApplicationContext<Context>()
val test: String =  targetContext.resources.getString(R.string.test)
Joseph
  • 5,793
  • 4
  • 34
  • 41
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

I have a longstanding Android library project in which I had renamed my sources' package at some point from a.b.c.d to a.b.c but I had forgot to change the package value in my library's AndroidManifest.xml file likewise. This led to some confusion when I added a res/values/strings.xml file to my library's androidTest folder but I was unable to see an a.b.c.test.R object in my test classes.

The solution for me was to rename the package value in my library's AndroidManifest.xml file to match my sources' package (i.e. a.b.c). I am now able to see an a.b.c.test.R object in my test classes and I am able to access the string resources defined in androidTest/res/values/strings.xml via the InstrumentationRegistry.getInstrumentation().context.getString(resId:) method.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147