15

I'm writing my unit tests using gradle in Android Studio. I have the tests located in:

src/androidTest/java

That works just fine. Now in my tests I want to access a resource that is defined in:

src/androidTest/res

Specifically, I want to get src/androidTest/res/layout/mylayout.xml. The problem is the R class for my test package or any package for that matter has mylayout defined.

I did this on my build.gradle and it didn't make a difference:

sourceSets {
    androidTest.resources.srcDirs = ['src/res']
}

How do I supply resources to androidTest classes that is only visible to the androidTest?

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
vangorra
  • 1,631
  • 19
  • 24
  • Why do you need to have resources available just to the test? – dragi Jun 25 '14 at 07:03
  • 4
    When you write tests especially for network protocols, you need a defined set of sample data that will work and sample data that will not work. It would be ridiculous to include those resources in the app as they serve no purpose other than testing. – vangorra Oct 27 '14 at 20:02

2 Answers2

16

For me, the solution was to put the source files in src/androidTest/java and the resource files in src/androidTest/res. Those are the standard locations, so you don't need to edit anything in gradle.build.

Plus, rename your R import from

import com.mycompany.myappname.R;

to

import com.mycompany.myappname.test.R;

Source: Configuring res srcDirs for androidTest sourceSet

Community
  • 1
  • 1
Damnum
  • 1,839
  • 16
  • 35
  • 3
    This helped me, although you can't import both the test and the regular "R" class at the same time. Since I wanted to use my app resource id's in my tests as well, I had to reference the test app's "R" file using the fully-qualified class name, i.e. `int someId = com.mycompany.myappname.test.R.raw.some_raw_resource;` – Chad Apr 14 '16 at 14:09
  • 1
    @aaronmarino I am getting the same thing. – Reid Mac Jan 16 '20 at 18:12
1

I found the solution, add this to your build.gradle file.

sourceSets {
    androidTest {
        java.srcDirs = ['src/androidTest/src']
        resources.srcDirs = ['src/androidTest/src']
    }
}
vangorra
  • 1,631
  • 19
  • 24
  • 1
    I'm not getting this to work for me either. Currently using 'com.android.tools.build:gradle:0.13.+' with installed Gradle 2.1. Changed the paths slightly to 'src/androidTest/java' and 'src/androidTest/res'. By the syntax, it appears that it should work. Interestingly, I can see that the intermediate resource files (pre-merge) are being generated for the layouts in my test folders. Anyone get this working? Please? :-) – Dan Devine Dec 01 '14 at 19:32
  • Shame. I'm getting the same error again and this isn't working anymore. – vangorra Feb 10 '15 at 19:45
  • This should be the default for those folders, but I've found that for library projects these are ignored: http://stackoverflow.com/questions/31039257/how-to-run-instrumentation-tests-for-an-android-library-in-android-studio/31083131#31083131 – CasualT Jun 26 '15 at 23:55
  • Other testing I've done with app based projects shows that the folders are used...but any IDs aren't added to R file, and therefore aren't available...also, the androidTest .xml files can't reference any of the main dimens or strings values (they are restricted to ones within androidTest). :( – CasualT Jun 26 '15 at 23:57
  • Anyone found the solution to this problem? I'm struggling with this too – Damnum Jul 18 '15 at 15:31
  • These are the default locations - shouldn't have to be specified at all in order to take effect – Stan Kurdziel Jul 30 '17 at 05:46
  • I agree with @stan-kurdziel, these are the default locations and there is no need to specify that. The only reason I can see to specify is if you are trying to use resources from test and not androidtest or getting the source set from a sibling project as in master-slave topology. – Hitesh Dec 05 '17 at 23:22