43

I have the following folder structure in Android Studio:

├── androidTest
│   ├── java
│   └── res
│       └── raw
│           └── test_file
└── main
    ├── java
    └── res
        └── raw
            └── app_file

I'm trying to access the test_file resource which exists in the raw folder of the androidTest elements. Here's the code inside a Robotium test case that inherits from ActivityInstrumentationTestCase2:

InputStream is = this.getInstrumentation()
                 .getContext()
                 .getResources()
                 .openRawResource(R.raw.test_file);

Android Studio throws a reference error since the resource cannot be found. The exact error is "Cannot resolve symbol test_file".

How can I reference this resource form a test case, which exists on the androidTest resources bundle?

3
  • Did you have a tool that would generate the tree for you? Aug 24, 2015 at 16:33
  • Yes, the bash 'tree' tool
    – jlhonora
    Aug 24, 2015 at 16:34
  • 1
    From where are you trying to get your test_file ? From a Robotium test written in androidTest/java ?
    – pdegand59
    Aug 24, 2015 at 16:39

4 Answers 4

54
+50

By default your androidTest project will include your app's R class, but androidTest's resources will be generated into a separate file. Make sure you import the R class from your test project:

import com.your.package.test.R;

[..]

getInstrumentation().getContext().getResources().openRawResource(R.raw.test_file);

You can also directly reference the test project's R class:

getInstrumentation().getContext().getResources().openRawResource(com.your.package.test.R.raw.test_file);
4
  • 8
    I've got the same file stucture as the OP, but raw is not generated for androidTest variant. Jan 29, 2016 at 12:51
  • @YaroslavMytkalyk what kind of file are you putting in the raw folder?
    – jlhonora
    Jan 29, 2016 at 13:22
  • @jlhonora a binary file with no extension. Tried renaming it adding some arbitrary extension, which didn't change anything. Feb 1, 2016 at 10:50
  • By doing so, the resource ends in the published APK too. How to avoid that? Apr 21, 2020 at 7:20
18

I had the androidTest resources in the right spot (src/androidTest/res) and I still couldn't access them via <normal.package>.test.R. I spent a lot of time googling trying to figure out what was going on..

I FINALLY stumbled onto the answer. If you're building a buildType where you specified an applicationIdSuffix, your files are at <applicationId><applicationIdSuffix>.test.R !!!!

i.e.

applicationId "com.example.my.app"

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
}

if you have androidTest resources in the right directory, then you can only access them via com.example.my.app.debug.test.R !!!!

1
  • Even without applicationIdSuffix you should use < applicationId>.test.R in case applicationId is different than your package name.
    – yshahak
    Aug 5, 2020 at 12:24
1

See Android Unit Tests Requiring Context. For instrumentation test use InstrumentationRegistry.getInstrumentation().getTargetContext() (in Kotlin: getInstrumentation().targetContext). Then you can access resources. You won't need to import R file.

1

As the others have stated, androidTest resource ids are generated in <applicationId>.test.R by default. Since applicationId can be modified by different build types and flavors, this leads to different R files location for each of them. This can be changed by assigning explicit value to testApplicationId in the defaultConfig of the app's android configuration in build.gradle. It can be useful if there are more than one build types/flavors that alter the appId, but the tests can be run for any of them.

build.gradle (app):

android {
    defaultConfig {
    applicationId "com.example.hello"
    testApplicationId = "com.example.hello.test"
    }
}

Test files:

import com.example.hello.test.R

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.